Project

General

Profile

0003-add-a-model-representation-for-expression-attribute-.patch

Benjamin Dauvergne, 14 August 2014 12:14 PM

Download (14.8 KB)

View differences:

Subject: [PATCH 3/3] add a model representation for expression attribute
 sources

 authentic2/admin.py                                |    5 +
 authentic2/dashboard.py                            |    6 +
 .../0017_auto__add_expressionattributesource.py    |  138 ++++++++++++++++++++
 authentic2/models.py                               |   42 +++++-
 .../functionattributegenerator/change_form.html    |    9 ++
 5 files changed, 198 insertions(+), 2 deletions(-)
 create mode 100644 authentic2/migrations/0017_auto__add_expressionattributesource.py
 create mode 100644 authentic2/templates/admin/authentic2/functionattributegenerator/change_form.html
authentic2/admin.py
196 196
    form = forms.GroupAdminForm
197 197

  
198 198

  
199
class ExpressionAttributeSourceAdmin(admin.ModelAdmin):
200
    pass
201

  
202
admin.site.register(models.ExpressionAttributeSource, ExpressionAttributeSourceAdmin)
203

  
199 204
admin.site.unregister(Group)
200 205
admin.site.register(Group, A2GroupAdmin)
authentic2/dashboard.py
50 50
                'authentic2.models.Attribute'),
51 51
        ))
52 52
        self.children.append(modules.ModelList(
53
            _('Attribute sources'),
54
            models=(
55
                'authentic2.models.ExpressionAttributeSource',
56
            ),
57
        ))
