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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #-*- coding: utf-8 -*-
  2. import importlib
  3. import warnings
  4. from EditorialModel.components import EmComponent
  5. from EditorialModel.exceptions import EmComponentCheckError
  6. import EditorialModel
  7. import EditorialModel.fieldtypes
  8. #from django.db import models
  9. ## EmField (Class)
  10. #
  11. # Represents one data for a lodel2 document
  12. class EmField(EmComponent):
  13. ranked_in = 'fieldgroup_id'
  14. ftype = None
  15. help = 'Default help text'
  16. ## Instanciate a new EmField
  17. # @todo define and test type for icon and fieldtype
  18. # @warning nullable == True by default
  19. 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):
  20. if self.ftype is None:
  21. raise NotImplementedError("Trying to instanciate an EmField and not one of the fieldtypes child classes")
  22. self.fieldgroup_id = fieldgroup_id
  23. self.check_type('fieldgroup_id', int)
  24. self.optional = optional
  25. self.check_type('optional', bool)
  26. self.internal = internal
  27. self.check_type('internal', bool)
  28. self.rel_field_id = rel_field_id
  29. self.check_type('rel_field_id', (int, type(None)))
  30. self.icon = icon
  31. #Field type elements
  32. self.fieldtype = fieldtype
  33. self.nullable = nullable
  34. self.default = default
  35. self.uniq = uniq
  36. if len(kwargs) > 0:
  37. for kwargs_f in kwargs:
  38. warnings.warn("Argument '%s' not used and will be invalid for EmField __init__" % kwargs_f, SyntaxWarning)
  39. 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)
  40. @staticmethod
  41. ## @brief Return an EmField subclass given a wanted field type
  42. # @return An EmField subclass
  43. # @throw When not found
  44. # @see EmField::fieldtypes()
  45. def get_field_class(ftype, **kwargs):
  46. if ftype == 'integer':
  47. ftype_module = importlib.import_module('EditorialModel.fieldtypes.int')
  48. else:
  49. ftype_module = importlib.import_module('EditorialModel.fieldtypes.%s' % ftype)
  50. return ftype_module.fclass
  51. @staticmethod
  52. ## @brief Return the list of allowed field type
  53. def fieldtypes_list():
  54. return [f for f in EditorialModel.fieldtypes.__all__ if f != '__init__']
  55. ## @brief Abstract method that should return a validation function
  56. # @param raise_e Exception : if not valid raise this exception
  57. # @param ret_valid : if valid return this value
  58. # @param ret_invalid : if not valid return this value
  59. def validation_function(self, raise_e=None, ret_valid=None, ret_invalid=None):
  60. if self.__class__ == EmField:
  61. raise NotImplementedError("Abstract method")
  62. if raise_e is None and ret_valid is None:
  63. raise AttributeError("Behavior doesn't allows to return a valid validation function")
  64. return False
  65. ## @brief Return the list of relation fields for a rel_to_type
  66. # @return None if the field is not a rel_to_type else return a list of EmField
  67. def rel_to_type_fields(self):
  68. if not self.rel_to_type_id: # TODO Ajouter cette propriété
  69. return None
  70. return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
  71. ## Check if the EmField is valid
  72. # @return True if valid False if not
  73. def check(self):
  74. super(EmField, self).check()
  75. em_fieldgroup = self.model.component(self.fieldgroup_id)
  76. if not em_fieldgroup:
  77. raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
  78. if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
  79. raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
  80. ## @brief Delete a field if it's not linked
  81. # @return bool : True if deleted False if deletion aborded
  82. # @todo Check if unconditionnal deletion is correct
  83. def delete_check(self):
  84. return True