mirror of
https://github.com/yweber/lodel2.git
synced 2026-05-21 04:36:05 +02:00
Add relation modification methods to LeType
This commit is contained in:
parent
7f3981f24a
commit
0d88ebca62
4 changed files with 80 additions and 16 deletions
|
|
@ -51,3 +51,26 @@ class DummyDatasource(object):
|
|||
def get(self, leclass, letype, field_list, filters, relational_filters):
|
||||
print("DummyDatasource.get: ", leclass, letype, field_list, filters, relational_filters)
|
||||
return []
|
||||
|
||||
## @brief Link two object given a relation nature, depth and rank
|
||||
# @param id_sup int : a lodel_id
|
||||
# @param id_sub int : a lodel_id
|
||||
# @param nature str|None : The relation nature or None if rel2type
|
||||
# @param rank int : a rank
|
||||
def add_relation(self, id_sup, id_sub, nature=None, depth=None, rank=None):
|
||||
pass
|
||||
|
||||
## @brief Delete a link between two objects given a relation nature
|
||||
# @param id_sup int : a lodel_id
|
||||
# @param id_sub int : a lodel_id
|
||||
# @param nature str|None : The relation nature
|
||||
def del_relation(self, id_sup, id_sub, nature=None):
|
||||
pass
|
||||
|
||||
## @brief Return all relation of a lodel_id given a position and a nature
|
||||
# @param lodel_id int : We want the relations of this lodel_id
|
||||
# @param superior bool : If true search the relations where lodel_id is in id_sup
|
||||
# @param nature str|None : Search for relations with the given nature (if None rel2type)
|
||||
# @param return an array of dict with keys [ id_sup, id_sub, rank, depth, nature ]
|
||||
def get_relations(self, lodel_id, superior=True, nature=None):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#-*- coding: utf-8 -*-
|
||||
|
||||
#import leobject
|
||||
import leobject
|
||||
|
||||
|
||||
|
||||
## @brief Represent an EmClass data instance
|
||||
|
|
@ -15,6 +16,8 @@ class LeClass(object):
|
|||
_fieldgroups = dict()
|
||||
## @brief Stores the EM uid
|
||||
_class_id = None
|
||||
## @brief Stores the classtype
|
||||
_classtype = None
|
||||
|
||||
## @brief Instanciate a new LeClass
|
||||
# @note Abstract method
|
||||
|
|
@ -22,14 +25,3 @@ class LeClass(object):
|
|||
def __init__(self, **kwargs):
|
||||
raise NotImplementedError("Abstract class")
|
||||
|
||||
## @brief Get the linked objects
|
||||
# @return an array of LeType derivated class instance
|
||||
def linked(self):
|
||||
pass
|
||||
|
||||
## @brief Link this class with an LeObject
|
||||
# @param leo LeObject : The object to be linked with
|
||||
# @return True if success False allready done
|
||||
# @throw A Leo exception if the link is not allowed
|
||||
def link_to(self, leo):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -86,11 +86,13 @@ class LeFactory(object):
|
|||
{name}._fieldtypes = {ftypes}
|
||||
{name}._linked_types = {ltypes}
|
||||
{name}._fieldgroups = {fgroups}
|
||||
{name}._classtype = {classtype}
|
||||
""".format(
|
||||
name=LeFactory.name2classname(emclass.name),
|
||||
ftypes="{" + (','.join(['\n\t%s:%s' % (repr(f), v) for f, v in cls_fields.items()])) + "\n}",
|
||||
ltypes="{" + (','.join(cls_linked_types)) + '}',
|
||||
fgroups=repr(cls_fieldgroup)
|
||||
name = LeFactory.name2classname(emclass.name),
|
||||
ftypes = "{" + (','.join(['\n\t%s:%s' % (repr(f), v) for f, v in cls_fields.items()])) + "\n}",
|
||||
ltypes = "{" + (','.join(cls_linked_types)) + '}',
|
||||
fgroups = repr(cls_fieldgroup),
|
||||
classtype = repr(emclass.classtype)
|
||||
)
|
||||
|
||||
## @brief Given a Model and an EmType instances generate python code for corresponding LeType
|
||||
|
|
|
|||
|
|
@ -78,6 +78,53 @@ class LeType(object):
|
|||
def db_delete(self):
|
||||
return self.delete([('lodel_id','=',repr(self.lodel_id))])
|
||||
|
||||
## @brief Add a superior given a nature
|
||||
# @param leo LeObject : A LeObject instance that will be the superior
|
||||
# @param nature str : the relation nature
|
||||
# @throw leobject.leobject.LeObjectError if this
|
||||
# @todo find what value depth and rank should have....
|
||||
# @todo Unit tests
|
||||
def add_superior(self, leo, nature):
|
||||
if nature not in self._superiors.keys():
|
||||
raise LeObjectError("%s cannot have a superior with %s as relation nature"%(self.__class__.__name__, nature))
|
||||
if leo.__class__ not in self._superiors[nature]:
|
||||
raise LeObjectError("%s cannot have a %s superior in a %s relation"%(self.__class__.__name__, leo.__class__.__name__, nature))
|
||||
|
||||
return self._datasource.add_relation(leo.lodel_id, self.lodel_id, nature = nature, depth=None, rank = None)
|
||||
|
||||
## @brief Delete a superior given a nature
|
||||
# @param leo LeObject : A LeObject instance
|
||||
# @param nature str : The relation nature
|
||||
# @todo Unit tests
|
||||
def del_superior(self, leo, nature):
|
||||
if nature is None:
|
||||
raise ValueError('The argument nature cannot be None')
|
||||
return self._datasource(leo.lodel_id, self.lodel_id, nature)
|
||||
|
||||
## @brief Get the linked objects lodel_id
|
||||
# @return an array of lodel_id linked with this object
|
||||
# @todo unit tests
|
||||
def linked(self):
|
||||
return [ rel['id_sub'] for rel in self._datasource.get_relations(self.lodel_id) ]
|
||||
|
||||
## @brief Link this object with a LeObject
|
||||
# @note rel2type
|
||||
# @param leo LeObject : The object to be linked with
|
||||
# @return True if success False allready done
|
||||
# @throw A Leo exception if the link is not allowed
|
||||
# @todo unit tests
|
||||
# @todo find a value for depth and rank....
|
||||
def link_to(self, leo):
|
||||
if leo.__class__ not in self._linked_types:
|
||||
raise leobject.leobject.LeObjectError("Constraint error : %s cannot be linked with %s"%(self.__class__.__name__, leo.__class__.__name__))
|
||||
return self._datasource.add_relation(self.lodel_id, leo.lodel_id)
|
||||
|
||||
## @brief Remove a link bewteen this object and another
|
||||
# @param leo LeObject : A LeObject instance linked with self
|
||||
# @todo unit tests
|
||||
def unlink(self, leo):
|
||||
return self._datasource.del_relation(self.lodel_id, leo.lodel_id)
|
||||
|
||||
## @brief Delete a LeType from the datasource
|
||||
# @param filters list : list of filters (see @ref leobject_filters)
|
||||
# @param cls
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue