Переглянути джерело

Implements utils function in LeType add_superior (hierarchy) and link_with (rel2type) and add a forgotten file

Implements a utils method in _LeRel2Type to fetch a rel2type leapi class name given a superior and a subordinate
Yann Weber 8 роки тому
джерело
коміт
4b4e465bb6
5 змінених файлів з 58 додано та 2 видалено
  1. 11
    0
      install/README.txt
  2. 1
    1
      leapi/lefactory.py
  3. 11
    0
      leapi/lerelation.py
  4. 34
    0
      leapi/letype.py
  5. 1
    1
      refreshdyn.py

+ 11
- 0
install/README.txt Переглянути файл

@@ -0,0 +1,11 @@
1
+Common operations :
2
+===================
3
+
4
+Refresh leapi dynamic code (when the Editorial Model is updated) :
5
+	make refreshdyn
6
+
7
+Update or init the database :
8
+	make dbinit
9
+
10
+To run an interactive python interpreter in the instance environnment run :
11
+	python loader.py

+ 1
- 1
leapi/lefactory.py Переглянути файл

@@ -83,7 +83,7 @@ class LeFactory(object):
83 83
         for field in [ f for f in model.components('EmField') if f.fieldtype == 'rel2type']:
84 84
             related = model.component(field.rel_to_type_id)
85 85
             src = field.em_class
86
-            cls_name = "Rel_%s2%s"%(src.name, related.name)
86
+            cls_name = "Rel_%s2%s"%(self.name2classname(src.name), self.name2classname(related.name))
87 87
 
88 88
             attr_l = dict()
89 89
             for attr in [ f for f in model.components('EmField') if f.rel_field_id == field.uid]:

+ 11
- 0
leapi/lerelation.py Переглянути файл

@@ -100,5 +100,16 @@ class _LeHierarch(_LeRelation):
100 100
 class _LeRel2Type(_LeRelation):
101 101
     ## @brief Stores the list of fieldtypes handling relations attributes
102 102
     _rel_attr_fieldtypes = dict()
103
+    
104
+    ## @brief Given a superior and a subordinate, returns the classname of the give rel2type
105
+    # @param lesupclass LeClass : LeClass child class (can be a LeType or a LeClass child)
106
+    # @param lesubclass LeType : A LeType child class
107
+    # @return a name as string
108
+    @staticmethod
109
+    def relname(lesupclass, lesubclass):
110
+        supname = lesupclass._leclass.__name__ if lesupclass.implements_letype() else lesupclass.__name__
111
+        subname = lesubclass.__name__
112
+
113
+        return "Rel_%s2%s" % (supname, subname)
103 114
     pass
104 115
     

+ 34
- 0
leapi/letype.py Переглянути файл

@@ -95,6 +95,40 @@ class _LeType(_LeClass):
95 95
     ## @brief Delete current instance from DB
96 96
     def delete(self):
97 97
         _LeCrud._delete(self)
98
+    
99
+    ## @brief Add a superior
100
+    # @param lesup LeObject : LeObject child class instance
101
+    # @param nature str : Relation nature
102
+    # @param del_if_exists bool : If true delete the superior if any before setting the new one
103
+    # @return relation id if successfully created else returns false
104
+    def add_superior(self, lesup, nature, del_if_exists = False):
105
+        lehierarch = self.name2class('LeHierarch')
106
+        if del_if_exists:
107
+            prev_sup = lehierarch.get(
108
+                [('lesub', '=', self), ('nature', '=', nature)],
109
+                [ lehierarch.uidname() ]
110
+            )
111
+            if len(prev_sup) > 0:
112
+                for todel_sup in prev_sup: #This loop shoud be useless...but we never know
113
+                    todel_sup.delete()
114
+
115
+        return lehierarch.insert(lesup = lesup, lesub = self, nature = nature)
116
+    
117
+    ## @brief Link the LeObject with another one (rel2type relations)
118
+    #
119
+    # @note This methods asser that self is the superior and leo_tolink the subordinate
120
+    #
121
+    # @param leo_tolink LeObject : LeObject child instance to link with
122
+    # @param **datas : Relation attributes (if any)
123
+    # @return a relation id if success
124
+    def link_with(self, leo_tolink, **datas):
125
+        # Fetch rel2type leapi class
126
+        r2t = self.name2class('LeRel2Type')
127
+        r2tcls = self.name2class(r2t.relname(self, leo_tolink))
128
+        if not r2tcls:
129
+            raise ValueError("No rel2type possible between a '%s' as superior and a '%s' as subordinate" % (self._leclass.__name__, leo_tolink.__class__.__name__))
130
+        return r2tcls.insert(lesup = self, lesub = leo_tolink, **datas)
131
+        
98 132
 
99 133
     ## @brief Get the linked objects lodel_id
100 134
     # @param letype LeType : Filter the result with LeType child class (not instance) 

+ 1
- 1
refreshdyn.py Переглянути файл

@@ -4,7 +4,7 @@ import sys
4 4
 from EditorialModel.model import Model
5 5
 from leapi.lefactory import LeFactory
6 6
 from EditorialModel.backend.json_backend import EmBackendJson
7
-from leapi.datasources.ledatasourcesql import LeDataSourceSQL
7
+from DataSource.MySQL.leapidatasource import LeDataSourceSQL
8 8
 
9 9
 OUTPUT = 'leapi/dyn.py' if len(sys.argv) == 1 else sys.argv[1]
10 10
 EMJSON = 'EditorialModel/test/me.json' if len(sys.argv) < 3 else sys.argv[2]

Loading…
Відмінити
Зберегти