1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-11-25 23:06:55 +01:00

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
This commit is contained in:
Yann 2015-12-02 16:09:48 +01:00
commit 4b4e465bb6
5 changed files with 58 additions and 2 deletions

11
install/README.txt Normal file
View file

@ -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

View file

@ -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]:

View file

@ -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

View file

@ -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)

View file

@ -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]