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.

classes.py 5.7KB

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