source: mauRepo/MolesManager/trunk/src/MolesManager/forms/date.py @ 8476

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

Implemented the core cedaObject_to_adapter.
At this moment only MO_ResponsiblePartyInfo and MO_party are fully implemented.
Many unittest have been added to the implementation, however it should still be tested against the database

Added a Sphinx documentation folder

  • Property svn:executable set to *
Line 
1'''
2Created on Nov 1, 2011
3
4@author: mnagni
5'''
6from MolesManager.forms.formsFactory import createFieldInstance
7from django.forms.forms import Form
8from sqlalchemy.orm.properties import ColumnProperty, RelationshipProperty
9import inspect
10from sqlalchemy.orm.util import class_mapper
11from sqlalchemy.orm.collections import InstrumentedList
12
13   
14
15def createForm(obj = None, mapper = None):
16    #mvh = MapperViewHelper()
17    #return mvh.fillMapperView(obj, mapper)
18
19    form = Model2Form(obj, mapper)
20    return type('CoreForm', (CoreForm,), form._createFields()) 
21
22class CoreForm(Form):
23    pass
24   
25def methodsWithDecorator(cls, decoratorName):
26    if type(cls).__name__ == 'type' or type(cls).__name__ == 'instancemetho':
27        return 
28    sourcelines = inspect.getsourcelines(cls)[0]
29    for i,line in enumerate(sourcelines):
30        line = line.strip()
31        if line.split('(')[0].strip() == '@'+decoratorName: # leaving a bit out
32            nextLine = sourcelines[i+1]
33            name = nextLine.split('def')[1].split('(')[0].strip()
34            yield(name)
35
36
37class Model2Form(object):
38    def __init__(self, obj = None, mapper = None):
39        self._coreFields = {}               
40        self._instance = None
41        self._clazz = None
42        self._mapper = mapper
43       
44        if inspect.isclass(obj):
45            self._clazz = object
46        else:
47            self._instance = obj
48            self._clazz = type(obj)
49
50
51    def _createFields(self):
52        if self._mapper is None:
53            self._mapper = class_mapper(self._clazz)
54        if self._mapper is None:
55            raise Exception("Class not mapped %s" % (self._clazz))
56        self._loopOnProperties(self._instance, self._mapper)
57        return self._coreFields
58   
59    def _loopOnProperties(self, instance, mapper):
60        for prop in mapper.iterate_properties:
61            if not prop.key.startswith('_'):
62                print prop.key
63       
64        for clazz_property in mapper.iterate_properties:
65            if clazz_property.key == 'relatedParty':
66                pass
67            try:
68                if isinstance(clazz_property, ColumnProperty):
69                    self._manageColumnProperty(clazz_property, instance)
70                if isinstance(clazz_property, RelationshipProperty):
71                    self._manageRelationshipProperty(clazz_property, instance)
72            except Exception as e:
73                print e
74               
75    def _manageColumnProperty(self, clazz_property, instance):
76        if hasattr(clazz_property, 'columns'):
77            columns = getattr(clazz_property, 'columns')
78            if len(columns) > 0:
79                self._coreFields[clazz_property.key] = createFieldInstance(columns[0].type, clazz_property.key, instance)                   
80 
81    def _manageRelationshipProperty(self, relationship_property, instance):
82        if relationship_property.key == 'relatedParty':
83            print relationship_property.key
84
85        prop = getattr(instance, relationship_property.key)                   
86        isInstrumentList = isinstance(prop, InstrumentedList)
87
88        if (relationship_property.direction is not None):
89            if (relationship_property.direction.name == 'MANYTOONE'):
90                pass
91            elif (relationship_property.direction.name == 'ONETOMANY'):
92                #is a subclass or property multiplicity?
93                if relationship_property.key.startswith('_'):                               
94                    self._loopOnProperties(prop, relationship_property.mapper)
95                else:
96                    if isInstrumentList:
97                        items = self._processInstrumentList(prop)
98                        self._coreFields[relationship_property.key] = items
99                    else:                           
100                        self._coreFields[relationship_property.key] = createForm(prop, relationship_property.mapper)
101   
102    def _processInstrumentList(self, items):
103        ret = []
104       
105        for item in items:
106            ret.append(createForm(item))
107        return ret
108   
109
110
111   
112
113   
114
115'''
116for className in getObjectsNames():
117    fa = CoreFormAdapter(getObject(className))
118    fa = CoreFormAdapter(createObjectInstance(getObject(className)))   
119    print className
120'''       
Note: See TracBrowser for help on using the repository browser.