No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

leobject.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #-*- coding: utf-8 -*-
  2. ## @package leapi.leobject
  3. # @brief Contains abstract class designed to be implemented by LeObject
  4. #
  5. # This package contains abstract classes leapi.leclass.LeClass , leapi.letype.LeType, leapi.leapi._LeObject.
  6. # Those abstract classes are designed to be mother classes of dynamically generated classes ( see leapi.lefactory.LeFactory )
  7. #
  8. # @note LeObject will be generated by leapi.lefactory.LeFactory
  9. import re
  10. import copy
  11. import warnings
  12. import leapi
  13. from leapi.lecrud import _LeCrud, REL_SUP, REL_SUB
  14. from leapi.lefactory import LeFactory
  15. import EditorialModel
  16. from EditorialModel.types import EmType
  17. ## @brief Main class to handle objects defined by the types of an Editorial Model
  18. class _LeObject(_LeCrud):
  19. ## @brief maps em uid with LeType or LeClass keys are uid values are LeObject childs classes
  20. # @todo check if this attribute shouldn't be in _LeCrud
  21. _me_uid = dict()
  22. ## @brief Stores the fields name associated with fieldtype of the fields that are common to every LeObject
  23. _leo_fieldtypes = dict()
  24. ## @brief Stores the names of the fields storing the EM class uid and EM type uid
  25. _me_uid_field_names = (None, None)
  26. ## @return True if the LeObject is partially instanciated
  27. def is_partial(self):
  28. return not hasattr(self, '_classtype')
  29. ## @brief Check if a LeObject is the relation tree Root
  30. # @todo implementation
  31. def is_root(self):
  32. return False
  33. ## @brief Dirty & quick comparison implementation
  34. def __cmp__(self, other):
  35. return 0 if self == other else 1
  36. ## @brief Dirty & quick equality implementation
  37. # @todo check class
  38. def __eq__(self, other):
  39. uid_fname = self.uidname()
  40. if not hasattr(other, uid_fname):
  41. return False
  42. return getattr(self, uid_fname) == getattr(other, uid_fname)
  43. ## @brief Quick str cast method implementation
  44. def __str__(self):
  45. return "<%s lodel_id=%d>"%(self.__class__, getattr(self, self.uidname()))
  46. def __repr__(self):
  47. return self.__str__()
  48. ## @brief Returns the name of the uid field
  49. @classmethod
  50. def uidname(cls):
  51. return EditorialModel.classtypes.object_uid
  52. ## @brief Given a ME uid return the corresponding LeClass or LeType class
  53. # @return a LeType or LeClass child class
  54. # @throw KeyError if no corresponding child classes
  55. # @todo check if this method shouldn't be in _LeCrud
  56. @classmethod
  57. def uid2leobj(cls, uid):
  58. uid = int(uid)
  59. if uid not in cls._me_uid:
  60. raise KeyError("No LeType or LeClass child classes with uid '%d'"%uid)
  61. return cls._me_uid[uid]
  62. ## @brief instanciate the relevant lodel object using a dict of datas
  63. @classmethod
  64. def object_from_data(cls, datas):
  65. return cls.uid2leobj(datas['type_id'])(**datas)
  66. @classmethod
  67. def fieldtypes(cls):
  68. if cls._fieldtypes_all is None:
  69. cls._fieldtypes_all = dict()
  70. cls._fieldtypes_all.update(cls._uid_fieldtype)
  71. cls._fieldtypes_all.update(cls._leo_fieldtypes)
  72. return cls._fieldtypes_all
  73. @classmethod
  74. def typefilter(cls):
  75. if hasattr(cls, '_type_id'):
  76. return ('type_id','=', cls._type_id)
  77. elif hasattr(cls, '_class_id'):
  78. return ('class_id', '=', cls._class_id)
  79. else:
  80. raise ValueError("Cannot generate a typefilter with %s class"%cls.__name__)
  81. ## @brief Check that a relational field is valid
  82. # @param field str : a relational field
  83. # @return a nature
  84. @staticmethod
  85. def _prepare_relational_fields(field):
  86. spl = field.split('.')
  87. if len(spl) != 2:
  88. return ValueError("The relationalfield '%s' is not valid"%field)
  89. nature = spl[-1]
  90. if nature not in EditorialModel.classtypes.EmNature.getall():
  91. return ValueError("'%s' is not a valid nature in the field %s"%(nature, field))
  92. if spl[0] == 'superior':
  93. return (REL_SUP, nature)
  94. elif spl[0] == 'subordinate':
  95. return (REL_SUB, nature)
  96. else:
  97. return ValueError("Invalid preffix for relationnal field : '%s'"%spl[0])
  98. ## @brief Class designed to represent the hierarchy roots
  99. # @see _LeObject.get_root() _LeObject.is_root()
  100. class LeRoot(object):
  101. pass
  102. class LeObjectError(Exception):
  103. pass
  104. class LeObjectQueryError(LeObjectError):
  105. pass