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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. ## List of fields
  15. # @todo Bad storage, here we want an ordereddict not a tuple list
  16. _fields = [('class_id', ftypes.EmField_integer)]
  17. ## Instanciate an EmFieldGroup with data fetched from db
  18. # @param id_or_name str|int: Identify the EmFieldGroup by name or by global_id
  19. # @throw TypeError
  20. # @see EditorialModel::components::EmComponent::__init__()
  21. # @throw EditorialModel::components::EmComponentNotExistError
  22. @classmethod
  23. ## Create a new EmFieldGroup
  24. #
  25. # Save it in database and return an instance*
  26. # @param name str: The name of the new EmFieldGroup
  27. # @param em_class EmClass|str|int : Can be an EditorialModel::classes::EmClass uid, name or instance
  28. # @throw ValueError If an EmFieldGroup with this name allready exists
  29. # @throw ValueError If the specified EmClass don't exists
  30. # @throw TypeError If an argument is of an unexepted type
  31. def create(cls, name, em_class):
  32. if not isinstance(em_class, EmClass):
  33. if isinstance(em_class, int) or isinstance(em_class, str):
  34. try:
  35. arg = em_class
  36. em_class = EmClass(arg)
  37. except EmComponentNotExistError:
  38. raise ValueError("No EmClass found with id or name '" + arg + "'")
  39. else:
  40. raise TypeError("Excepting an EmClass, an int or a str for 'em_class' argument. Not an " + str(type(em_class)) + ".")
  41. if not isinstance(name, str):
  42. raise TypeError("Excepting a string as first argument, not an " + str(type(name)))
  43. try:
  44. exists = EmFieldGroup(name)
  45. raise ValueError("An EmFieldgroup named " + name + " allready exist")
  46. except EmComponentNotExistError:
  47. return super(EmFieldGroup, cls).create(name=name, class_id=em_class.uid) # Check the return value ?
  48. return exists
  49. ## Get the list of associated fields
  50. # @return A list of EmField instance
  51. # @todo Implement this method
  52. def fields(self):
  53. field_table = sqlutils.getTable(EditorialModel.fields.EmField)
  54. req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
  55. conn = self.__class__.db_engine().connect()
  56. res = conn.execute(req)
  57. rows = res.fetchall()
  58. conn.close()
  59. return [EditorialModel.fields.EmField(row['uid']) for row in rows]