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.3KB

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