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

39 statements  

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

1from io import BytesIO 

2 

3from portality.core import app 

4from portality.dao import DomainObject 

5from portality.lib import es_data_mapping 

6from portality.lib.coerce import COERCE_MAP 

7from portality.lib.ris import RisEntry 

8from portality.lib.seamless import SeamlessMixin 

9from portality.lib import dates 

10 

11STRUCT = { 

12 "fields": { 

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

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

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

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

17 "ris": {"coerce": "unicode"}, 

18 } 

19} 

20 

21MAPPING_OPTS = { 

22 "dynamic": None, 

23 "coerces": app.config["DATAOBJ_TO_MAPPING_DEFAULTS"], 

24 "exceptions": { 

25 # The RIS text is only ever read back from _source by id, never searched, so don't index it: the default 

26 # unicode mapping (text + stored keyword "exact") kept it three times over (~90GB primary for 16.6M articles) 

27 "ris": {"type": "text", "index": False} 

28 } 

29} 

30 

31class RISExport(SeamlessMixin, DomainObject): 

32 __type__ = "ris_export" 

33 

34 __SEAMLESS_STRUCT__ = STRUCT 

35 __SEAMLESS_COERCE__ = COERCE_MAP 

36 

37 def __init__(self, **kwargs): 

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

39 if "_source" in kwargs: 

40 kwargs = kwargs["_source"] 

41 super(RISExport, self).__init__(raw=kwargs) 

42 

43 def mappings(self): 

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

45 

46 @property 

47 def data(self): 

48 return self.__seamless__.data 

49 

50 @property 

51 def ris_raw(self): 

52 return self.__seamless__.get_single("ris") 

53 

54 @ris_raw.setter 

55 def ris_raw(self, val): 

56 if isinstance(val, RisEntry): 

57 val = val.to_text() 

58 self.__seamless__.set_with_struct("ris", val) 

59 

60 @property 

61 def byte_stream(self): 

62 raw = self.ris_raw 

63 raw = raw.encode('utf-8', errors='ignore') 

64 bs = BytesIO() 

65 bs.write(raw) 

66 bs.seek(0) 

67 return bs