Coverage for portality/api/current/bulk/applications.py: 63%

43 statements  

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

1# ~~APIBulkApplications:Feature->APIBulk:Feature~~ 

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

3from portality.api.common import Api404Error, Api400Error, Api403Error 

4from portality.api.current.crud import ApplicationsCrudApi 

5from copy import deepcopy 

6 

7class ApplicationsBulkApi(CrudApi): 

8 

9 API_KEY_OPTIONAL = False 

10 

11 # ~~->Swagger:Feature~~ 

12 # ~~->API:Documentation~~ 

13 SWAG_TAG = 'Bulk API' 

14 SWAG_DELETE_PARAM = { 

15 "description": "<div class=\"search-query-docs\">List of DOAJ application IDs to be deleted. You must own all of the ids, and they must all not have entered the DOAJ workflow yet, or none of them will be processed.e.g. [4cf8b72139a749c88d043129f00e1b07, 8e896b60-35f1-4cd3-b3f9-07f7f29d8a98].</div>", 

16 "required": True, 

17 "schema": {"type" : "string"}, 

18 "name": "application_ids", 

19 "in": "body" 

20 } 

21 SWAG_APPLICATION_BODY_PARAM = { 

22 "description": "<div class=\"search-query-docs\">List of Application JSON objects that you would like to create. Each element of the list should comply with the schema displayed in the <a href=\"/api/docs#CRUD_Applications_get_api_application_application_id\"> GET (Retrieve) an application route</a>.</div>", 

23 "required": True, 

24 "schema": {"type" : "string"}, 

25 "name": "application_json", 

26 "in": "body" 

27 } 

28 

29 @classmethod 

30 def create_swag(cls): 

31 template = deepcopy(cls.SWAG_TEMPLATE) 

32 template['parameters'].append(cls.SWAG_APPLICATION_BODY_PARAM) 

33 template['responses']['201'] = cls.R201_BULK 

34 template['responses']['400'] = cls.R400 

35 return cls._build_swag_response(template) 

36 

37 @classmethod 

38 def create(cls, applications, account): 

39 # we run through create twice, once as a dry-run and the second time 

40 # as the real deal 

41 # ~~->APICrudApplications:Feature~~ 

42 for a in applications: 

43 ApplicationsCrudApi.create(a, account, dry_run=True) 

44 

45 ids = [] 

46 for a in applications: 

47 n = ApplicationsCrudApi.create(a, account) 

48 ids.append(n.id) 

49 

50 return ids 

51 

52 @classmethod 

53 def delete_swag(cls): 

54 template = deepcopy(cls.SWAG_TEMPLATE) 

55 template['parameters'].append(cls.SWAG_DELETE_PARAM) 

56 template['responses']['204'] = cls.R204 

57 template['responses']['400'] = cls.R400 

58 return cls._build_swag_response(template) 

59 

60 @classmethod 

61 def delete(cls, application_ids, account): 

62 # we run through delete twice, once as a dry-run and the second time 

63 # as the real deal 

64 # ~~->APICrudApplications:Feature~~ 

65 for id in application_ids: 

66 try: 

67 ApplicationsCrudApi.delete(id, account, dry_run=True) 

68 except Api404Error as e: 

69 raise Api400Error("Id {x} does not exist or does not belong to this user account".format(x=id)) 

70 except Api403Error as e: 

71 raise Api400Error("Id {x} is not in a state which allows it to be deleted".format(x=id)) 

72 

73 for id in application_ids: 

74 ApplicationsCrudApi.delete(id, account)