Нема описа
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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #-*- coding: utf-8 -*-
  2. import leapi.letype as letype
  3. import leapi.leclass as leclass
  4. from .generic import ReferenceFieldType, FieldTypeError
  5. class EmFieldType(ReferenceFieldType):
  6. help = 'Fieldtypes designed to handle pk of LeObject in LeRelations'
  7. _construct_data_deps = []
  8. ## @todo Replace hardcoded string for reference initialisation
  9. def __init__(self, superior=True, **kwargs):
  10. super().__init__(superior = superior, reference='object', **kwargs)
  11. def _check_data_value(self, value):
  12. if not isinstance(value, int):
  13. if not letype._LeType.implements_leobject():
  14. return (None, ValueError("An instance of a child class of LeType was expected"))
  15. if not hasattr(value, 'lodel_id'):
  16. return (None, ValueError("The LeType instance given has no lodel_id !"))
  17. return (value, None)
  18. ## @brief If field value is an integer, returns a partially instanciated LeObject (only with an ID)
  19. # @todo what should we do if the get fails ? Raise ?
  20. def construct_data(self, lec, fname, datas, cur_value):
  21. if isinstance(cur_value, str):
  22. # Cast to int
  23. try:
  24. cur_value = int(cur_value)
  25. except (ValueError, TypeError) as e:
  26. raise e # Raise Here !?
  27. if hasattr(cur_value, 'is_leobject') and cur_value.is_leobject():
  28. # Its an object not populated (we dont now its type)
  29. cur_value = cur_value.lodel_id #Optimize here giving only class_id and type_id to populate ?
  30. if isinstance(cur_value, int):
  31. # Get instance with id
  32. resget = lec.name2class('LeObject').get(['lodel_id = %d' % cur_value])
  33. if resget is None or len(resget) != 1:
  34. # Bad filter or bad id... raise ?
  35. raise Exception("BAAAAAD")
  36. return cur_value
  37. ## @brief checks datas consistency
  38. # @param lec LeCrud : A LeCrud child instance
  39. # @param fname str : concerned field name
  40. # @param datas dict : Datas of lec
  41. # @return True if ok else an Exception instance
  42. def check_data_consistency(self, lec, fname, datas):
  43. if self.superior:
  44. return self.check_sup_consistency(lec, fname, datas)
  45. else:
  46. return self.check_sub_consistency(lec, fname, datas)
  47. def check_sup_consistency(self, lec, fname, datas):
  48. if lec.implements_lerel2type():
  49. # Checking consistency for a rel2type relation
  50. lesup = datas[lec._superior_field_name]
  51. lesub = datas[lec._subordinate_field_name]
  52. if lesub.__class__ not in lesup._linked_types.values():
  53. return FieldTypeError("Rel2type not authorized between %s and %s"%(lesup, lesub))
  54. pass
  55. def check_sub_consistency(self, lec, fname, datas):
  56. pass