-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rnegron-user-creation-date-field'
- Loading branch information
Showing
5 changed files
with
120 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ | |
"model": "users.user", | ||
"pk": 1, | ||
"fields": { | ||
"password": "pbkdf2_sha256$36000$nQmi1RHXkxtM$wLJEFsVaXDQ0uxEDsR30j72A4N8g0sHcXPLTvdlz4DM=", | ||
"password": "password", | ||
"last_login": null, | ||
"email": "[email protected]", | ||
"first_name": "Juan", | ||
"last_name": "Pueblo", | ||
"is_admin": true, | ||
"is_active": true | ||
"is_active": true, | ||
"created_at": "2018-02-15 11:31:15.099956+00:00", | ||
"modified_at": "2018-03-24 20:28:11.099956+00:00" | ||
} | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.15 on 2018-09-08 17:42 | ||
from __future__ import unicode_literals | ||
|
||
import datetime | ||
|
||
import django.utils.timezone | ||
from django.db import migrations, models | ||
|
||
import pytz | ||
|
||
utc = pytz.utc | ||
|
||
|
||
def set_created_at_defaults(apps, schema_editor): | ||
print("Setting user created_at defaults...") | ||
User = apps.get_model("users", "User") | ||
db_alias = schema_editor.connection.alias | ||
|
||
users = User.objects.using(db_alias).all() | ||
for user in users: | ||
user_comments = user.comment_set.order_by("-created_at") | ||
if user_comments.exists(): | ||
first_comment = user_comments.first() | ||
created_at = first_comment.created_at | ||
user.created_at = datetime.datetime.combine( | ||
created_at, datetime.time(tzinfo=utc) | ||
) | ||
|
||
user.save() | ||
else: | ||
user.created_at = django.utils.timezone.now() | ||
user.save() | ||
|
||
|
||
def reverse_created_at_defaults(apps, schema_editor): | ||
pass | ||
|
||
|
||
def set_modified_at_defaults(apps, schema_editor): | ||
User = apps.get_model("users", "User") | ||
db_alias = schema_editor.connection.alias | ||
|
||
users = User.objects.using(db_alias).all() | ||
for user in users: | ||
user.modified_at = user.created_at | ||
user.save() | ||
|
||
|
||
def reverse_modified_at_defaults(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("users", "0001_initial")] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="created_at", | ||
field=models.DateTimeField( | ||
auto_now_add=True, null=True, verbose_name="created at" | ||
), | ||
), | ||
# Set created_at field as the User's first comment creation date, or default if no first comment | ||
migrations.RunPython(set_created_at_defaults), | ||
# Remove null=True | ||
migrations.AlterField( | ||
model_name="user", | ||
name="created_at", | ||
field=models.DateTimeField(auto_now_add=True, verbose_name="created at"), | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="modified_at", | ||
field=models.DateTimeField( | ||
auto_now=True, null=True, verbose_name="modified at" | ||
), | ||
), | ||
# Set modified_at field as the same as the created_at field by default | ||
migrations.RunPython(set_modified_at_defaults), | ||
# Remove null=True | ||
migrations.AlterField( | ||
model_name="user", | ||
name="modified_at", | ||
field=models.DateTimeField(auto_now=True, verbose_name="modified at"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters