Projet

Général

Profil

0001-custom_user-add-modified-field-fixes-15617.patch

Benjamin Dauvergne, 24 mars 2017 15:39

Télécharger (4,35 ko)

Voir les différences:

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
src/authentic2/custom_user/migrations/0012_user_modified.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.db import migrations, models
5
import datetime
6
from django.utils.timezone import utc
7

  
8

  
9
class Migration(migrations.Migration):
10

  
11
    dependencies = [
12
        ('custom_user', '0011_manual_attribute_values_for_name_fields'),
13
    ]
14

  
15
    operations = [
16
        migrations.AddField(
17
            model_name='user',
18
            name='modified',
19
            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),
20
            preserve_default=False,
21
        ),
22
    ]
src/authentic2/custom_user/models.py
72 72
        blank=True,
73 73
        null=True,
74 74
        swappable=False)
75
    modified = models.DateTimeField(
76
        verbose_name=_('Last modification time'),
77
        db_index=True,
78
        auto_now=True)
75 79

  
76 80

  
77 81
    objects = UserManager()
src/authentic2/models.py
6 6
from django.db import models
7 7
from django.db.models.query import Q
8 8
from django.utils.translation import ugettext_lazy as _
9
from django.core.exceptions import ValidationError
9
from django.core.exceptions import ValidationError, FieldDoesNotExist
10 10
from django.contrib.contenttypes.models import ContentType
11 11

  
12 12
from model_utils.managers import QueryManager
......
234 234
                av.verified = verified
235 235
                av.save()
236 236

  
237
        # if owner has a modified field, update it
238
        try:
239
            modified = owner.__class__._meta.get_field('modified')
240
        except FieldDoesNotExist:
241
            if getattr(modified, 'auto_now', False):
242
                owner.save(update_fields=['modified'])
243

  
237 244
    def natural_key(self):
238 245
        return (self.name,)
239 246

  
tests/test_all.py
65 65
                    'is_superuser': False,
66 66
                    'last_login': u.last_login,
67 67
                    'date_joined': u.date_joined,
68
                    'modified': u.modified,
68 69
                    'groups': [],
69 70
                    'user_permissions': [],
70 71
                    'password': '',
tests/test_api.py
172 172
    if user.is_superuser or user.roles.exists():
173 173
        assert set(['ou', 'id', 'uuid', 'is_staff', 'is_superuser', 'first_name', 'last_name',
174 174
                    'date_joined', 'last_login', 'username', 'password', 'email', 'is_active',
175
                    'title']) == set(resp.json.keys())
175
                    'title', 'modified']) == set(resp.json.keys())
176 176
        assert resp.json['first_name'] == payload['first_name']
177 177
        assert resp.json['last_name'] == payload['last_name']
178 178
        assert resp.json['email'] == payload['email']
179
-