Coverage for portality/api/current/crud/journals.py: 69%

29 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-20 16:12 +0100

1# ~~APICrudJournals:Feature->APICrud:Feature~~ 

2from portality import models 

3from portality.api.current.crud.common import CrudApi 

4from portality.api.current.data_objects.journal import OutgoingJournal, JOURNAL_STRUCT 

5from portality.api.common import Api400Error, Api401Error, Api404Error 

6 

7from copy import deepcopy 

8 

9 

10class JournalsCrudApi(CrudApi): 

11 

12 API_KEY_OPTIONAL = True 

13 

14 # ~~->Swagger:Feature~~ 

15 # ~~->API:Documentation~~ 

16 SWAG_TAG = 'CRUD Journals' 

17 SWAG_ID_PARAM = { 

18 "description": "<div class=\"search-query-docs\">DOAJ journal ID. E.g. 4cf8b72139a749c88d043129f00e1b07 .</div>", 

19 "required": True, 

20 "type": "string", 

21 "name": "journal_id", 

22 "in": "path" 

23 } 

24 

25 @classmethod 

26 def retrieve_swag(cls): 

27 template = deepcopy(cls.SWAG_TEMPLATE) 

28 template["parameters"].append(cls.SWAG_ID_PARAM) 

29 template['responses']['200'] = cls.R200 

30 template['responses']['200']['schema'] = OutgoingJournal().struct_to_swag(schema_title='Journal schema', 

31 struct=JOURNAL_STRUCT) 

32 template['responses']['200']['description'] = 'Detailed documentation on the response format is available <a ' \ 

33 'href="https://doaj.github.io/doaj-docs/master/data_models/OutgoingAPIJournal">here</a> ' 

34 template['responses']['404'] = cls.R404 

35 return cls._build_swag_response(template, api_key_override=False) 

36 

37 @classmethod 

38 def retrieve(cls, jid, account): 

39 # is the journal id valid 

40 try: 

41 j = models.Journal.pull(jid) # ~~->Journal:Model~~ 

42 except Exception as e: 

43 raise Api400Error(str(e)) 

44 if j is None: 

45 raise Api404Error() 

46 

47 # at this point we're happy to return the journal if it's 

48 # meant to be seen by the public 

49 if j.is_in_doaj(): 

50 return OutgoingJournal.from_model(j) # ~~->APIOutgoingJournal:Model~~ 

51 

52 # if the journal is not Public then it doesn't matter who asks for it, it can't be retrieved 

53 raise Api404Error()