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

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

updated the _fieldFactory dictionary

  • 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       'TEXT': CharField,
16       'Integer': IntegerField,
17       'NUMERIC': DecimalField,
18     }   
19
20def createASEnumField(clazz, **kwargs):
21    sel = []
22    for attrName in clazz.enum.__dict__:
23        if (attrName.startswith('_')):
24            continue
25        attr = getattr(clazz.enum, attrName)
26        if (isinstance(attr, EnumSymbol)):
27            sel.append((attr.name, attr.value))
28    return models.CharField(choices=tuple(sel))
29
30
31def getTypeName(clazz):   
32    return type(clazz).__name__
33
34def createField(name):
35    return _fieldFactory.get(name)
36
37def createFieldInstance(clazz, **kwargs):
38    name = getTypeName(clazz)
39    if (name == 'ASEnumType'):
40        return createASEnumField(clazz)
41    elif (name == 'ARRAY'):
42        return formset_factory(createField(getTypeName(clazz.item_type)))   
43    elif _fieldFactory.has_key(name):
44        return createField(name)(**kwargs)
Note: See TracBrowser for help on using the repository browser.