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 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #-*- coding: utf-8 -*-
  2. from EditorialModel.components import EmComponent, EmComponentNotExistError
  3. from Database import sqlutils
  4. import sqlalchemy as sql
  5. import EditorialModel
  6. """Represent one data for a lodel2 document"""
  7. class EmField(EmComponent):
  8. table = 'em_field'
  9. def __init__(self, id_or_name):
  10. """ Instanciate an EmField with data fetched from db
  11. @param id_or_name str|int: Identify the EmType by name or by global_id
  12. @throw TypeError
  13. @see EmComponent::__init__()
  14. """
  15. self.table = EmField.table
  16. super(EmField, self).__init__(id_or_name)
  17. @classmethod
  18. def create(c, name, em_fieldgroup, em_fieldtype, optional=True, internal=False):
  19. """ Create a new EmField and instanciate it
  20. @static
  21. @param name str: The name of the new Type
  22. @param em_fieldgroup EmFieldGroup: The new field will belong to this fieldgroup
  23. @param ml_repr MlString|None: Multilingual representation of the type
  24. @param ml_help MlString|None: Multilingual help for the type
  25. @param The string|None: filename of the icon
  26. @param optional bool: Is the field optional ?
  27. @param internal bool: Is the field internal?
  28. @throw TypeError
  29. @see EmComponent::__init__()
  30. @staticmethod
  31. """
  32. try:
  33. exists = EmField(name)
  34. except EmComponentNotExistError:
  35. values = {
  36. 'uid' : None,
  37. 'name' : name,
  38. 'fieldgroup_id' : em_fieldgroup.id,
  39. 'fieldtype' : em_fieldtype.name,
  40. 'optional' : 1 if optional else 0,
  41. 'internal' : 1 if internal else 0,
  42. }
  43. return super(EmField,c).create(values)
  44. return exists
  45. """ Use dictionary (from database) to populate the object
  46. """
  47. def populate(self):
  48. row = super(EmField, self).populate()
  49. self.em_fieldgroup = EditorialModel.fieldgroups.EmFieldGroup(int(row.fieldgroup_id))
  50. self.em_fieldtype = EditorialModel.fieldtypes.get_field_type(row.fieldtype)
  51. self.optional = True if row.optional == 1 else False;
  52. self.internal = True if row.internal == 1 else False;
  53. self.icon = row.icon
  54. self.rel_to_type_id = EditorialModel.fieldtypes.EmFieldType(int(row.rel_to_type_id)) if row.rel_to_type_id else None
  55. self.rel_field_id = EmField(int(row.rel_field_id)) if row.rel_field_id else None
  56. def save(self):
  57. # should not be here, but cannot see how to do this
  58. if self.name is None:
  59. self.populate()
  60. values = {
  61. 'fieldgroup_id' : self.em_fieldgroup.id,
  62. 'fieldtype' : self.em_fieldtype.name,
  63. 'optional' : 1 if self.optional else 0,
  64. 'internal' : 1 if self.internal else 0,
  65. 'icon' : self.icon,
  66. 'rel_to_type_id' : self.rel_to_type_id.id if self.rel_to_type_id is not None else None,
  67. 'rel_field_id' : self.rel_field_id.id if self.rel_field_id is not None else None
  68. }
  69. return super(EmField, self).save(values)