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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #-*- coding: utf-8 -*-
  2. import importlib
  3. from EditorialModel.components import EmComponent
  4. from EditorialModel.exceptions import EmComponentCheckError
  5. import EditorialModel
  6. from django.db import models
  7. ## EmField (Class)
  8. #
  9. # Represents one data for a lodel2 document
  10. class EmField(EmComponent):
  11. ranked_in = 'fieldgroup_id'
  12. ftype = None
  13. fieldtypes = {
  14. 'int': models.IntegerField,
  15. 'integer': models.IntegerField,
  16. 'bigint': models.BigIntegerField,
  17. 'smallint': models.SmallIntegerField,
  18. 'boolean': models.BooleanField,
  19. 'bool': models.BooleanField,
  20. 'float': models.FloatField,
  21. 'char': models.CharField,
  22. 'varchar': models.CharField,
  23. 'text': models.TextField,
  24. 'time': models.TimeField,
  25. 'date': models.DateField,
  26. 'datetime': models.DateTimeField,
  27. }
  28. ## Instanciate a new EmField
  29. # @todo define and test type for icon and fieldtype
  30. # @warning nullable == True by default
  31. def __init__(self, model, uid, name, fieldgroup_id, 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):
  32. self.fieldgroup_id = fieldgroup_id
  33. self.check_type('fieldgroup_id', int)
  34. self.optional = optional
  35. self.check_type('optional', bool)
  36. self.internal = internal
  37. self.check_type('internal', bool)
  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.nullable = nullable
  43. self.default = default
  44. self.uniq = uniq
  45. self.options = kwargs
  46. 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)
  47. @staticmethod
  48. def get_field_class(ftype, **kwargs):
  49. ftype_module = importlib.import_module('EditorialModel.fieldtypes.%s'%ftype)
  50. return ftype_module.fclass
  51. ## @brief Abstract method that should return a validation function
  52. # @param raise_e Exception : if not valid raise this exception
  53. # @param ret_valid : if valid return this value
  54. # @param ret_invalid : if not valid return this value
  55. def validation_function(self, raise_e = None, ret_valid = None, ret_invalid = None):
  56. if self.__class__ == EmField:
  57. raise NotImplementedError("Abstract method")
  58. if raise_e is None and ret_valid is None:
  59. raise AttributeError("Behavior doesn't allows to return a valid validation function")
  60. return False
  61. ## @brief Return the list of relation fields for a rel_to_type
  62. # @return None if the field is not a rel_to_type else return a list of EmField
  63. def rel_to_type_fields(self):
  64. if not self.rel_to_type_id:
  65. return None
  66. return [ f for f in self.model.components(EmField) if f.rel_field_id == self.uid ]
  67. ## Check if the EmField is valid
  68. # @return True if valid False if not
  69. def check(self):
  70. super(EmField, self).check()
  71. em_fieldgroup = self.model.component(self.fieldgroup_id)
  72. if not em_fieldgroup:
  73. raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
  74. if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
  75. raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
  76. ## @brief Delete a field if it's not linked
  77. # @return bool : True if deleted False if deletion aborded
  78. # @todo Check if unconditionnal deletion is correct
  79. def delete_check(self):
  80. return True
  81. """
  82. def to_django(self):
  83. if self.fieldtype in ('varchar', 'char'):
  84. max_length = None if 'max_length' not in self.options else self.options['max_length']
  85. return self.fieldtypes[self.fieldtype](max_length=max_length, **self.options)
  86. if self.fieldtype in ('time', 'datetime', 'date'):
  87. auto_now = False if 'auto_now' not in self.options else self.options['auto_now']
  88. auto_now_add = False if 'auto_now_add' not in self.options else self.options['auto_now_add']
  89. return self.fieldtypes[self.fieldtype](auto_now=auto_now, auto_now_add=auto_now_add, **self.options)
  90. if self.fieldtype == 'boolean' and ('nullable' in self.options and self.options['nullable'] == 1):
  91. return models.NullBooleanField(**self.options)
  92. return self.fieldtypes[self.fieldtype](**self.options)
  93. """