From 0576fff36c5e1bad7168a9f27c0ecdaa27d8c572 Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Fri, 22 Nov 2024 18:08:57 +0800 Subject: [PATCH] fix: Fix the uncaught exception when face capture fails --- .../authentication/face_capture.html | 2 +- apps/users/views/profile/face.py | 25 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/apps/authentication/templates/authentication/face_capture.html b/apps/authentication/templates/authentication/face_capture.html index f04716a59..a6342bace 100644 --- a/apps/authentication/templates/authentication/face_capture.html +++ b/apps/authentication/templates/authentication/face_capture.html @@ -85,7 +85,7 @@ } $('#retry_button').on('click', function () { - window.location.href = "{% url 'authentication:login-face-capture' %}"; + window.location.href = "{{ request.get_full_path }}"; }); }); diff --git a/apps/users/views/profile/face.py b/apps/users/views/profile/face.py index 6eb63f95f..62c3af38d 100644 --- a/apps/users/views/profile/face.py +++ b/apps/users/views/profile/face.py @@ -27,24 +27,31 @@ class UserFaceCaptureView(AuthMixin, FormView): return super().form_valid(form) def get_context_data(self, **kwargs): - context = super().get_context_data() + context = super().get_context_data(**kwargs) - if not self.get_form().is_bound: + if 'form' not in kwargs: + form = self.get_form() + context['form'] = form + + if not context['form'].is_bound: context.update({ "active": True, }) - kwargs.update(context) - return kwargs + return context -class UserFaceEnableView(UserFaceCaptureView, MFAFaceMixin): +class UserFaceEnableView(MFAFaceMixin, UserFaceCaptureView): def form_valid(self, form): - code = self.get_face_code() - user = self.get_user_from_session() - user.face_vector = code - user.save(update_fields=['face_vector']) + try: + code = self.get_face_code() + user = self.get_user_from_session() + user.face_vector = code + user.save(update_fields=['face_vector']) + except Exception as e: + form.add_error("code", str(e)) + return super().form_invalid(form) auth_logout(self.request) return super().form_valid(form)