From 5ecd336447797f8161b49c4c74579a4526c82806 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 13 Mar 2017 14:38:14 +0100 Subject: [PATCH] custom_user: add modified field (fixes #15617) It should simplify incremental synchronization of users. --- .../custom_user/migrations/0012_user_modified.py | 22 ++++++++++++++++++++++ src/authentic2/custom_user/models.py | 4 ++++ src/authentic2/models.py | 9 ++++++++- tests/test_all.py | 1 + tests/test_api.py | 2 +- 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/authentic2/custom_user/migrations/0012_user_modified.py diff --git a/src/authentic2/custom_user/migrations/0012_user_modified.py b/src/authentic2/custom_user/migrations/0012_user_modified.py new file mode 100644 index 0000000..4b1aa2e --- /dev/null +++ b/src/authentic2/custom_user/migrations/0012_user_modified.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import datetime +from django.utils.timezone import utc + + +class Migration(migrations.Migration): + + dependencies = [ + ('custom_user', '0011_manual_attribute_values_for_name_fields'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='modified', + field=models.DateTimeField(default=datetime.datetime(2017, 3, 13, 14, 41, 7, 593150, tzinfo=utc), auto_now=True, verbose_name='Last modification time', db_index=True), + preserve_default=False, + ), + ] diff --git a/src/authentic2/custom_user/models.py b/src/authentic2/custom_user/models.py index 86bd7f7..a917117 100644 --- a/src/authentic2/custom_user/models.py +++ b/src/authentic2/custom_user/models.py @@ -72,6 +72,10 @@ class User(AbstractBaseUser, PermissionMixin): blank=True, null=True, swappable=False) + modified = models.DateTimeField( + verbose_name=_('Last modification time'), + db_index=True, + auto_now=True) objects = UserManager() diff --git a/src/authentic2/models.py b/src/authentic2/models.py index a4f6b20..61a445b 100644 --- a/src/authentic2/models.py +++ b/src/authentic2/models.py @@ -6,7 +6,7 @@ from django.conf import settings from django.db import models from django.db.models.query import Q from django.utils.translation import ugettext_lazy as _ -from django.core.exceptions import ValidationError +from django.core.exceptions import ValidationError, FieldDoesNotExist from django.contrib.contenttypes.models import ContentType from model_utils.managers import QueryManager @@ -234,6 +234,13 @@ class Attribute(models.Model): av.verified = verified av.save() + # if owner has a modified field, update it + try: + modified = owner.__class__._meta.get_field('modified') + except FieldDoesNotExist: + if getattr(modified, 'auto_now', False): + owner.save(update_fields=['modified']) + def natural_key(self): return (self.name,) diff --git a/tests/test_all.py b/tests/test_all.py index 7825267..c0f90da 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -65,6 +65,7 @@ class SerializerTests(TestCase): 'is_superuser': False, 'last_login': u.last_login, 'date_joined': u.date_joined, + 'modified': u.modified, 'groups': [], 'user_permissions': [], 'password': '', diff --git a/tests/test_api.py b/tests/test_api.py index 7fe1e08..c585479 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -172,7 +172,7 @@ def test_api_users_create(app, user): if user.is_superuser or user.roles.exists(): assert set(['ou', 'id', 'uuid', 'is_staff', 'is_superuser', 'first_name', 'last_name', 'date_joined', 'last_login', 'username', 'password', 'email', 'is_active', - 'title']) == set(resp.json.keys()) + 'title', 'modified']) == set(resp.json.keys()) assert resp.json['first_name'] == payload['first_name'] assert resp.json['last_name'] == payload['last_name'] assert resp.json['email'] == payload['email'] -- 2.1.4