Projet

Général

Profil

0001-misc-hide-disabled-attributes-and-values-42963.patch

Benjamin Dauvergne, 18 mai 2020 16:11

Télécharger (6,91 ko)

Voir les différences:

Subject: [PATCH 1/2] misc: hide disabled attributes and values (#42963)

 src/authentic2/custom_user/models.py          | 22 +++++++---
 src/authentic2/managers.py                    |  2 +-
 .../migrations/0021_attribute_order.py        |  7 +++-
 src/authentic2/models.py                      |  7 ++--
 tests/test_models.py                          | 40 +++++++++++++++++++
 5 files changed, 67 insertions(+), 11 deletions(-)
 create mode 100644 tests/test_models.py
src/authentic2/custom_user/models.py
41 41
@RequestCache
42 42
def get_attributes_map():
43 43
    mapping = {}
44
    for at in Attribute.all_objects.all():
44
    for at in Attribute.objects.all():
45 45
        mapping[at.id] = at
46 46
        mapping[at.name] = at
47 47
    return mapping
......
54 54
        if not hasattr(self.owner, '_a2_attributes_cache'):
55 55
            values = {}
56 56
            setattr(self.owner, '_a2_attributes_cache', values)
57
            for atv in self.owner.attribute_values.all():
57
            for atv in self.owner.attribute_values.filter(attribute__disabled=False):
58 58
                attribute = get_attributes_map()[atv.attribute_id]
59 59
                atv.attribute = attribute
60 60
                if attribute.multiple:
......
316 316
        update_fields = kwargs.get('update_fields')
317 317
        rc = super(User, self).save(*args, **kwargs)
318 318
        if not update_fields or not set(update_fields).isdisjoint(set(['first_name', 'last_name'])):
319
            if self.attributes.first_name != self.first_name:
320
                self.attributes.first_name = self.first_name
321
            if self.attributes.last_name != self.last_name:
322
                self.attributes.last_name = self.last_name
319
            try:
320
                self.attributes.first_name
321
            except AttributeError:
322
                pass
323
            else:
324
                if self.attributes and self.attributes.first_name != self.first_name:
325
                    self.attributes.first_name = self.first_name
326
            try:
327
                self.attributes.last_name
328
            except AttributeError:
329
                pass
330
            else:
331
                if self.attributes.last_name != self.last_name:
332
                    self.attributes.last_name = self.last_name
323 333
        return rc
324 334

  
325 335
    def can_change_password(self):
src/authentic2/managers.py
111 111

  
112 112

  
113 113
ServiceManager = BaseServiceManager.from_queryset(ServiceQuerySet)
114
AttributeValueManager = models.Manager.from_queryset(AttributeValueQuerySet)
114
AttributeValueManager = managers.QueryManager.from_queryset(AttributeValueQuerySet)
src/authentic2/migrations/0021_attribute_order.py
24 24
        migrations.AlterModelManagers(
25 25
            name='attribute',
26 26
            managers=[
27
                ('objects', django.db.models.manager.Manager()),
27
                ('all_objects', django.db.models.manager.Manager()),
28
            ],
29
        ),
30
        migrations.AlterModelManagers(
31
            name='attributevalue',
32
            managers=[
28 33
                ('all_objects', django.db.models.manager.Manager()),
29 34
            ],
30 35
        ),
src/authentic2/models.py
160 160
        verbose_name=_('order'),
161 161
        default=0)
162 162

  
163
    objects = managers.AttributeManager(disabled=False)
164 163
    all_objects = managers.AttributeManager()
164
    objects = managers.AttributeManager(disabled=False)
165 165

  
166 166
    registration_attributes = QueryManager(asked_on_registration=True)
167 167
    user_attributes = QueryManager(user_editable=True)
......
218 218
    def get_value(self, owner, verified=None):
219 219
        kind = self.get_kind()
220 220
        deserialize = kind['deserialize']
221
        atvs = AttributeValue.objects.with_owner(owner)
221
        atvs = AttributeValue.all_objects.with_owner(owner)
222 222
        if verified is True or verified is False:
223 223
            atvs = atvs.filter(verified=verified)
224 224
        if self.multiple:
......
324 324
    content = models.TextField(verbose_name=_('content'), db_index=True)
325 325
    verified = models.BooleanField(default=False)
326 326

  
327
    objects = managers.AttributeValueManager()
327
    all_objects = managers.AttributeValueManager()
328
    objects = managers.AttributeValueManager(attribute__disabled=False)
328 329

  
329 330
    def to_python(self):
330 331
        deserialize = self.attribute.get_kind()['deserialize']
tests/test_models.py
1
# -*- coding: utf-8 -*-
2
# authentic2 - versatile identity manager
3
# Copyright (C) 2010-2019 Entr'ouvert
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License as published
7
# by the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17

  
18
from __future__ import unicode_literals
19

  
20
import pytest
21

  
22
from authentic2.models import Attribute
23
from authentic2.custom_user.models import User
24

  
25

  
26
def test_attribute_disabled(db):
27
    attribute = Attribute.all_objects.create(name='test', label='test', kind='string')
28
    user = User.objects.create()
29
    user.attributes.test = 'abcd'
30

  
31
    assert user.to_json()['test'] == 'abcd'
32
    attribute.disabled = True
33
    attribute.save()
34
    assert 'test' not in user.to_json()
35

  
36
    with pytest.raises(AttributeError):
37
        assert user.attributes.test == 'abcd'
38

  
39
    with pytest.raises(AttributeError):
40
        user.attributes.test = '1234'
0
-