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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #-*- coding: utf-8 -*-
  2. from EditorialModel.components import EmComponent, EmComponentNotExistError
  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. # @todo Bad storage, here we want an ordereddict not a tuple list
  17. _fields = [('class_id', ftypes.EmField_integer)]
  18. ## Instanciate an EmFieldGroup with data fetched from db
  19. # @param id_or_name str|int: Identify the EmFieldGroup by name or by global_id
  20. # @throw TypeError
  21. # @see EditorialModel::components::EmComponent::__init__()
  22. # @throw EditorialModel::components::EmComponentNotExistError
  23. @classmethod
  24. ## Create a new EmFieldGroup
  25. #
  26. # Save it in database and return an instance*
  27. # @param name str: The name of the new EmFieldGroup
  28. # @param em_class EmClass : An EditorialModel::classes::EmClass instance
  29. # @param **em_component_args : @ref EditorialModel::components::create()
  30. # @throw EmComponentExistError If an EmFieldGroup with this name allready exists
  31. # @throw TypeError If an argument is of an unexepted type
  32. def create(cls, name, em_class, **em_component_args):
  33. if not isinstance(name, str):
  34. raise TypeError("Excepting <class str> as name. But got " + str(type(name)))
  35. if not isinstance(em_class, EmClass):
  36. raise TypeError("Excepting <class EmClass> as em_class. But got "+str(type(name)))
  37. return super(EmFieldGroup, cls).create(name=name, class_id=em_class.uid, **em_component_args)
  38. ## Get the list of associated fields
  39. # @return A list of EmField instance
  40. def fields(self):
  41. field_table = sqlutils.getTable(EditorialModel.fields.EmField)
  42. req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
  43. conn = self.__class__.db_engine().connect()
  44. res = conn.execute(req)
  45. rows = res.fetchall()
  46. conn.close()
  47. return [EditorialModel.fields.EmField(row['uid']) for row in rows]