From 4b4e465bb6a3b49db990dc5c243e117303ac8d19 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 2 Dec 2015 16:09:48 +0100 Subject: [PATCH] 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 --- install/README.txt | 11 +++++++++++ leapi/lefactory.py | 2 +- leapi/lerelation.py | 11 +++++++++++ leapi/letype.py | 34 ++++++++++++++++++++++++++++++++++ refreshdyn.py | 2 +- 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 install/README.txt diff --git a/install/README.txt b/install/README.txt new file mode 100644 index 0000000..2c7ad7d --- /dev/null +++ b/install/README.txt @@ -0,0 +1,11 @@ +Common operations : +=================== + +Refresh leapi dynamic code (when the Editorial Model is updated) : + make refreshdyn + +Update or init the database : + make dbinit + +To run an interactive python interpreter in the instance environnment run : + python loader.py diff --git a/leapi/lefactory.py b/leapi/lefactory.py index a7dbf86..1352fd8 100644 --- a/leapi/lefactory.py +++ b/leapi/lefactory.py @@ -83,7 +83,7 @@ class LeFactory(object): for field in [ f for f in model.components('EmField') if f.fieldtype == 'rel2type']: related = model.component(field.rel_to_type_id) src = field.em_class - cls_name = "Rel_%s2%s"%(src.name, related.name) + cls_name = "Rel_%s2%s"%(self.name2classname(src.name), self.name2classname(related.name)) attr_l = dict() for attr in [ f for f in model.components('EmField') if f.rel_field_id == field.uid]: diff --git a/leapi/lerelation.py b/leapi/lerelation.py index e75fcb6..f96ee5f 100644 --- a/leapi/lerelation.py +++ b/leapi/lerelation.py @@ -100,5 +100,16 @@ class _LeHierarch(_LeRelation): class _LeRel2Type(_LeRelation): ## @brief Stores the list of fieldtypes handling relations attributes _rel_attr_fieldtypes = dict() + + ## @brief Given a superior and a subordinate, returns the classname of the give rel2type + # @param lesupclass LeClass : LeClass child class (can be a LeType or a LeClass child) + # @param lesubclass LeType : A LeType child class + # @return a name as string + @staticmethod + def relname(lesupclass, lesubclass): + supname = lesupclass._leclass.__name__ if lesupclass.implements_letype() else lesupclass.__name__ + subname = lesubclass.__name__ + + return "Rel_%s2%s" % (supname, subname) pass diff --git a/leapi/letype.py b/leapi/letype.py index b910dab..3304781 100644 --- a/leapi/letype.py +++ b/leapi/letype.py @@ -95,6 +95,40 @@ class _LeType(_LeClass): ## @brief Delete current instance from DB def delete(self): _LeCrud._delete(self) + + ## @brief Add a superior + # @param lesup LeObject : LeObject child class instance + # @param nature str : Relation nature + # @param del_if_exists bool : If true delete the superior if any before setting the new one + # @return relation id if successfully created else returns false + def add_superior(self, lesup, nature, del_if_exists = False): + lehierarch = self.name2class('LeHierarch') + if del_if_exists: + prev_sup = lehierarch.get( + [('lesub', '=', self), ('nature', '=', nature)], + [ lehierarch.uidname() ] + ) + if len(prev_sup) > 0: + for todel_sup in prev_sup: #This loop shoud be useless...but we never know + todel_sup.delete() + + return lehierarch.insert(lesup = lesup, lesub = self, nature = nature) + + ## @brief Link the LeObject with another one (rel2type relations) + # + # @note This methods asser that self is the superior and leo_tolink the subordinate + # + # @param leo_tolink LeObject : LeObject child instance to link with + # @param **datas : Relation attributes (if any) + # @return a relation id if success + def link_with(self, leo_tolink, **datas): + # Fetch rel2type leapi class + r2t = self.name2class('LeRel2Type') + r2tcls = self.name2class(r2t.relname(self, leo_tolink)) + if not r2tcls: + raise ValueError("No rel2type possible between a '%s' as superior and a '%s' as subordinate" % (self._leclass.__name__, leo_tolink.__class__.__name__)) + return r2tcls.insert(lesup = self, lesub = leo_tolink, **datas) + ## @brief Get the linked objects lodel_id # @param letype LeType : Filter the result with LeType child class (not instance) diff --git a/refreshdyn.py b/refreshdyn.py index 4f25040..0b12071 100755 --- a/refreshdyn.py +++ b/refreshdyn.py @@ -4,7 +4,7 @@ import sys from EditorialModel.model import Model from leapi.lefactory import LeFactory from EditorialModel.backend.json_backend import EmBackendJson -from leapi.datasources.ledatasourcesql import LeDataSourceSQL +from DataSource.MySQL.leapidatasource import LeDataSourceSQL OUTPUT = 'leapi/dyn.py' if len(sys.argv) == 1 else sys.argv[1] EMJSON = 'EditorialModel/test/me.json' if len(sys.argv) < 3 else sys.argv[2]