Browse Source

Implements partial instanciation of LeObjects + utility classmethod for LeRelation

Yann Weber 8 years ago
parent
commit
1cd5998a88
3 changed files with 45 additions and 11 deletions
  1. 4
    7
      EditorialModel/fieldtypes/leo.py
  2. 29
    4
      leapi/leobject.py
  3. 12
    0
      leapi/lerelation.py

+ 4
- 7
EditorialModel/fieldtypes/leo.py View File

20
             if not hasattr(value, 'lodel_id'):
20
             if not hasattr(value, 'lodel_id'):
21
                 return (None, ValueError("The LeType instance given has no lodel_id !"))
21
                 return (None, ValueError("The LeType instance given has no lodel_id !"))
22
         return (value, None)
22
         return (value, None)
23
-
23
+    
24
+    ## @brief If field value is an integer, returns a partially instanciated LeObject (only with an ID)
24
     def construct_data(self, lec, fname, datas):
25
     def construct_data(self, lec, fname, datas):
26
+        import leapi.lecrud as lecrud
25
         if isinstance(datas[fname], int):
27
         if isinstance(datas[fname], int):
26
             leobject = lecrud._LeCrud.name2class('LeObject')
28
             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])
29
+            return leobject.get(datas[fname])
33
         else:
30
         else:
34
             return datas[fname]
31
             return datas[fname]
35
     
32
     

+ 29
- 4
leapi/leobject.py View File

32
     
32
     
33
     ## @brief Stores the fields name associated with fieldtype of the fields that are common to every LeObject
33
     ## @brief Stores the fields name associated with fieldtype of the fields that are common to every LeObject
34
     _leo_fieldtypes = dict()
34
     _leo_fieldtypes = dict()
35
+    
36
+    ## @brief Instanciate a partial LeObject with a lodel_id
37
+    # @note use the get_instance method to fetch datas and instanciate a concret LeObject
38
+    def __init__(self, lodel_id):
39
+        #Warning ! Handles only single pk
40
+        uid_fname, uid_ft = list(self._uid_fieldtype.items())[0]
41
+        new_id, err = uid_ft.check_data_value(lodel_id)
42
+        if not (err is None):
43
+            raise err
44
+        setattr(self, uid_fname, lodel_id)
45
+    
46
+    ## @return Corresponding populated LeObject
47
+    def get_instance(self):
48
+        uid_fname = self.uidname()
49
+        qfilter = '{uid_fname} = {uid}'.format(uid_fname = uid_fname, uid = getattr(self, uid_fname))
50
+        return leobject.get([qfilter])[0]
51
+    
52
+    ## @brief Dirty & quick comparison implementation
53
+    def __cmp__(self, other):
54
+        uid_fname = self.uidname()
55
+        if not hasattr(other, uid_fname):
56
+            return False
57
+        return getattr(self, uid_fname) == getattr(other, uid_fname)
58
+        
59
+    ## @brief Quick str cast method implementation
60
+    def __str__(self):
61
+        return "<%s lodel_id=%d>"%(self.__class__, getattr(self, self.uidname()))
35
 
62
 
36
-    ## @brief Instantiate with a Model and a DataSource
37
-    # @param **kwargs dict : datas usefull to instanciate a _LeObject
38
-    def __init__(self, **kwargs):
39
-        raise NotImplementedError("Abstract constructor")
63
+    def __repr__(self):
64
+        return self.__str__()
40
     
65
     
41
     ## @brief Given a ME uid return the corresponding LeClass or LeType class
66
     ## @brief Given a ME uid return the corresponding LeClass or LeType class
42
     # @return a LeType or LeClass child class
67
     # @return a LeType or LeClass child class

+ 12
- 0
leapi/lerelation.py View File

20
     def __init__(self, rel_id, **kwargs):
20
     def __init__(self, rel_id, **kwargs):
21
        pass 
21
        pass 
22
  
22
  
23
+    @classmethod
24
+    def sup_filter(self, leo):
25
+        if isinstance(leo, _LeObject):
26
+            return ('lesup', '=', leo)
27
+
28
+    @classmethod
29
+    def sub_filter(self, leo):
30
+        if isinstance(leo, _LeObject):
31
+            return ('lesub', '=', leo)
32
+
33
+
23
     @classmethod
34
     @classmethod
24
     def fieldtypes(cls):
35
     def fieldtypes(cls):
25
         rel_ft = dict()
36
         rel_ft = dict()
33
     def _prepare_relational_fields(cls, field):
44
     def _prepare_relational_fields(cls, field):
34
         return lecrud.LeApiQueryError("Relational field '%s' given but %s doesn't is not a LeObject"%(field,cls.__name__))
45
         return lecrud.LeApiQueryError("Relational field '%s' given but %s doesn't is not a LeObject"%(field,cls.__name__))
35
 
46
 
47
+
36
             
48
             
37
 
49
 
38
 ## @brief Abstract class to handle hierarchy relations
50
 ## @brief Abstract class to handle hierarchy relations

Loading…
Cancel
Save