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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 == 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. ftype_module = importlib.import_module('EditorialModel.fieldtypes.%s'%ftype)
  47. return ftype_module.fclass
  48. @staticmethod
  49. ## @brief Return the list of allowed field type
  50. def fieldtypes_list():
  51. return [ f for f in EditorialModel.fieldtypes.__all__ if f != '__init__' ]
  52. ## @brief Abstract method that should return a validation function
  53. # @param raise_e Exception : if not valid raise this exception
  54. # @param ret_valid : if valid return this value
  55. # @param ret_invalid : if not valid return this value
  56. def validation_function(self, raise_e = None, ret_valid = None, ret_invalid = None):
  57. if self.__class__ == EmField:
  58. raise NotImplementedError("Abstract method")
  59. if raise_e is None and ret_valid is None:
  60. raise AttributeError("Behavior doesn't allows to return a valid validation function")
  61. return False
  62. ## @brief Return the list of relation fields for a rel_to_type
  63. # @return None if the field is not a rel_to_type else return a list of EmField
  64. def rel_to_type_fields(self):
  65. if not self.rel_to_type_id:
  66. return None
  67. return [ f for f in self.model.components(EmField) if f.rel_field_id == self.uid ]
  68. ## Check if the EmField is valid
  69. # @return True if valid False if not
  70. def check(self):
  71. super(EmField, self).check()
  72. em_fieldgroup = self.model.component(self.fieldgroup_id)
  73. if not em_fieldgroup:
  74. raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
  75. if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
  76. raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
  77. ## @brief Delete a field if it's not linked
  78. # @return bool : True if deleted False if deletion aborded
  79. # @todo Check if unconditionnal deletion is correct
  80. def delete_check(self):
  81. return True