source: mauRepo/MolesManager/trunk/src/MolesManager/forms/formsFactory.py @ 7976

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/forms/formsFactory.py@7976
Revision 7976, 1.1 KB checked in by mnagni, 8 years ago (diff)

removed all the EA LIbrary until they are in stable status

  • Property svn:executable set to *
Line 
1'''
2Created on Nov 3, 2011
3
4@author: mnagni
5'''
6from django.db import models
7from django.forms.fields import CharField, DecimalField, IntegerField
8from ascore.asenumeration import EnumSymbol
9from django.forms.formsets import formset_factory
10
11
12
13_fieldFactory = { 
14       'TEXT': CharField,
15       'Integer': IntegerField,
16       'NUMERIC': DecimalField,
17     }   
18
19def createASEnumField(clazz, **kwargs):
20    sel = []
21    for attrName in clazz.enum.__dict__:
22        if (attrName.startswith('_')):
23            continue
24        attr = getattr(clazz.enum, attrName)
25        if (isinstance(attr, EnumSymbol)):
26            sel.append((attr.name, attr.value))
27    return models.CharField(choices=tuple(sel))
28
29
30def getTypeName(clazz):   
31    return type(clazz).__name__
32
33def createField(name):
34    return _fieldFactory.get(name)
35
36def createFieldInstance(clazz, **kwargs):
37    name = getTypeName(clazz)
38    if (name == 'ASEnumType'):
39        return createASEnumField(clazz)
40    elif (name == 'ARRAY'):
41        return formset_factory(createField(getTypeName(clazz.item_type)))   
42    elif _fieldFactory.has_key(name):
43        return createField(name)(**kwargs)
Note: See TracBrowser for help on using the repository browser.