88from rest_framework import exceptions , status
99from rest_framework .views import APIView
1010
11- from apps .creds .utils import get_creds_by_obj , get_url
11+ from apps .creds .utils import get_creds_by_obj
1212from .models import CredentialingReqest
1313
1414Application = get_application_model ()
@@ -19,20 +19,22 @@ def get(self, request, *args, **kwargs):
1919 logger = logging .getLogger (logging .AUDIT_CREDS_REQUEST_LOGGER , request )
2020
2121 creds_req_id = kwargs .get ("prod_cred_req_id" )
22-
2322 creds_req = self ._get_creds_req (creds_req_id )
2423
2524 # check if expired
2625 if self ._is_expired (creds_req ):
27- raise exceptions .PermissionDenied ("Generated credentialing request expired." , code = status .HTTP_403_FORBIDDEN )
26+ raise exceptions .PermissionDenied (
27+ "Generated credentialing request expired." ,
28+ code = status .HTTP_403_FORBIDDEN ,
29+ )
2830
2931 creds_dict = get_creds_by_obj (creds_req )
3032 # fetch creds request and update visits count and relevant timestamps
3133 creds_req .visits_count = creds_req .visits_count + 1
3234 creds_req .last_visit = datetime .datetime .now (datetime .timezone .utc )
3335
34- ctx = { "fetch_creds_link" : get_url ( creds_req_id )}
35- ctx .update (creds_dict )
36+ ctx = creds_dict
37+ ctx .update ({ "creds_req_id" : creds_req_id } )
3638
3739 log_dict = {
3840 "type" : "credentials request" ,
@@ -50,8 +52,10 @@ def get(self, request, *args, **kwargs):
5052 log_dict .update (action = action )
5153 else :
5254 # already fetched, fetch again forbidden
53- raise exceptions .PermissionDenied ("Credentials already fetched (download), doing it again not allowed." ,
54- code = status .HTTP_403_FORBIDDEN )
55+ raise exceptions .PermissionDenied (
56+ "Credentials already fetched (download), doing it again not allowed." ,
57+ code = status .HTTP_403_FORBIDDEN ,
58+ )
5559 else :
5660 # do not give out creds yet if not a fetch request
5761 if "client_id" in ctx :
@@ -65,20 +69,28 @@ def get(self, request, *args, **kwargs):
6569
6670 if action == "download" :
6771 response = JsonResponse (creds_dict )
68- response ['Content-Disposition' ] = 'attachment; filename="{}.json"' .format (creds_req_id )
72+ response ["Content-Disposition" ] = 'attachment; filename="{}.json"' .format (
73+ creds_req_id
74+ )
6975 return response
7076 else :
71- return render (request , ' get_creds.html' , ctx )
77+ return render (request , " get_creds.html" , ctx )
7278
7379 def _is_expired (self , creds_req ):
74- t_elapsed_since_created = datetime .datetime .now (datetime .timezone .utc ) - creds_req .created_at
75- return t_elapsed_since_created .seconds > settings .CREDENTIALS_REQUEST_URL_TTL * 60
80+ t_elapsed_since_created = (
81+ datetime .datetime .now (datetime .timezone .utc ) - creds_req .created_at
82+ )
83+ return (
84+ t_elapsed_since_created .seconds > settings .CREDENTIALS_REQUEST_URL_TTL * 60
85+ )
7686
7787 def _get_creds_req (self , id ):
7888
7989 if not id :
8090 # bad request
81- raise exceptions .ValidationError ("Credentialing request ID missing." , code = status .HTTP_400_BAD_REQUEST )
91+ raise exceptions .ValidationError (
92+ "Credentialing request ID missing." , code = status .HTTP_400_BAD_REQUEST
93+ )
8294
8395 creds_req = None
8496
0 commit comments