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

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