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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = 'fieldgroup_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 default * : Default field value
  26. # @param uniq bool : if True the value should be uniq in the db table
  27. # @param **kwargs : more keywords arguments for the fieldtype
  28. def __init__(self, model, uid, name, fieldgroup_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=True, default=None, uniq=False, **kwargs):
  29. self.fieldgroup_id = fieldgroup_id
  30. self.check_type('fieldgroup_id', int)
  31. self.optional = bool(optional)
  32. if not internal:
  33. self.internal = False
  34. else:
  35. if internal.lower() not in ['object', 'automatic']:
  36. raise ValueError("The internal arguments possible values are : [False, 'object', 'automatic']")
  37. self.internal = internal.lower()
  38. self.rel_field_id = rel_field_id
  39. self.check_type('rel_field_id', (int, type(None)))
  40. self.icon = icon
  41. #Field type elements
  42. self._fieldtype_cls = GenericFieldType.from_name(fieldtype)
  43. self.fieldtype = fieldtype
  44. self._fieldtype_args = kwargs
  45. self._fieldtype_args.update({'nullable' : nullable, 'default' : default, 'uniq' : uniq})
  46. try:
  47. fieldtype_instance = self._fieldtype_cls(**self._fieldtype_args)
  48. except AttributeError as e:
  49. raise AttributeError("Error will instanciating fieldtype : %s"%e)
  50. if not fieldtype_instance.check(default):
  51. raise TypeError("Default value ('%s') is not valid given the fieldtype '%s'"%(default, fieldtype))
  52. self.nullable = nullable
  53. self.default = default
  54. self.uniq = uniq
  55. for kname, kval in kwargs.items():
  56. setattr(self, kname, kval)
  57. 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)
  58. @staticmethod
  59. ## @brief Return the list of allowed field type
  60. def fieldtypes_list():
  61. return [f for f in EditorialModel.fieldtypes.__all__ if f != '__init__' and f != 'generic' ]
  62. ## @brief Get the fieldtype instance
  63. # @return a fieldtype instance
  64. def fieldtype_instance(self):
  65. return self._fieldtype_cls(self._fieldtype_args)
  66. ## @brief Return the list of relation fields for a rel_to_type
  67. # @return None if the field is not a rel_to_type else return a list of EmField
  68. def rel_to_type_fields(self):
  69. if not self.rel_to_type_id: # TODO Ajouter cette propriété
  70. return None
  71. return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
  72. ## Check if the EmField is valid
  73. # @return True if valid False if not
  74. def check(self):
  75. super(EmField, self).check()
  76. em_fieldgroup = self.model.component(self.fieldgroup_id)
  77. if not em_fieldgroup:
  78. raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
  79. if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
  80. raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
  81. ## @brief Delete a field if it's not linked
  82. # @return bool : True if deleted False if deletion aborded
  83. # @todo Check if unconditionnal deletion is correct
  84. def delete_check(self):
  85. return True