Coverage for portality / tasks / harvester_helpers / epmc / queries.py: 94%

31 statements  

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

1from portality.lib import dates 

2from portality.lib.dates import FMT_DATE_STD 

3 

4 

5class QueryBuilder(object): 

6 def __init__(self): 

7 self.fields = [] 

8 

9 def add_string_field(self, field, value, fuzzy=False): 

10 self.fields.append((field, value, fuzzy)) 

11 

12 def add_date_field(self, field, fro, to=None): 

13 value = fro 

14 if to is not None: 

15 value = "[" + fro + " TO " + to + "]" 

16 self.fields.append((field, value, True)) 

17 

18 def to_url_query_param(self): 

19 q = "" 

20 for field, value, fuzzy in self.fields: 

21 wrap = "\"" if not fuzzy else "" 

22 if q != "": 

23 q += " " 

24 q += field + ":" + wrap + value + wrap 

25 return q 

26 

27#################################################### 

28## Some utility functions which will return potted 

29## queries for common things you might want to know 

30 

31def oa_issn_updated(issn, fro, to=None, date_sort=False): 

32 """ 

33 Query by ISSN for articles in the OA subset which have been 

34 updated between the supplied dates 

35 

36 :param issn: ISSN of the journal 

37 :param fro: updated since (YYYY-MM-DD) 

38 :param to: updated before (YYYY-MM-DD) 

39 :return: 

40 """ 

41 

42 fro = dates.reformat(fro, out_format=FMT_DATE_STD) 

43 if to is not None: 

44 to = dates.reformat(to, out_format=FMT_DATE_STD) 

45 

46 qb = QueryBuilder() 

47 qb.add_string_field("ISSN", issn) 

48 qb.add_string_field("OPEN_ACCESS", "y") 

49 qb.add_date_field("UPDATE_DATE", fro, to) 

50 if date_sort: 

51 qb.add_string_field("sort_date", "y") 

52 return qb