Projet

Général

Profil

0001-attribute_kinds-compute-attribute-kinds-dictionnary-.patch

Benjamin Dauvergne, 08 mai 2015 15:40

Télécharger (4,37 ko)

Voir les différences:

Subject: [PATCH] attribute_kinds: compute attribute kinds dictionnary on
 demande (fixes #7188)

Also add some tests.
 src/authentic2/attribute_kinds.py | 33 ++++++++++++++++++++-------------
 src/authentic2/tests.py           | 30 +++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 14 deletions(-)
src/authentic2/attribute_kinds.py
1 1
import string
2 2
import json
3 3

  
4
from itertools import chain
5

  
4 6
from django import forms
5 7
from django.core.exceptions import ValidationError
6 8
from django.core.validators import RegexValidator
......
13 15

  
14 16
capfirst = allow_lazy(capfirst, unicode)
15 17

  
18

  
19
DEFAULT_ATTRIBUTE_KINDS = [
20
        {
21
          'label': _('string'),
22
          'name': 'string',
23
          'field_class': forms.CharField,
24
        },
25
]
26

  
27
def get_attribute_kinds():
28
    attribute_kinds = {}
29
    for attribute_kind in chain(DEFAULT_ATTRIBUTE_KINDS, app_settings.A2_ATTRIBUTE_KINDS):
30
        attribute_kinds[attribute_kind['name']] = attribute_kind
31
    return attribute_kinds
32

  
16 33
@to_list
17 34
def get_choices():
18 35
    '''Produce a choice list to use in form fields'''
19
    for d in ATTRIBUTE_KINDS.itervalues():
36
    for d in get_attribute_kinds().itervalues():
20 37
        yield (d['name'], capfirst(d['label']))
21 38

  
22 39
def only_digits(value):
......
46 63
        attribute_description.contribute_to_form(form)
47 64

  
48 65
def get_form_field(kind, **kwargs):
49
    defn = ATTRIBUTE_KINDS[kind]
66
    defn = get_attribute_kinds()[kind]
50 67
    if 'kwargs' in defn:
51 68
        kwargs.update(defn['kwargs'])
52 69
    return defn['field_class'](**kwargs)
53 70

  
54 71
def get_kind(kind):
55
    d = ATTRIBUTE_KINDS[kind]
72
    d = get_attribute_kinds()[kind]
56 73
    d.setdefault('serialize', json.dumps)
57 74
    d.setdefault('deserialize', json.loads)
58 75
    return d
59

  
60
ATTRIBUTE_KINDS = [
61
        {
62
          'label': _('string'),
63
          'name': 'string',
64
          'field_class': forms.CharField,
65
        },
66
]
67
ATTRIBUTE_KINDS += app_settings.A2_ATTRIBUTE_KINDS
68
ATTRIBUTE_KINDS = dict((d['name'], d) for d in ATTRIBUTE_KINDS)
src/authentic2/tests.py
12 12
from django.test.utils import override_settings
13 13
from django.contrib.auth import REDIRECT_FIELD_NAME
14 14

  
15
from . import hashers, utils, models, decorators
15
from . import hashers, utils, models, decorators, attribute_kinds
16 16

  
17 17
def get_response_form(response, form='form'):
18 18
    contexts = list(response.context)
......
505 505
        response3 = client.get('/cache/', HTTP_HOST='cache1.example.com')
506 506
        self.assertNotEqual(response1.content, response2.content)
507 507
        self.assertEqual(response1.content, response3.content)
508

  
509
class AttributeKindsTest(TestCase):
510
    def test_simple(self):
511
        from django.core.exceptions import ValidationError
512
        from django import forms
513

  
514
        with self.settings(A2_ATTRIBUTE_KINDS=[
515
                {
516
                    'label': 'integer',
517
                    'name': 'integer',
518
                    'field_class': forms.IntegerField,
519
                }]):
520
            self.assertTrue(isinstance(attribute_kinds.get_form_field('string'),
521
                    forms.CharField))
522
            self.assertEqual(attribute_kinds.get_kind('string')['name'],
523
                    'string')
524
            self.assertTrue(isinstance(attribute_kinds.get_form_field('integer'),
525
                    forms.IntegerField))
526
            self.assertEqual(attribute_kinds.get_kind('integer')['name'],
527
                    'integer')
528
            attribute_kinds.validate_siret('49108189900024')
529
            with self.assertRaises(ValidationError):
530
                attribute_kinds.validate_siret('49108189900044')
531
        with self.assertRaises(KeyError):
532
            attribute_kinds.get_form_field('integer')
533
        with self.assertRaises(KeyError):
534
            attribute_kinds.get_kind('integer')
535

  
508
-