Coverage for portality / models / ur_review_route.py: 100%

58 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-05 00:09 +0100

1from portality.core import app 

2from portality.dao import DomainObject 

3from portality.lib import es_data_mapping 

4from portality.lib.coerce import COERCE_MAP 

5from portality.lib.seamless import SeamlessMixin 

6 

7STRUCT = { 

8 "fields": { 

9 "id": {"coerce": "unicode"}, 

10 "created_date": {"coerce": "utcdatetime"}, 

11 "last_updated": {"coerce": "utcdatetime"}, 

12 "es_type": {"coerce": "unicode"}, 

13 "account_id": {"coerce": "unicode"}, 

14 "country_code": {"coerce": "unicode"}, 

15 "country_name": {"coerce": "unicode"}, 

16 "target": {"coerce": "unicode"} 

17 } 

18} 

19 

20MAPPING_OPTS = { 

21 "dynamic": None, 

22 "coerces": app.config["DATAOBJ_TO_MAPPING_DEFAULTS"] 

23} 

24 

25class URReviewRoute(SeamlessMixin, DomainObject): 

26 __type__ = "ur_review_route" 

27 

28 __SEAMLESS_STRUCT__ = STRUCT 

29 __SEAMLESS_COERCE__ = COERCE_MAP 

30 

31 def __init__(self, **kwargs): 

32 # FIXME: hack, to deal with ES integration layer being improperly abstracted 

33 if "_source" in kwargs: 

34 kwargs = kwargs["_source"] 

35 super(URReviewRoute, self).__init__(raw=kwargs) 

36 

37 def mappings(self): 

38 return es_data_mapping.create_mapping(self.__seamless_struct__.raw, MAPPING_OPTS) 

39 

40 @classmethod 

41 def by_account(cls, account_id): 

42 """ 

43 Get a URReviewRoute by account id 

44 :param account_id: 

45 :return: 

46 """ 

47 q = { 

48 "query": { 

49 "term": { 

50 "account_id.exact": account_id 

51 } 

52 }, 

53 "sort": { 

54 "created_date": { 

55 "order": "desc" 

56 } 

57 }, 

58 "size": 1 

59 } 

60 res = cls.object_query(q) 

61 if res and len(res) > 0: 

62 return res[0] 

63 else: 

64 return None 

65 

66 @classmethod 

67 def by_country(cls, country_code): 

68 """ 

69 Get a URReviewRoute by country name 

70 :return: 

71 """ 

72 q = { 

73 "query": { 

74 "term": { 

75 "country_code.exact": country_code 

76 } 

77 }, 

78 "sort": { 

79 "created_date": { 

80 "order": "desc" 

81 } 

82 }, 

83 "size": 1 

84 } 

85 res = cls.object_query(q) 

86 if res and len(res) > 0: 

87 return res[0] 

88 else: 

89 return None 

90 

91 @property 

92 def data(self): 

93 return self.__seamless__.data 

94 

95 @property 

96 def account_id(self): 

97 return self.__seamless__.get_single("account_id") 

98 

99 @account_id.setter 

100 def account_id(self, val): 

101 self.__seamless__.set_with_struct("account_id", val) 

102 

103 @property 

104 def country_code(self): 

105 return self.__seamless__.get_single("country_code") 

106 

107 @country_code.setter 

108 def country_code(self, val): 

109 self.__seamless__.set_with_struct("country_code", val) 

110 

111 @property 

112 def country_name(self): 

113 return self.__seamless__.get_single("country_name") 

114 

115 @country_name.setter 

116 def country_name(self, val): 

117 self.__seamless__.set_with_struct("country_name", val) 

118 

119 @property 

120 def target(self): 

121 return self.__seamless__.get_single("target") 

122 

123 @target.setter 

124 def target(self, val): 

125 self.__seamless__.set_with_struct("target", val)