暫無描述
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.8KB

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