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

30 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 18:38 +0100

1from portality.lib import dates 

2 

3class QueryBuilder(object): 

4 def __init__(self): 

5 self.fields = [] 

6 

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

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

9 

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

11 value = fro 

12 if to is not None: 

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

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

15 

16 def to_url_query_param(self): 

17 q = "" 

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

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

20 if q != "": 

21 q += " " 

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

23 return q 

24 

25#################################################### 

26## Some utility functions which will return potted 

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

28 

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

30 """ 

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

32 updated between the supplied dates 

33 

34 :param issn: ISSN of the journal 

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

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

37 :return: 

38 """ 

39 

40 fro = dates.reformat(fro, out_format="%Y-%m-%d") 

41 if to is not None: 

42 to = dates.reformat(to, out_format="%Y-%m-%d") 

43 

44 qb = QueryBuilder() 

45 qb.add_string_field("ISSN", issn) 

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

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

48 if date_sort: 

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

50 return qb