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.

fieldgroups.py 2.0KB

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