Coverage for portality/models/note.py: 87%

54 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

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 "note" : {"coerce" : "unicode"}, 

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

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

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

14 "author_id" : {"coerce" : "unicode"}, # account_id of the note's author 

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

16 "resource_id": {"coerce" : "unicode"}, 

17 } 

18} 

19 

20MAPPING_OPTS = { 

21 "dynamic": None, 

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

23} 

24 

25class Note(SeamlessMixin, DomainObject): 

26 __type__ = "note" 

27 

28 __SEAMLESS_STRUCT__ = STRUCT 

29 __SEAMLESS_COERCE__ = COERCE_MAP 

30 

31 @classmethod 

32 def all_by_resource(cls, resource_type, resource_id): 

33 q = NotesByResourceQuery(resource_type, resource_id) 

34 return [n for n in cls.iterate_unstable(q.query())] 

35 

36 def __init__(self, **kwargs): 

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

38 if "_source" in kwargs: 

39 kwargs = kwargs["_source"] 

40 super(Note, self).__init__(raw=kwargs) 

41 

42 def mappings(self): 

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

44 

45 @property 

46 def data(self): 

47 return self.__seamless__.data 

48 

49 @property 

50 def note(self): 

51 return self.__seamless__.get_single("note") 

52 

53 @note.setter 

54 def note(self, val): 

55 self.__seamless__.set_single("note", val) 

56 

57 @property 

58 def author_id(self): 

59 return self.__seamless__.get_single("author_id") 

60 

61 @author_id.setter 

62 def author_id(self, val): 

63 self.__seamless__.set_single("author_id", val) 

64 

65 @property 

66 def resource_type(self): 

67 return self.__seamless__.get_single("resource_type") 

68 

69 @resource_type.setter 

70 def resource_type(self, val): 

71 self.__seamless__.set_single("resource_type", val) 

72 

73 @property 

74 def resource_id(self): 

75 return self.__seamless__.get_single("resource_id") 

76 

77 @resource_id.setter 

78 def resource_id(self, val): 

79 self.__seamless__.set_single("resource_id", val) 

80 

81class NotesByResourceQuery: 

82 def __init__(self, resource_type, resource_id): 

83 self._resource_type = resource_type 

84 self._resource_id = resource_id 

85 

86 def query(self): 

87 return { 

88 "query": { 

89 "bool": { 

90 "must": [ 

91 {"term": {"resource_type": self._resource_type}}, 

92 {"term": {"resource_id": self._resource_id}} 

93 ] 

94 } 

95 } 

96 }