From 88d1382f33d5058568739b491a98126a274fcdc4 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 8 May 2015 15:22:43 +0200 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(-) diff --git a/src/authentic2/attribute_kinds.py b/src/authentic2/attribute_kinds.py index bca2fd3..801892d 100644 --- a/src/authentic2/attribute_kinds.py +++ b/src/authentic2/attribute_kinds.py @@ -1,6 +1,8 @@ import string import json +from itertools import chain + from django import forms from django.core.exceptions import ValidationError from django.core.validators import RegexValidator @@ -13,10 +15,25 @@ from . import app_settings capfirst = allow_lazy(capfirst, unicode) + +DEFAULT_ATTRIBUTE_KINDS = [ + { + 'label': _('string'), + 'name': 'string', + 'field_class': forms.CharField, + }, +] + +def get_attribute_kinds(): + attribute_kinds = {} + for attribute_kind in chain(DEFAULT_ATTRIBUTE_KINDS, app_settings.A2_ATTRIBUTE_KINDS): + attribute_kinds[attribute_kind['name']] = attribute_kind + return attribute_kinds + @to_list def get_choices(): '''Produce a choice list to use in form fields''' - for d in ATTRIBUTE_KINDS.itervalues(): + for d in get_attribute_kinds().itervalues(): yield (d['name'], capfirst(d['label'])) def only_digits(value): @@ -46,23 +63,13 @@ def contribute_to_form(attribute_descriptions, form): attribute_description.contribute_to_form(form) def get_form_field(kind, **kwargs): - defn = ATTRIBUTE_KINDS[kind] + defn = get_attribute_kinds()[kind] if 'kwargs' in defn: kwargs.update(defn['kwargs']) return defn['field_class'](**kwargs) def get_kind(kind): - d = ATTRIBUTE_KINDS[kind] + d = get_attribute_kinds()[kind] d.setdefault('serialize', json.dumps) d.setdefault('deserialize', json.loads) return d - -ATTRIBUTE_KINDS = [ - { - 'label': _('string'), - 'name': 'string', - 'field_class': forms.CharField, - }, -] -ATTRIBUTE_KINDS += app_settings.A2_ATTRIBUTE_KINDS -ATTRIBUTE_KINDS = dict((d['name'], d) for d in ATTRIBUTE_KINDS) diff --git a/src/authentic2/tests.py b/src/authentic2/tests.py index 21d64a4..dce76aa 100644 --- a/src/authentic2/tests.py +++ b/src/authentic2/tests.py @@ -12,7 +12,7 @@ from django.contrib.auth.hashers import check_password from django.test.utils import override_settings from django.contrib.auth import REDIRECT_FIELD_NAME -from . import hashers, utils, models, decorators +from . import hashers, utils, models, decorators, attribute_kinds def get_response_form(response, form='form'): contexts = list(response.context) @@ -505,3 +505,31 @@ class CacheTests(TestCase): response3 = client.get('/cache/', HTTP_HOST='cache1.example.com') self.assertNotEqual(response1.content, response2.content) self.assertEqual(response1.content, response3.content) + +class AttributeKindsTest(TestCase): + def test_simple(self): + from django.core.exceptions import ValidationError + from django import forms + + with self.settings(A2_ATTRIBUTE_KINDS=[ + { + 'label': 'integer', + 'name': 'integer', + 'field_class': forms.IntegerField, + }]): + self.assertTrue(isinstance(attribute_kinds.get_form_field('string'), + forms.CharField)) + self.assertEqual(attribute_kinds.get_kind('string')['name'], + 'string') + self.assertTrue(isinstance(attribute_kinds.get_form_field('integer'), + forms.IntegerField)) + self.assertEqual(attribute_kinds.get_kind('integer')['name'], + 'integer') + attribute_kinds.validate_siret('49108189900024') + with self.assertRaises(ValidationError): + attribute_kinds.validate_siret('49108189900044') + with self.assertRaises(KeyError): + attribute_kinds.get_form_field('integer') + with self.assertRaises(KeyError): + attribute_kinds.get_kind('integer') + -- 2.1.4