Browse Source

Added 2 fieldtypes : leo and naturerelation + add a uidname method to _LeCrud

Yann Weber 9 years ago
parent
commit
798ad5a68e
3 changed files with 61 additions and 1 deletions
  1. 34
    0
      EditorialModel/fieldtypes/leo.py
  2. 16
    0
      EditorialModel/fieldtypes/naturerelation.py
  3. 11
    1
      leapi/lecrud.py

+ 34
- 0
EditorialModel/fieldtypes/leo.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from .generic import GenericFieldType
4
+
5
+class EmFieldType(GenericFieldType):
6
+    
7
+    help = 'Fieldtypes designed to handle pk of LeObject in LeRelations'
8
+
9
+    ftype = 'leobject'
10
+
11
+    def __init__(self, superior=True, **kwargs):
12
+        super(EmFieldType, self).__init__(ftype = 'leobject', **kwargs)
13
+
14
+    def _check_data_value(self, value):
15
+        import leapi.lecrud as lecrud
16
+        err = None
17
+        if not isinstance(value, int):
18
+            if not isinstance(value, lecrud._LeCrud.name2class('LeType')):
19
+                return (None, ValueError("An instance of a child class of LeType was expected"))
20
+            if not hasattr(value, 'lodel_id'):
21
+                return (None, ValueError("The LeType instance given has no lodel_id !"))
22
+        return (value, None)
23
+
24
+    def construct_data(self, lec, fname, datas):
25
+        if isinstance(datas[fname], int):
26
+            leobject = lecrud._LeCrud.name2class('LeObject')
27
+            qfilter = '{uid_name} = {uid}'.format(
28
+                uid_name = leobject.uidname(),
29
+                uid = datas[fname],
30
+            )
31
+
32
+            return leobject.get([qfilter])
33
+        else:
34
+            return datas[fname]

+ 16
- 0
EditorialModel/fieldtypes/naturerelation.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from .char import EmFieldType
4
+
5
+import EditorialModel.classtypes as classtypes
6
+
7
+class EmFieldType(EmFieldType):
8
+    
9
+    help = 'Stores a relation\'s nature'
10
+
11
+    def __init__(self, **kwargs):
12
+        kwargs.update({'nullable': True, 'check_data_value': EmFieldType.check_data_value})
13
+        super(EmFieldType, self).__init__(**kwargs)
14
+
15
+    def check_data_value(self, value):
16
+        return value is None or ( value in classtypes.getall())

+ 11
- 1
leapi/lecrud.py View File

70
         raise NotImplementedError("Abstract method") #child classes should return their uid fieldtype
70
         raise NotImplementedError("Abstract method") #child classes should return their uid fieldtype
71
     
71
     
72
     ## @return A dict with fieldtypes marked as internal
72
     ## @return A dict with fieldtypes marked as internal
73
+    # @todo check if this method is in use, else delete it
73
     @classmethod
74
     @classmethod
74
     def fieldtypes_internal(self):
75
     def fieldtypes_internal(self):
75
         return { fname: ft for fname, ft in cls.fieldtypes().items() if hasattr(ft, 'internal') and ft.internal }
76
         return { fname: ft for fname, ft in cls.fieldtypes().items() if hasattr(ft, 'internal') and ft.internal }
79
     def fieldlist(cls):
80
     def fieldlist(cls):
80
         return cls.fieldtypes().keys()
81
         return cls.fieldtypes().keys()
81
     
82
     
83
+    ## @return The name of the uniq id field
84
+    # @todo test for abstract method !!!
85
+    @classmethod
86
+    def uidname(cls):
87
+        if len(cls._uid_fieldtype) == 0:
88
+            raise NotImplementedError("Abstract method uid_name for %s!"%cls.__name__)
89
+        return list(cls._uid_fieldtype.keys())[0]
90
+        
91
+    
82
     ## @brief Returns object datas
92
     ## @brief Returns object datas
83
     # @param
93
     # @param
84
     # @return a dict of fieldname : value
94
     # @return a dict of fieldname : value
222
     # @warning assert that the uid is not composed with multiple fieldtypes
232
     # @warning assert that the uid is not composed with multiple fieldtypes
223
     # @return A filter of the form tuple(UID, '=', self.UID)
233
     # @return A filter of the form tuple(UID, '=', self.UID)
224
     def _id_filter(self):
234
     def _id_filter(self):
225
-        id_name = list(self._uid_fieldtype.keys())[0]
235
+        id_name = self.uidname()
226
         return ( id_name, '=', getattr(self, id_name) )
236
         return ( id_name, '=', getattr(self, id_name) )
227
 
237
 
228
     ## @brief Construct datas values
238
     ## @brief Construct datas values

Loading…
Cancel
Save