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.

classtypes.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # -*- coding: utf-8 -*-
  2. common_fields = {
  3. 'lodel_id': {
  4. 'fieldtype': 'pk',
  5. 'internal': 'automatic',
  6. },
  7. 'class_id': {
  8. 'fieldtype': 'integer',
  9. 'internal': 'automatic',
  10. },
  11. 'type_id': {
  12. 'fieldtype': 'integer',
  13. 'internal': 'automatic',
  14. },
  15. 'string': {
  16. 'fieldtype': 'char',
  17. 'max_length': 128,
  18. 'internal': 'automatic',
  19. },
  20. 'creation_date': {
  21. 'fieldtype': 'datetime',
  22. 'now_on_create': True,
  23. 'internal': 'automatic',
  24. },
  25. 'modification_date': {
  26. 'fieldtype': 'datetime',
  27. 'now_on_create': True,
  28. 'now_on_update': True,
  29. 'internal': 'automatic',
  30. }
  31. }
  32. relations_common_fields = {
  33. 'id_relation': {
  34. 'fieldtype': 'pk',
  35. 'internal': 'automatic',
  36. },
  37. 'nature': {
  38. 'fieldtype': 'naturerelation',
  39. },
  40. 'depth': {
  41. 'fieldtype': 'integer',
  42. 'internal': 'automatic',
  43. },
  44. 'rank': {
  45. 'fieldtype': 'integer',
  46. 'internal': 'automatic',
  47. },
  48. }
  49. def pk_name():
  50. for name, option in common_fields.items():
  51. if option['fieldtype'] == 'pk':
  52. return name
  53. ## EmNature (Class)
  54. #
  55. # constant name for the nature of type hierarchy
  56. class EmNature(object):
  57. PARENT = 'parent'
  58. TRANSLATION = 'translation'
  59. IDENTITY = 'identity'
  60. @classmethod
  61. def getall(cls):
  62. return [cls.PARENT, cls.TRANSLATION, cls.IDENTITY]
  63. ## EmClassType (Class)
  64. #
  65. # Representation of the classTypes
  66. #
  67. # Defines 3 generic classtype : entity, entry and person
  68. # - entity : to define editorial content
  69. # - entry : to define keywords
  70. # - person : to define people (in the real world, this classtype will only have one class and one type)
  71. #
  72. # The hierarchy dict fixes the possible hierarchical links the types of each classtype can have :
  73. # - 'attach' : what type of superior a type can have
  74. # - 'classtype' a type can have superiors of the same classtype
  75. # - 'type' a type can only have superiors of the same type
  76. # - automatic : possible superiors
  77. # - False : possible superiors must be defined
  78. # - True : possible superiors can not be defined, they will be enforced by the ME automatically
  79. # - maxdepth : maximum depth
  80. # - maxchildren : maximum children
  81. #
  82. # Classtypes contains default internal fields too
  83. #
  84. class EmClassType(object):
  85. entity = {
  86. 'name': 'entity',
  87. 'hierarchy': {
  88. EmNature.PARENT: {
  89. 'attach': 'classtype',
  90. 'automatic': False,
  91. 'maxdepth': -1,
  92. 'maxchildren': -1
  93. },
  94. EmNature.TRANSLATION: {
  95. 'attach': 'type',
  96. 'automatic': False,
  97. 'maxdepth': 1,
  98. 'maxchildren': -1
  99. },
  100. },
  101. 'default_fields': {}
  102. }
  103. entry = {
  104. 'name': 'entry',
  105. 'hierarchy': {
  106. EmNature.PARENT: {
  107. 'attach': 'type',
  108. 'automatic': False,
  109. },
  110. EmNature.TRANSLATION: {
  111. 'attach': 'type',
  112. 'automatic': False,
  113. 'maxdepth': 1,
  114. 'maxchildren': -1
  115. },
  116. },
  117. 'default_fields': {}
  118. }
  119. person = {
  120. 'name': 'person',
  121. 'hierarchy': {
  122. EmNature.IDENTITY: {
  123. 'attach': 'classtype',
  124. 'automatic': True,
  125. 'maxdepth': -1,
  126. 'maxchildren': 1
  127. },
  128. },
  129. 'default_fields': {}
  130. }
  131. ## @brief return a classtype from its name
  132. # @param cls
  133. # @param classtype str : A classtype name
  134. # @return None if no classtype with this name, else return a dict containing classtype informations
  135. @classmethod
  136. def get(cls, classtype):
  137. try:
  138. return getattr(cls, classtype.lower())
  139. except AttributeError:
  140. return None
  141. ## @brief Get all the classtype
  142. # @return A list of dict representing classtypes
  143. @classmethod
  144. def getall(cls):
  145. return [cls.entity, cls.entry, cls.person]
  146. ## @brief Return possible nature of relations for a classtype name
  147. #
  148. # @param classtype_name str: The classtype name
  149. # @return A list of EmNature names (list of str)
  150. @staticmethod
  151. def natures(classtype_name):
  152. if not isinstance(classtype_name, str):
  153. raise TypeError("Excepted <class str> but got %s" % str(type(classtype_name)))
  154. try:
  155. classtype = getattr(EmClassType, classtype_name)
  156. except AttributeError:
  157. raise AttributeError("Unknown classtype : '%s'" % classtype_name)
  158. return classtype['hierarchy'].keys()