58
        self.children.append(modules.ModelList(
53 59
            _('SAML2'),
54 60
            models=(
55 61
                'authentic2.saml.models.LibertyProvider',
authentic2/migrations/0017_auto__add_expressionattributesource.py
1
# -*- coding: utf-8 -*-
2
from south.utils import datetime_utils as datetime
3
from south.db import db
4
from south.v2 import SchemaMigration
5
from django.db import models
6

  
7

  
8
class Migration(SchemaMigration):
9

  
10
    def forwards(self, orm):
11
        # Adding model 'ExpressionAttributeSource'
12
        db.create_table(u'authentic2_expressionattributesource', (
13
            (u'attributesource_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['authentic2.AttributeSource'], unique=True, primary_key=True)),
14
            ('condition', self.gf('django.db.models.fields.TextField')(blank=True)),
15
            ('expression', self.gf('django.db.models.fields.TextField')()),
16
        ))
17
        db.send_create_signal(u'authentic2', ['ExpressionAttributeSource'])
18

  
19

  
20
    def backwards(self, orm):
21
        # Deleting model 'ExpressionAttributeSource'
22
        db.delete_table(u'authentic2_expressionattributesource')
23

  
24

  
25
    models = {
26
        u'auth.group': {
27
            'Meta': {'object_name': 'Group'},
28
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
29
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
30
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
31
        },
32
        u'auth.permission': {
33
            'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
34
            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
35
            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
36
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
37
            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
38
        },
39
        u'auth.user': {
40
            'Meta': {'object_name': 'User'},
41
            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
42
            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
43
            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
44
            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
45
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
46
            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
47
            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
48
            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
49
            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
50
            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
51
            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
52
            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
53
            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
54
        },
55
        u'authentic2.attribute': {
56
            'Meta': {'object_name': 'Attribute'},
57
            'asked_on_registration': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
58
            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
59
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
60
            'kind': ('django.db.models.fields.CharField', [], {'max_length': '16'}),
61
            'label': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '63'}),
62
            'multiple': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
63
            'name': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '256'}),
64
            'required': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
65
            'user_editable': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
66
            'user_visible': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
67
        },
68
        u'authentic2.attributesource': {
69
            'Meta': {'object_name': 'AttributeSource'},
70
            'description': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
71
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
72
            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
73
        },
74
        u'authentic2.attributevalue': {
75
            'Meta': {'object_name': 'AttributeValue'},
76
            'attribute': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['authentic2.Attribute']"}),
77
            'content': ('django.db.models.fields.TextField', [], {}),
78
            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
79
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
80
            'object_id': ('django.db.models.fields.PositiveIntegerField', [], {})
81
        },
82
        u'authentic2.authenticationevent': {
83
            'Meta': {'object_name': 'AuthenticationEvent'},
84
            'how': ('django.db.models.fields.CharField', [], {'max_length': '10'}),
85
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
86
            'nonce': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
87
            'when': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
88
            'who': ('django.db.models.fields.CharField', [], {'max_length': '80'})
89
        },
90
        u'authentic2.deleteduser': {
91
            'Meta': {'object_name': 'DeletedUser'},
92
            'creation': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
93
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
94
            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"})
95
        },
96
        u'authentic2.expressionattributesource': {
97
            'Meta': {'object_name': 'ExpressionAttributeSource', '_ormbases': [u'authentic2.AttributeSource']},
98
            u'attributesource_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['authentic2.AttributeSource']", 'unique': 'True', 'primary_key': 'True'}),
99
            'condition': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
100
            'expression': ('django.db.models.fields.TextField', [], {})
101
        },
102
        u'authentic2.federatedid': {
103
            'Meta': {'object_name': 'FederatedId'},
104
            'about': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
105
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
106
            'id_format': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
107
            'id_value': ('django.db.models.fields.TextField', [], {}),
108
            'provider': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
109
            'service': ('django.db.models.fields.CharField', [], {'max_length': '255'})
110
        },
111
        u'authentic2.logouturl': {
112
            'Meta': {'object_name': 'LogoutUrl'},
113
            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
114
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
115
            'logout_url': ('django.db.models.fields.URLField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
116
            'logout_use_iframe': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
117
            'logout_use_iframe_timeout': ('django.db.models.fields.PositiveIntegerField', [], {'default': '300'}),
118
            'object_id': ('django.db.models.fields.PositiveIntegerField', [], {})
119
        },
120
        u'authentic2.userexternalid': {
121
            'Meta': {'object_name': 'UserExternalId'},
122
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
123
            'external_id': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
124
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
125
            'source': ('django.db.models.fields.URLField', [], {'max_length': '256'}),
126
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
127
            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"})
128
        },
129
        u'contenttypes.contenttype': {
130
            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
131
            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
132
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
133
            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
134
            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
135
        }
136
    }
137

  
138
    complete_apps = ['authentic2']
authentic2/models.py
2 2
import re
3 3
import urlparse
4 4

  
5
from django.core.exceptions import ImproperlyConfigured
5
from django.core.exceptions import ImproperlyConfigured, ValidationError
6 6
from django.utils import timezone
7 7
from django.utils.http import urlquote
8 8
from django.conf import settings
......
23 23
from safe_expression import SafeExpression, InvalidExpression
24 24

  
25 25

  
26
from . import managers, plugins, attribute_kinds
26
from . import managers, plugins, attribute_kinds, validators as a2_validators
27 27

  
28 28
plugins.init()
29 29

  
......
391 391
        verbose_name = _('attribute source')
392 392
        verbose_name_plural = _('attribute sources')
393 393

  
394
class ExpressionAttributeSource(AttributeSource):
395
    condition = models.TextField(verbose_name=_('condition expression'),
396
            help_text=_('it must be a valid python expression'), blank=True,
397
            validators=[a2_validators.safe_python_expression])
398
    expression = models.TextField(verbose_name=_('value expression'),
399
            help_text=_('it must be a valid python expression'),
400
            validators=[a2_validators.safe_python_expression])
401

  
402
    def clean(self):
403
        '''Validate expressions'''
404
        if self.condition:
405
            try:
406
                SafeExpression(self.condition).validate(raises=True)
407
            except InvalidExpression, e:
408
                raise ValidationError(_('invalid condition: %s') % e)
409
        try:
410
            SafeExpression(self.expression).validate(raises=True)
411
        except InvalidExpression, e:
412
            raise ValidationError(_('invalid condition: %s') % e)
413

  
414
    def get_definition(self, ctx={}):
415
        dependencies = set()
416
        if self.condition:
417
            dependencies.update(SafeExpression(self.condition).names())
418
        dependencies.update(SafeExpression(self.expression).names())
419

  
420
        definition = {
421
                'name': self.name,
422
                'label': self.description,
423
                'dependencies': dependencies,
424
                'condition': self.condition,
425
                'expression': self.expression,
426
        }
427
        return definition
428

  
429
    class Meta:
430
        verbose_name = _('expression')
431
        verbose_name_plural = _('expressions')
394 432

  
395 433
class ModelWithAttributeSources(models.Model):
396 434
    attribute_sources = models.ManyToManyField(AttributeSource, blank=True)
authentic2/templates/admin/authentic2/functionattributegenerator/change_form.html
1
{% extends "admin/change_form.html" %}
2

  
3
{% block after_related_objects %}
4
{% if original.id %}
5
  <pre>
6
    {{ original.get_definition|pprint }}
7
  </pre>
8
{% endif %}
9
{% endblock %}
0
-