Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

classes.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # -*- coding: utf-8 -*-
  2. ## @file classes.py
  3. # @see EditorialModel::classes::EmClass
  4. from EditorialModel.components import EmComponent
  5. import EditorialModel.fieldtypes as ftypes
  6. import EditorialModel
  7. ## @brief Manipulate Classes of the Editorial Model
  8. # Create classes of object.
  9. # @see EmClass, EmType, EmFieldGroup, EmField
  10. # @todo sortcolumn handling
  11. class EmClass(EmComponent):
  12. table = 'em_class'
  13. ranked_in = 'classtype'
  14. ## @brief Specific EmClass fields
  15. # @see EditorialModel::components::EmComponent::_fields
  16. _fields = [
  17. ('classtype', ftypes.EmField_char),
  18. ('icon', ftypes.EmField_icon),
  19. ('sortcolumn', ftypes.EmField_char)
  20. ]
  21. ## Create a new class
  22. # @param name str: name of the new class
  23. # @param class_type EmClasstype: type of the class
  24. # @return an EmClass instance
  25. # @throw EmComponentExistError if an EmClass with this name and a different classtype exists
  26. @classmethod
  27. def create(cls, name, classtype, icon=None, sortcolumn='rank', **em_component_args):
  28. result = super(Em, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
  29. return result
  30. # return cls._create_db(name=name, classtype=classtype['name'], icon=icon, sortcolumn=sortcolumn, **em_component_args)
  31. # @classmethod
  32. # def _create_db(cls, name, classtype, icon, sortcolumn, **em_component_args):
  33. # result = super(Em, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
  34. #
  35. # dbe = result.db_engine
  36. # conn= dbe.connect()
  37. #
  38. # meta = sql.MetaData()
  39. # emclasstable = sql.Table(result.class_table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
  40. # emclasstable.create(conn)
  41. # conn.close()
  42. # return result
  43. @property
  44. ## @brief Return the table name used to stores data on this class
  45. def class_table_name(self):
  46. return self.name
  47. ## @brief Delete a class if it's ''empty''
  48. # If a class has no fieldgroups delete it
  49. # @return bool : True if deleted False if deletion aborded
  50. def delete(self):
  51. pass
  52. # fieldgroups = self.fieldgroups()
  53. # if len(fieldgroups) > 0:
  54. # return False
  55. #
  56. # dbe = self.db_engine
  57. # meta = sqlutils.meta(dbe)
  58. # Here we have to give a connection
  59. # class_table = sql.Table(self.name, meta)
  60. # meta.drop_all(tables=[class_table], bind=dbe)
  61. # return super(EmClass, self).delete()
  62. ## Retrieve list of the field_groups of this class
  63. # @return A list of fieldgroups instance
  64. def fieldgroups(self):
  65. records = self._fieldgroups_db() # TODO Modifier l'appel
  66. fieldgroups = [EditorialModel.fieldgroups.EmFieldGroup(int(record.uid)) for record in records]
  67. return fieldgroups
  68. ## Isolate SQL for EmClass::fieldgroups
  69. # @return An array of dict (sqlalchemy fetchall)
  70. # def _fieldgroups_db(self):
  71. # dbe = self.db_engine
  72. # emfg = sql.Table(EditorialModel.fieldgroups.EmFieldGroup.table, sqlutils.meta(dbe))
  73. # req = emfg.select().where(emfg.c.class_id == self.uid)
  74. #
  75. # conn = dbe.connect()
  76. # res = conn.execute(req)
  77. # return res.fetchall()
  78. ## Retrieve list of fields
  79. # @return fields [EmField]:
  80. def fields(self):
  81. fieldgroups = self.fieldgroups()
  82. fields = []
  83. for fieldgroup in fieldgroups:
  84. fields += fieldgroup.fields()
  85. return fields
  86. ## Retrieve list of type of this class
  87. # @return types [EmType]:
  88. def types(self):
  89. pass
  90. records = self._types_db() # TODO Modifier l'appel
  91. types = [EditorialModel.types.EmType(int(record.uid)) for record in records]
  92. return types
  93. ## Isolate SQL for EmCLass::types
  94. # @return An array of dict (sqlalchemy fetchall)
  95. # def _types_db(self):
  96. # dbe = self.db_engine
  97. # emtype = sql.Table(EditorialModel.types.EmType.table, sqlutils.meta(dbe))
  98. # req = emtype.select().where(emtype.c.class_id == self.uid)
  99. # conn = dbe.connect()
  100. # res = conn.execute(req)
  101. # return res.fetchall()
  102. ## Add a new EmType that can ben linked to this class
  103. # @param em_type EmType: type to link
  104. # @return success bool: done or not
  105. def link_type(self, em_type):
  106. table_name = self.name + '_' + em_type.name
  107. self._link_type_db(table_name) # TODO Modifier l'appel
  108. return True
  109. # def _link_type_db(self, table_name):
  110. # Create a new table storing additionnal fields for the relation between the linked type and this EmClass
  111. # conn = self.db_engine.connect()
  112. # meta = sql.MetaData()
  113. # emlinketable = sql.Table(table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
  114. # emlinketable.create(conn)
  115. # conn.close()
  116. ## Retrieve list of EmType that are linked to this class
  117. # @return types [EmType]:
  118. def linked_types(self):
  119. return self._linked_types_db() # TODO Modifier l'appel
  120. # def _linked_types_db(self):
  121. # dbe = self.db_engine
  122. # meta = sql.MetaData()
  123. # meta.reflect(dbe)
  124. #
  125. # linked_types = []
  126. # for table in meta.tables.values():
  127. # table_name_elements = table.name.split('_')
  128. # if len(table_name_elements) == 2:
  129. # linked_types.append(EditorialModel.types.EmType(table_name_elements[1]))
  130. #
  131. # return linked_types