Skip to content

Commit

Permalink
Merge branch 'rnegron-user-creation-date-field'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla committed Sep 22, 2018
2 parents 17e1a86 + 8773a6b commit ca72f9f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
27 changes: 14 additions & 13 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion notaso/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ class UserAdmin(ImportExportModelAdmin, UserAdmin):
form = UserChangeForm
add_form = UserCreationForm

list_display = ("email", "first_name", "last_name", "is_active", "is_admin")
list_display = (
"email",
"first_name",
"last_name",
"is_active",
"is_admin",
"created_at",
"modified_at",
)
list_filter = ("is_admin", "is_active")
fieldsets = (
(None, {"fields": ("email", "password")}),
Expand Down
6 changes: 4 additions & 2 deletions notaso/users/fixtures/dummy.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}]
89 changes: 89 additions & 0 deletions notaso/users/migrations/0002_auto_20180909_2015.py
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"),
),
]
4 changes: 4 additions & 0 deletions notaso/users/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _


class MyUserManager(BaseUserManager):
Expand Down Expand Up @@ -33,6 +34,9 @@ class User(AbstractBaseUser):
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)

created_at = models.DateTimeField(_("created at"), auto_now_add=True)
modified_at = models.DateTimeField(_("modified at"), auto_now=True)

objects = MyUserManager()

USERNAME_FIELD = "email"
Expand Down

0 comments on commit ca72f9f

Please sign in to comment.