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.

lerelation.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #-*- coding: utf-8 -*-
  2. import copy
  3. import EditorialModel.fieldtypes.leo as ft_leo
  4. from . import lecrud
  5. from . import leobject
  6. ## @brief Main class for relations
  7. class _LeRelation(lecrud._LeCrud):
  8. ## @brief Handles the superior
  9. _lesup_fieldtype = {'lesup': ft_leo.EmFieldType(True)}
  10. ## @brief Handles the subordinate
  11. _lesub_fieldtype = {'lesub': ft_leo.EmFieldType(False) }
  12. ## @brief Stores the list of fieldtypes that are common to all relations
  13. _rel_fieldtypes = dict()
  14. def __init__(self, rel_id, **kwargs):
  15. pass
  16. @classmethod
  17. def sup_filter(self, leo):
  18. if isinstance(leo, leobject._LeObject):
  19. return ('lesup', '=', leo)
  20. @classmethod
  21. def sub_filter(self, leo):
  22. if isinstance(leo, leobject._LeObject):
  23. return ('lesub', '=', leo)
  24. @classmethod
  25. def fieldtypes(cls):
  26. rel_ft = dict()
  27. rel_ft.update(cls._lesup_fieldtype)
  28. rel_ft.update(cls._lesub_fieldtype)
  29. rel_ft.update(cls._rel_fieldtypes)
  30. if cls.implements_lerel2type():
  31. rel_ft.update(cls._rel_attr_fieldtypes)
  32. return rel_ft
  33. @classmethod
  34. def _prepare_relational_fields(cls, field):
  35. return lecrud.LeApiQueryError("Relational field '%s' given but %s doesn't is not a LeObject" % (field,
  36. cls.__name__))
  37. @classmethod
  38. def _prepare_filters(cls, filters_l):
  39. filters = list()
  40. res = list()
  41. rel = list()
  42. for filter_item in filters_l:
  43. if isinstance(filter_item, tuple):
  44. filters.append(filter_item)
  45. else:
  46. filter_item_data = filter_item.split(" ")
  47. if len(filter_item_data) == 3:
  48. if filter_item_data[0] in cls._lesub_fieldtype.keys():
  49. filter_item_data[2] = cls._lesub_fieldtype[filter_item_data[0]].construct_data(
  50. cls,
  51. filter_item_data[0],
  52. {filter_item_data[0]: int(filter_item_data[2])}
  53. )
  54. elif filter_item_data[0] in cls._lesup_fieldtype.keys():
  55. filter_item_data[2] = cls._lesup_fieldtype[filter_item_data[0]].construct_data(
  56. cls,
  57. filter_item_data[0],
  58. {filter_item_data[0]: int(filter_item_data[2])}
  59. )
  60. filters.append(tuple(filter_item_data))
  61. for field, operator, value in filters:
  62. if field.startswith('superior') or field.startswith('subordinate'):
  63. rel.append((field, operator, value))
  64. else:
  65. res.append((field, operator, value))
  66. return (res, rel)
  67. @classmethod
  68. ## @brief deletes a relation between two objects
  69. # @param filters_list list
  70. # @param target_class str
  71. def delete(cls, filters_list, target_class):
  72. filters, rel_filters = cls._prepare_filters(filters_list)
  73. ret = cls._datasource.delete(target_class, filters)
  74. return True if ret == 1 else False
  75. ## @brief Abstract class to handle hierarchy relations
  76. class _LeHierarch(_LeRelation):
  77. def __init__(self, rel_id):
  78. pass
  79. ## @brief Abstract class to handle rel2type relations
  80. class _LeRel2Type(_LeRelation):
  81. ## @brief Stores the list of fieldtypes handling relations attributes
  82. _rel_attr_fieldtypes = dict()
  83. pass