Coverage for src/oai/views.py: 60%

66 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-05 09:56 +0000

1import importlib 

2from datetime import datetime 

3 

4from django.conf import settings 

5from django.http import HttpResponseServerError 

6from django.shortcuts import render 

7from django.utils.decorators import method_decorator 

8from django.views.decorators.csrf import csrf_exempt 

9from django.views.generic import View 

10 

11from .protocol import argdescrs 

12 

13try: 

14 REPOSITORY_MODULE = importlib.import_module(settings.OAI_REPOSITORY) 

15except Exception: 

16 REPOSITORY_MODULE = None 

17 

18 

19@method_decorator(csrf_exempt, name="dispatch") 

20class OAIView(View): 

21 def post(self, request, *args, **kwargs): 

22 request.REQUEST = request.POST 

23 return self.oai(request) 

24 

25 def get(self, request, *args, **kwargs): 

26 request.REQUEST = request.GET 

27 return self.oai(request) 

28 

29 def oai(self, request): 

30 if REPOSITORY_MODULE is None: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true

31 return HttpResponseServerError("No OAI repository configured") 

32 code = "200 OK" 

33 status = 200 

34 message = "" 

35 if "verb" in request.REQUEST: 35 ↛ 50line 35 didn't jump to line 50 because the condition on line 35 was always true

36 verb = request.REQUEST["verb"] 

37 try: 

38 argdescr, klass = argdescrs[verb] 

39 

40 except KeyError: 

41 code = "badVerb" 

42 status = 405 

43 message = "Illegal or missing OAI verb" 

44 pass 

45 else: 

46 base_url = settings.SITE_DOMAIN 

47 repo = REPOSITORY_MODULE.get_repository(base_url) 

48 view = klass(verb, argdescr, repo) 

49 return view(request) 

50 if status == 405: 50 ↛ 53line 50 didn't jump to line 53 because the condition on line 50 was always true

51 return error(request, code=code, message=message, status=status) 

52 else: 

53 return success(request, code=code, message=message, status=status) 

54 

55 # TODO : j ai l impression que le meme code existe dans protocole.py 

56 

57 

58def error(request, code, message, status): 

59 context = { 

60 "request": request, 

61 "code": code, 

62 "message": message, 

63 } 

64 

65 return render( 

66 request=request, 

67 template_name="oai/error.html", 

68 context=context, 

69 content_type="text/xml; charset=UTF-8", 

70 status=status, 

71 ) 

72 

73 

74def success(request, code, message, status): 

75 base_url = settings.SITE_DOMAIN 

76 

77 repo = REPOSITORY_MODULE.get_repository(base_url) 

78 identify = repo.Identify() 

79 granularity = identify["granularity"] 

80 granularity_format = "Y-m-d" 

81 format_date = "%Y-%m-%d" 

82 if len(granularity) > 10: 

83 granularity_format = granularity_format + r"\TH:i:sZ" 

84 format_date = "%Y-%m-%dT%H:%M:%S.%fZ" 

85 

86 context = { 

87 "request": request, 

88 "code": code, 

89 "message": message, 

90 "granularity": granularity_format, 

91 "now": datetime.now().strftime(format_date), 

92 } 

93 

94 return render( 

95 request=request, 

96 template_name="oai/response.html", 

97 context=context, 

98 content_type="text/xml; charset=UTF-8", 

99 status=status, 

100 ) 

101 

102 

103def is_authorized(request): 

104 x_forwarded_for = request.headers.get("x-forwarded-for") 

105 if x_forwarded_for: 

106 ip_address = x_forwarded_for.split(",")[0] 

107 else: 

108 ip_address = request.META.get("REMOTE_ADDR") 

109 if ip_address in settings.EUDML_AUTHORIZED_IP: 

110 return True 

111 return False