add api flow for webhook
This commit is contained in:
parent
59c99d8279
commit
082cf25283
|
@ -7,7 +7,7 @@ from django.template import loader
|
||||||
from django.core.mail import EmailMultiAlternatives
|
from django.core.mail import EmailMultiAlternatives
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.views.generic.edit import View, FormView
|
from django.views.generic.edit import View, FormView
|
||||||
from django.http import HttpResponse, Http404, JsonResponse
|
from django.http import HttpResponse, Http404, JsonResponse, QueryDict
|
||||||
from django.shortcuts import get_object_or_404, redirect
|
from django.shortcuts import get_object_or_404, redirect
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
@ -150,16 +150,10 @@ class VerifyView(View):
|
||||||
|
|
||||||
for user in User.objects.filter(is_admin=True):
|
for user in User.objects.filter(is_admin=True):
|
||||||
self.send_email(user)
|
self.send_email(user)
|
||||||
self.send_api()
|
|
||||||
|
|
||||||
response["response"] = "Validation Code {}".format(code)
|
response["response"] = "Validation Code {}".format(code)
|
||||||
return JsonResponse(response)
|
return JsonResponse(response)
|
||||||
|
|
||||||
def send_api(self):
|
|
||||||
data = {"vp": self.vp_token.vp_token, "code": self.vp_token.code}
|
|
||||||
url = self.vp_token.org.response_uri
|
|
||||||
requests.post(url, data=data)
|
|
||||||
|
|
||||||
def validate(self, request):
|
def validate(self, request):
|
||||||
auth_header = request.headers.get('Authorization', b'')
|
auth_header = request.headers.get('Authorization', b'')
|
||||||
auth_data = auth_header.split()
|
auth_data = auth_header.split()
|
||||||
|
@ -228,6 +222,7 @@ class VerifyView(View):
|
||||||
def get_verification(self):
|
def get_verification(self):
|
||||||
return self.vp_token.get_user_info_all()
|
return self.vp_token.get_user_info_all()
|
||||||
|
|
||||||
|
|
||||||
class AllowCodeView(View):
|
class AllowCodeView(View):
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
code = self.request.GET.get("code")
|
code = self.request.GET.get("code")
|
||||||
|
@ -239,6 +234,9 @@ class AllowCodeView(View):
|
||||||
code=code,
|
code=code,
|
||||||
code_used=False
|
code_used=False
|
||||||
)
|
)
|
||||||
|
if self.request.session.get("response_uri"):
|
||||||
|
url = self.send_api()
|
||||||
|
return redirect(url)
|
||||||
|
|
||||||
promotion = self.authorization.promotions.first()
|
promotion = self.authorization.promotions.first()
|
||||||
if not promotion:
|
if not promotion:
|
||||||
|
@ -246,6 +244,40 @@ class AllowCodeView(View):
|
||||||
|
|
||||||
return redirect(promotion.get_url(code))
|
return redirect(promotion.get_url(code))
|
||||||
|
|
||||||
|
def send_api(self):
|
||||||
|
vp = self.get_vp_token()
|
||||||
|
if not vp:
|
||||||
|
return
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"vp_token": vp,
|
||||||
|
"code": self.authorization.code
|
||||||
|
}
|
||||||
|
url = self.request.session.get("response_uri")
|
||||||
|
result = requests.post(url, data=data)
|
||||||
|
return result.json().get('redirect_uri')
|
||||||
|
|
||||||
|
def get_vp_token(self):
|
||||||
|
vp = self.authorization.vp_tokens.first()
|
||||||
|
if not vp:
|
||||||
|
return
|
||||||
|
return base64.b64encode(vp.vp_token.encode()).decode()
|
||||||
|
|
||||||
|
def get_response_uri(self):
|
||||||
|
data = {
|
||||||
|
"code": self.authorization.code,
|
||||||
|
}
|
||||||
|
query_dict = QueryDict('', mutable=True)
|
||||||
|
query_dict.update(data)
|
||||||
|
|
||||||
|
response_uri = self.request.session.get("response_uri")
|
||||||
|
|
||||||
|
url = '{response_uri}?{params}'.format(
|
||||||
|
response_uri=response_uri,
|
||||||
|
params=query_dict.urlencode()
|
||||||
|
)
|
||||||
|
return url
|
||||||
|
|
||||||
|
|
||||||
class ReceivedCodeView(View):
|
class ReceivedCodeView(View):
|
||||||
template_name = "received_code.html"
|
template_name = "received_code.html"
|
||||||
|
|
|
@ -39,6 +39,7 @@ class ContractView(FormView):
|
||||||
success_url = reverse_lazy('promotion:thanks')
|
success_url = reverse_lazy('promotion:thanks')
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
|
import pdb; pdb.set_trace()
|
||||||
self.context = super().get_context_data(**kwargs)
|
self.context = super().get_context_data(**kwargs)
|
||||||
code = self.request.GET.get("code")
|
code = self.request.GET.get("code")
|
||||||
self.get_discount(code)
|
self.get_discount(code)
|
||||||
|
@ -106,9 +107,13 @@ class SelectWalletView(FormView):
|
||||||
success_url = reverse_lazy('promotion:select_wallet')
|
success_url = reverse_lazy('promotion:select_wallet')
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
self.get_response_uri()
|
presentation = self.get_response_uri()
|
||||||
|
if not presentation:
|
||||||
|
presentation = json.dumps(
|
||||||
|
settings.SUPPORTED_CREDENTIALS
|
||||||
|
)
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
kwargs['presentation_definition'] = json.dumps(settings.SUPPORTED_CREDENTIALS)
|
kwargs['presentation_definition'] = presentation
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
|
@ -120,9 +125,14 @@ class SelectWalletView(FormView):
|
||||||
if len(path) < 2:
|
if len(path) < 2:
|
||||||
return
|
return
|
||||||
|
|
||||||
args = path[1]
|
args = dict(
|
||||||
response_uri = dict(
|
[x.split("=") for x in path[1].split("&")]
|
||||||
[x.split("=") for x in args.split("&")]
|
)
|
||||||
).get('response_uri')
|
response_uri = args.get('response_uri')
|
||||||
|
|
||||||
self.request.session["response_uri"] = response_uri
|
self.request.session["response_uri"] = response_uri
|
||||||
|
presentation = args.get('presentation_definition')
|
||||||
|
|
||||||
|
for x in settings.SUPPORTED_CREDENTIALS:
|
||||||
|
if x in presentation:
|
||||||
|
return json.dumps([x])
|
||||||
|
|
Loading…
Reference in New Issue