Bladeren bron

Implements partial instanciation of LeObjects + utility classmethod for LeRelation

Yann Weber 9 jaren geleden
bovenliggende
commit
1cd5998a88
3 gewijzigde bestanden met toevoegingen van 45 en 11 verwijderingen
  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 Bestand weergeven

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

+ 29
- 4
leapi/leobject.py Bestand weergeven

@@ -32,11 +32,36 @@ class _LeObject(_LeCrud):
32 32
     
33 33
     ## @brief Stores the fields name associated with fieldtype of the fields that are common to every LeObject
34 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 66
     ## @brief Given a ME uid return the corresponding LeClass or LeType class
42 67
     # @return a LeType or LeClass child class

+ 12
- 0
leapi/lerelation.py Bestand weergeven

@@ -20,6 +20,17 @@ class _LeRelation(lecrud._LeCrud):
20 20
     def __init__(self, rel_id, **kwargs):
21 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 34
     @classmethod
24 35
     def fieldtypes(cls):
25 36
         rel_ft = dict()
@@ -33,6 +44,7 @@ class _LeRelation(lecrud._LeCrud):
33 44
     def _prepare_relational_fields(cls, field):
34 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 50
 ## @brief Abstract class to handle hierarchy relations

Loading…
Annuleren
Opslaan