暫無描述
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.

fieldgroups.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #-*- coding: utf-8 -*-
  2. from EditorialModel.components import EmComponent
  3. from EditorialModel.classes import EmClass
  4. import EditorialModel.fieldtypes as ftypes
  5. from Database import sqlutils
  6. import EditorialModel
  7. ## Represents groups of EmField associated with an EmClass
  8. #
  9. # EmClass fields representation is organised with EmFieldGroup
  10. # @see EditorialModel::fields::EmField EditorialModel::classes::EmClass
  11. class EmFieldGroup(EmComponent):
  12. ## The database table name
  13. table = 'em_fieldgroup'
  14. ranked_in = 'class_id'
  15. ## List of fields
  16. _fields = [('class_id', ftypes.EmField_integer)]
  17. @classmethod
  18. ## Create a new EmFieldGroup
  19. #
  20. # Save it in database and return an instance*
  21. # @param name str: The name of the new EmFieldGroup
  22. # @param em_class EmClass : An EditorialModel::classes::EmClass instance
  23. # @param **em_component_args : @ref EditorialModel::components::create()
  24. # @throw EmComponentExistError If an EmFieldGroup with this name allready exists
  25. # @throw TypeError If an argument is of an unexepted type
  26. def create(cls, name, em_class, **em_component_args):
  27. if not isinstance(name, str):
  28. raise TypeError("Excepting <class str> as name. But got " + str(type(name)))
  29. if not isinstance(em_class, EmClass):
  30. raise TypeError("Excepting <class EmClass> as em_class. But got "+str(type(name)))
  31. return super(EmFieldGroup, cls).create(name=name, class_id=em_class.uid, **em_component_args)
  32. ## Get the list of associated fields
  33. # @return A list of EmField instance
  34. def fields(self):
  35. field_table = sqlutils.getTable(EditorialModel.fields.EmField)
  36. req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
  37. conn = self.__class__.db_engine().connect()
  38. res = conn.execute(req)
  39. rows = res.fetchall()
  40. conn.close()
  41. return [EditorialModel.fields.EmField(row['uid']) for row in rows]