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.

fields.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #-*- coding: utf-8 -*-
  2. from EditorialModel.components import EmComponent
  3. from EditorialModel.fieldtypes import EmField_boolean, EmField_char, EmField_integer, EmField_icon, get_field_type
  4. from EditorialModel.fieldgroups import EmFieldGroup
  5. from EditorialModel.classes import EmClass
  6. from Database import sqlutils
  7. from Database.sqlalter import DropColumn, AddColumn
  8. import sqlalchemy as sql
  9. ## EmField (Class)
  10. #
  11. # Represents one data for a lodel2 document
  12. class EmField(EmComponent):
  13. table = 'em_field'
  14. ranked_in = 'fieldgroup_id'
  15. _fields = [
  16. ('fieldtype', EmField_char),
  17. ('fieldgroup_id', EmField_integer),
  18. ('rel_to_type_id', EmField_integer),
  19. ('rel_field_id', EmField_integer),
  20. ('optional', EmField_boolean),
  21. ('internal', EmField_boolean),
  22. ('icon', EmField_icon)
  23. ]
  24. ## Create (Function)
  25. #
  26. # Creates a new EmField and instanciates it
  27. #
  28. # @static
  29. #
  30. # @param name str: Name of the field
  31. # @param fieldgroup EmFieldGroup: Field group in which the field is
  32. # @param fieldtype EmFieldType: Type of the field
  33. # @param optional int: is the field optional ? (default=0)
  34. # @param internal int: is the field internal ? (default=0)
  35. # @param rel_to_type_id int: default=0
  36. # @param rel_field_id int: default=0
  37. # @param icon int: default=0
  38. # @param **em_component_args : @ref EditorialModel::components::create()
  39. #
  40. # @throw TypeError
  41. # @throw RuntimeError if the associated column creation fails
  42. # @throw EmComponentExistError if an EmField with this name allready exists in this fieldgroup
  43. # @see EmComponent::__init__()
  44. # @staticmethod
  45. @classmethod
  46. def create(cls, name, fieldgroup, fieldtype, optional=0, internal=0, rel_to_type_id=0, rel_field_id=0, icon=None, **em_component_args):
  47. created_field = super(EmField, cls).create(
  48. name=name,
  49. fieldgroup_id=fieldgroup.uid,
  50. fieldtype=fieldtype.name,
  51. optional=optional,
  52. internal=internal,
  53. rel_to_type_id=rel_to_type_id,
  54. rel_field_id=rel_field_id,
  55. icon=icon,
  56. **em_component_args
  57. )
  58. if not created_field.add_field_column_to_class_table():
  59. raise RuntimeError("Unable to create the column for the EmField " + str(created_field))
  60. return created_field
  61. ## @brief Delete a field if it's not linked
  62. # @return bool : True if deleted False if deletion aborded
  63. # @todo Check if unconditionnal deletion is correct
  64. def delete(self):
  65. dbe = self.db_engine
  66. class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
  67. field_col = sql.Column(self.name)
  68. ddl = DropColumn(class_table, field_col)
  69. sqlutils.ddl_execute(ddl, self.db_engine)
  70. return super(EmField, self).delete()
  71. ## add_field_column_to_class_table (Function)
  72. #
  73. # Adds a column representing the field in its class' table
  74. #
  75. # @return True in case of success, False if not
  76. def add_field_column_to_class_table(self):
  77. dbe = self.db_engine
  78. fieldtype = get_field_type(self.fieldtype)
  79. new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()))
  80. class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
  81. ddl = AddColumn(class_table, new_column)
  82. return sqlutils.ddl_execute(ddl, dbe)
  83. ## get_class_table (Function)
  84. #
  85. # Gets the name of the table of the class corresponding to the field
  86. #
  87. # @return Name of the table
  88. def get_class_table(self):
  89. return self.get_class().class_table_name
  90. ## @brief Get the class that contains this field
  91. # @return An EmClass instance
  92. def get_class(self):
  93. #<SQL>
  94. dbe = self.db_engine
  95. meta = sqlutils.meta(dbe)
  96. conn = dbe.connect()
  97. fieldgroup_table = sql.Table(EmFieldGroup.table, meta)
  98. req = fieldgroup_table.select().where(fieldgroup_table.c.uid == self.fieldgroup_id)
  99. res = conn.execute(req)
  100. row = res.fetchone()
  101. #</SQL>
  102. return EmClass(row['class_id'])