暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

fields.py 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #-*- coding: utf-8 -*-
  2. from EditorialModel.components import EmComponent
  3. from EditorialModel.exceptions import EmComponentCheckError
  4. import EditorialModel
  5. from django.db import models
  6. ## EmField (Class)
  7. #
  8. # Represents one data for a lodel2 document
  9. class EmField(EmComponent):
  10. ranked_in = 'fieldgroup_id'
  11. fieldtypes = {
  12. 'int': models.IntegerField,
  13. 'integer': models.IntegerField,
  14. 'bigint': models.BigIntegerField,
  15. 'smallint': models.SmallIntegerField,
  16. 'boolean': models.BooleanField,
  17. 'bool': models.BooleanField,
  18. 'float': models.FloatField,
  19. 'char': models.CharField,
  20. 'varchar': models.CharField,
  21. 'text': models.TextField,
  22. 'time': models.TimeField,
  23. 'date': models.DateField,
  24. 'datetime': models.DateTimeField,
  25. }
  26. ## Instanciate a new EmField
  27. # @todo define and test type for icon and fieldtype
  28. def __init__(self, model, uid, name, fieldgroup_id, fieldtype, optional=False, internal=False, rel_to_type_id=None, rel_field_id=None, icon='0', string=None, help_text=None, date_update=None, date_create=None, rank=None, **kwargs):
  29. self.fieldgroup_id = fieldgroup_id
  30. self.check_type('fieldgroup_id', int)
  31. self.fieldtype = fieldtype
  32. self.optional = optional
  33. self.check_type('optional', bool)
  34. self.internal = internal
  35. self.check_type('internal', bool)
  36. self.rel_to_type_id = rel_to_type_id
  37. self.check_type('rel_to_type_id', (int, type(None)))
  38. self.rel_field_id = rel_field_id
  39. self.check_type('rel_field_id', (int, type(None)))
  40. self.icon = icon
  41. self.options = kwargs
  42. 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)
  43. ## @brief Return the list of relation fields for a rel_to_type
  44. # @return None if the field is not a rel_to_type else return a list of EmField
  45. def rel_to_type_fields(self):
  46. if not self.rel_to_type_id:
  47. return None
  48. return [ f for f in self.model.components(EmField) if f.rel_field_id == self.uid ]
  49. ## Check if the EmField is valid
  50. # @return True if valid False if not
  51. def check(self):
  52. super(EmField, self).check()
  53. em_fieldgroup = self.model.component(self.fieldgroup_id)
  54. if not em_fieldgroup:
  55. raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
  56. if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
  57. raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
  58. ## @brief Delete a field if it's not linked
  59. # @return bool : True if deleted False if deletion aborded
  60. # @todo Check if unconditionnal deletion is correct
  61. def delete_check(self):
  62. return True
  63. def to_django(self):
  64. if self.fieldtype in ('varchar', 'char'):
  65. max_length = None if 'max_length' not in self.options else self.options['max_length']
  66. return self.fieldtypes[self.fieldtype](max_length=max_length, **self.options)
  67. if self.fieldtype in ('time', 'datetime', 'date'):
  68. auto_now = False if 'auto_now' not in self.options else self.options['auto_now']
  69. auto_now_add = False if 'auto_now_add' not in self.options else self.options['auto_now_add']
  70. return self.fieldtypes[self.fieldtype](auto_now=auto_now, auto_now_add=auto_now_add, **self.options)
  71. if self.fieldtype == 'boolean' and ('nullable' in self.options and self.options['nullable'] == 1):
  72. return models.NullBooleanField(**self.options)
  73. return self.fieldtypes[self.fieldtype](**self.options)