From 798ad5a68e773c7e30fb7e63283954def69aed8c Mon Sep 17 00:00:00 2001 From: Yann Date: Thu, 26 Nov 2015 17:29:21 +0100 Subject: [PATCH] Added 2 fieldtypes : leo and naturerelation + add a uidname method to _LeCrud --- EditorialModel/fieldtypes/leo.py | 34 +++++++++++++++++++++ EditorialModel/fieldtypes/naturerelation.py | 16 ++++++++++ leapi/lecrud.py | 12 +++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 EditorialModel/fieldtypes/leo.py create mode 100644 EditorialModel/fieldtypes/naturerelation.py diff --git a/EditorialModel/fieldtypes/leo.py b/EditorialModel/fieldtypes/leo.py new file mode 100644 index 0000000..bc62df1 --- /dev/null +++ b/EditorialModel/fieldtypes/leo.py @@ -0,0 +1,34 @@ +#-*- coding: utf-8 -*- + +from .generic import GenericFieldType + +class EmFieldType(GenericFieldType): + + help = 'Fieldtypes designed to handle pk of LeObject in LeRelations' + + ftype = 'leobject' + + def __init__(self, superior=True, **kwargs): + super(EmFieldType, self).__init__(ftype = 'leobject', **kwargs) + + def _check_data_value(self, value): + import leapi.lecrud as lecrud + err = None + if not isinstance(value, int): + if not isinstance(value, lecrud._LeCrud.name2class('LeType')): + return (None, ValueError("An instance of a child class of LeType was expected")) + if not hasattr(value, 'lodel_id'): + return (None, ValueError("The LeType instance given has no lodel_id !")) + return (value, None) + + def construct_data(self, lec, fname, datas): + if isinstance(datas[fname], int): + leobject = lecrud._LeCrud.name2class('LeObject') + qfilter = '{uid_name} = {uid}'.format( + uid_name = leobject.uidname(), + uid = datas[fname], + ) + + return leobject.get([qfilter]) + else: + return datas[fname] diff --git a/EditorialModel/fieldtypes/naturerelation.py b/EditorialModel/fieldtypes/naturerelation.py new file mode 100644 index 0000000..d0a271c --- /dev/null +++ b/EditorialModel/fieldtypes/naturerelation.py @@ -0,0 +1,16 @@ +#-*- coding: utf-8 -*- + +from .char import EmFieldType + +import EditorialModel.classtypes as classtypes + +class EmFieldType(EmFieldType): + + help = 'Stores a relation\'s nature' + + def __init__(self, **kwargs): + kwargs.update({'nullable': True, 'check_data_value': EmFieldType.check_data_value}) + super(EmFieldType, self).__init__(**kwargs) + + def check_data_value(self, value): + return value is None or ( value in classtypes.getall()) diff --git a/leapi/lecrud.py b/leapi/lecrud.py index a635b22..0ed0691 100644 --- a/leapi/lecrud.py +++ b/leapi/lecrud.py @@ -70,6 +70,7 @@ class _LeCrud(object): raise NotImplementedError("Abstract method") #child classes should return their uid fieldtype ## @return A dict with fieldtypes marked as internal + # @todo check if this method is in use, else delete it @classmethod def fieldtypes_internal(self): return { fname: ft for fname, ft in cls.fieldtypes().items() if hasattr(ft, 'internal') and ft.internal } @@ -79,6 +80,15 @@ class _LeCrud(object): def fieldlist(cls): return cls.fieldtypes().keys() + ## @return The name of the uniq id field + # @todo test for abstract method !!! + @classmethod + def uidname(cls): + if len(cls._uid_fieldtype) == 0: + raise NotImplementedError("Abstract method uid_name for %s!"%cls.__name__) + return list(cls._uid_fieldtype.keys())[0] + + ## @brief Returns object datas # @param # @return a dict of fieldname : value @@ -222,7 +232,7 @@ class _LeCrud(object): # @warning assert that the uid is not composed with multiple fieldtypes # @return A filter of the form tuple(UID, '=', self.UID) def _id_filter(self): - id_name = list(self._uid_fieldtype.keys())[0] + id_name = self.uidname() return ( id_name, '=', getattr(self, id_name) ) ## @brief Construct datas values