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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #-*- coding: utf-8 -*-
  2. import importlib
  3. import warnings
  4. from EditorialModel.components import EmComponent
  5. from EditorialModel.exceptions import EmComponentCheckError
  6. from EditorialModel.fieldtypes.generic import GenericFieldType
  7. import EditorialModel
  8. import EditorialModel.fieldtypes
  9. #from django.db import models
  10. ## EmField (Class)
  11. #
  12. # Represents one data for a lodel2 document
  13. class EmField(EmComponent):
  14. ranked_in = 'class_id'
  15. ## Instanciate a new EmField
  16. # @todo define and test type for icon
  17. # @warning nullable == True by default
  18. # @param model Model : Editorial model
  19. # @param uid int : Uniq id
  20. # @param fieldtype str : Fieldtype name ( see Editorialmodel::fieldtypes )
  21. # @param optional bool : Indicate if a field is optional or not
  22. # @param internal str|bool : If False the field is not internal, else it can takes value in "object" or "automatic"
  23. # @param rel_field_id int|None : If not None indicates that the field is a relation attribute (and the value is the UID of the rel2type field)
  24. # @param nullable bool : If True None values are allowed
  25. # @param uniq bool : if True the value should be uniq in the db table
  26. # @param **kwargs : more keywords arguments for the fieldtype
  27. def __init__(self, model, uid, name, class_id, fieldtype, optional=False, internal=False, rel_field_id=None, icon='0', string=None, help_text=None, date_update=None, date_create=None, rank=None, nullable=False, uniq=False, **kwargs):
  28. self.class_id = class_id
  29. self.check_type('class_id', int)
  30. self.optional = bool(optional)
  31. if not internal:
  32. self.internal = False
  33. else:
  34. if internal.lower() not in ['automatic']:
  35. raise ValueError("The internal arguments possible values are : [False, 'object', 'automatic']")
  36. self.internal = internal.lower()
  37. if self.internal == 'object' and name not in EditorialModel.classtypes.common_fields.keys():
  38. raise ValueError("Only common_fields are allowed to have the argument internal='object'")
  39. self.rel_field_id = rel_field_id
  40. self.check_type('rel_field_id', (int, type(None)))
  41. self.icon = icon
  42. #Field type elements
  43. self._fieldtype_cls = GenericFieldType.from_name(fieldtype)
  44. self.fieldtype = fieldtype
  45. self._fieldtype_args = kwargs
  46. self._fieldtype_args.update({'nullable': nullable, 'uniq': uniq, 'internal': self.internal})
  47. try:
  48. fieldtype_instance = self._fieldtype_cls(**self._fieldtype_args)
  49. except AttributeError as e:
  50. raise AttributeError("Error will instanciating fieldtype : %s" % e)
  51. if 'default' in kwargs:
  52. if not fieldtype_instance.check(default):
  53. raise TypeError("Default value ('%s') is not valid given the fieldtype '%s'" % (default, fieldtype))
  54. self.nullable = nullable
  55. self.uniq = uniq
  56. for kname, kval in kwargs.items():
  57. setattr(self, kname, kval)
  58. super(EmField, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
  59. @staticmethod
  60. ## @brief Return the list of allowed field type
  61. def fieldtypes_list():
  62. return [f for f in EditorialModel.fieldtypes.__all__ if f != '__init__' and f != 'generic']
  63. ##@brief Return the EmFieldgroup this EmField belongs to
  64. @property
  65. def _fieldgroup(self):
  66. return self.model.component(self.fieldgroup_id)
  67. ## @brief Returns the EmClass this EmField belongs to
  68. @property
  69. def em_class(self):
  70. return self.model.component(self.class_id)
  71. ## @brief Get the fieldtype instance
  72. # @return a fieldtype instance
  73. def fieldtype_instance(self):
  74. return self._fieldtype_cls(**self._fieldtype_args)
  75. ## @brief Return the list of relation fields for a rel_to_type
  76. # @return None if the field is not a rel_to_type else return a list of EmField
  77. def rel_to_type_fields(self):
  78. if not self.rel_to_type_id: # TODO Ajouter cette propriété
  79. return None
  80. return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
  81. ## Check if the EmField is valid
  82. #
  83. # Check multiple things :
  84. # - the fieldgroup_id is valid
  85. # - the name is uniq in the EmClass
  86. # - if its a rel2type check that the linked_type is uniq in the EmClass
  87. # @return True if valid False if not
  88. def check(self):
  89. super(EmField, self).check()
  90. """
  91. #Fieldgroup check
  92. em_fieldgroup = self.model.component(self.fieldgroup_id)
  93. if not em_fieldgroup:
  94. raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
  95. if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
  96. raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
  97. """
  98. #Uniq Name check
  99. if len([f for f in self.em_class.fields() if f.name == self.name]) > 1:
  100. raise EmComponentCheckError("The field %d has a name '%s' that is not uniq in the EmClass %d" % (self.uid, self.name, self.em_class.uid))
  101. #rel2type uniq check
  102. if self.fieldtype == 'rel2type':
  103. if len([f for f in self.em_class.fields() if f.fieldtype == 'rel2type' and f.rel_to_type_id == self.rel_to_type_id]) > 1:
  104. raise EmComponentCheckError("The rel2type %d is not uniq, another field is linked to the same type '%s' in the same class '%s'" % (self.uid, self.model.component(self.rel_to_type_id).name, self.em_class.name))
  105. ## @brief Delete a field if it's not linked
  106. # @return bool : True if deleted False if deletion aborded
  107. # @todo Check if unconditionnal deletion is correct
  108. def delete_check(self):
  109. return True