From 4cd1e43564efcb8bdc064fb079294d95ee37756c Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 18 Jan 2021 20:02:49 +0100 Subject: [PATCH] It is possible to validate payment status --- apps/registration/forms.py | 33 ++++++- apps/registration/models.py | 3 + .../templates/registration/payment_form.html | 14 +++ .../templates/registration/user_detail.html | 88 ++++++++++++------- apps/registration/urls.py | 9 +- apps/registration/views.py | 32 ++++++- 6 files changed, 140 insertions(+), 39 deletions(-) create mode 100644 apps/registration/templates/registration/payment_form.html diff --git a/apps/registration/forms.py b/apps/registration/forms.py index ad28483..ac4650b 100644 --- a/apps/registration/forms.py +++ b/apps/registration/forms.py @@ -8,7 +8,7 @@ from django.core.exceptions import ValidationError from django.forms import FileInput from django.utils.translation import gettext_lazy as _ -from .models import AdminRegistration, CoachRegistration, StudentRegistration, VolunteerRegistration +from .models import AdminRegistration, CoachRegistration, Payment, StudentRegistration, VolunteerRegistration class SignupForm(UserCreationForm): @@ -197,3 +197,34 @@ class AdminRegistrationForm(forms.ModelForm): class Meta: model = AdminRegistration fields = ('role', 'give_contact_to_animath', 'email_confirmed',) + + +class PaymentForm(forms.ModelForm): + """ + Indicate payment information + """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields["valid"].widget.choices[0] = ('unknown', _("Pending")) + + def clean_scholarship_file(self): + if "scholarship_file" in self.files: + file = self.files["scholarship_file"] + if file.size > 2e6: + raise ValidationError(_("The uploaded file size must be under 2 Mo.")) + if file.content_type not in ["application/pdf", "image/png", "image/jpeg"]: + raise ValidationError(_("The uploaded file must be a PDF, PNG of JPEG file.")) + return self.cleaned_data["scholarship_file"] + + def clean(self): + cleaned_data = super().clean() + + if "type" in cleaned_data and cleaned_data["type"] == "scholarship" \ + and "scholarship" not in cleaned_data and not self.instance.scholarship_file: + self.add_error("scholarship_file", _("You must upload your scholarship attestation.")) + + return cleaned_data + + class Meta: + model = Payment + fields = ('type', 'scholarship_file', 'additional_information', 'valid',) diff --git a/apps/registration/models.py b/apps/registration/models.py index 18dc200..e45d61f 100644 --- a/apps/registration/models.py +++ b/apps/registration/models.py @@ -338,6 +338,9 @@ class Payment(models.Model): default=False, ) + def get_absolute_url(self): + return reverse_lazy("registration:user_detail", args=(self.registration.user.id,)) + class Meta: verbose_name = _("payment") verbose_name_plural = _("payments") diff --git a/apps/registration/templates/registration/payment_form.html b/apps/registration/templates/registration/payment_form.html new file mode 100644 index 0000000..bd8db0d --- /dev/null +++ b/apps/registration/templates/registration/payment_form.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% load crispy_forms_filters i18n %} + +{% block content %} +
+
+ {% csrf_token %} + {{ form|crispy }} +
+ +
+{% endblock content %} + diff --git a/apps/registration/templates/registration/user_detail.html b/apps/registration/templates/registration/user_detail.html index 39be1d4..d7310b8 100644 --- a/apps/registration/templates/registration/user_detail.html +++ b/apps/registration/templates/registration/user_detail.html @@ -53,7 +53,7 @@ {% if user_object.registration.photo_authorization %} {% trans "Download" %} {% endif %} - {% if user_object.pk == user.pk %} + {% if user_object.registration.team and not user_object.registration.team.participation.valid %} {% endif %} @@ -63,7 +63,7 @@ {% if user_object.registration.health_sheet %} {% trans "Download" %} {% endif %} - {% if user_object.pk == user.pk %} + {% if user_object.registration.team and not user_object.registration.team.participation.valid %} {% endif %} @@ -76,7 +76,7 @@ {% if user_object.registration.parental_authorization %} {% trans "Download" %} {% endif %} - {% if user_object.pk == user.pk %} + {% if user_object.registration.team and not user_object.registration.team.participation.valid %} {% endif %} @@ -125,6 +125,11 @@ {% else %} {{ user_object.registration.payment.get_type_display }}, {% trans "valid:" %} {{ user_object.registration.payment.valid|yesno:yesnodefault }} {% endif %} + {% if user.registration.is_admin or user_object.registration.payment.valid is False %} + + {% endif %} {% endwith %} @@ -145,20 +150,34 @@ {% url "registration:update_user" pk=user_object.pk as modal_action %} {% include "base_modal.html" with modal_id="updateUser" %} - {% trans "Upload photo authorization" as modal_title %} - {% trans "Upload" as modal_button %} - {% url "registration:upload_user_photo_authorization" pk=user_object.registration.pk as modal_action %} - {% include "base_modal.html" with modal_id="uploadPhotoAuthorization" modal_enctype="multipart/form-data" %} + {% if user_object.registration.team and not user_object.registration.team.participation.valid %} + {% trans "Upload photo authorization" as modal_title %} + {% trans "Upload" as modal_button %} + {% url "registration:upload_user_photo_authorization" pk=user_object.registration.pk as modal_action %} + {% include "base_modal.html" with modal_id="uploadPhotoAuthorization" modal_enctype="multipart/form-data" %} - {% trans "Upload health sheet" as modal_title %} - {% trans "Upload" as modal_button %} - {% url "registration:upload_user_health_sheet" pk=user_object.registration.pk as modal_action %} - {% include "base_modal.html" with modal_id="uploadHealthSheet" modal_enctype="multipart/form-data" %} + {% trans "Upload health sheet" as modal_title %} + {% trans "Upload" as modal_button %} + {% url "registration:upload_user_health_sheet" pk=user_object.registration.pk as modal_action %} + {% include "base_modal.html" with modal_id="uploadHealthSheet" modal_enctype="multipart/form-data" %} - {% trans "Upload parental authorization" as modal_title %} - {% trans "Upload" as modal_button %} - {% url "registration:upload_user_parental_authorization" pk=user_object.registration.pk as modal_action %} - {% include "base_modal.html" with modal_id="uploadParentalAuthorization" modal_enctype="multipart/form-data" %} + {% trans "Upload parental authorization" as modal_title %} + {% trans "Upload" as modal_button %} + {% url "registration:upload_user_parental_authorization" pk=user_object.registration.pk as modal_action %} + {% include "base_modal.html" with modal_id="uploadParentalAuthorization" modal_enctype="multipart/form-data" %} + + {% trans "Upload parental authorization" as modal_title %} + {% trans "Upload" as modal_button %} + {% url "registration:upload_user_parental_authorization" pk=user_object.registration.pk as modal_action %} + {% include "base_modal.html" with modal_id="uploadParentalAuthorization" modal_enctype="multipart/form-data" %} + {% endif %} + + {% if user_object.registration.team.participation.valid %} + {% trans "Update payment" as modal_title %} + {% trans "Update" as modal_button %} + {% url "registration:update_payment" pk=user_object.registration.payment.pk as modal_action %} + {% include "base_modal.html" with modal_id="updatePayment" modal_additional_class="modal-xl" modal_enctype="multipart/form-data" %} + {% endif %} {% endblock %} {% block extrajavascript %} @@ -169,21 +188,30 @@ if (!modalBody.html().trim()) modalBody.load("{% url "registration:update_user" pk=user_object.pk %} #form-content"); }); - $('button[data-target="#uploadPhotoAuthorizationModal"]').click(function() { - let modalBody = $("#uploadPhotoAuthorizationModal div.modal-body"); - if (!modalBody.html().trim()) - modalBody.load("{% url "registration:upload_user_photo_authorization" pk=user_object.registration.pk %} #form-content"); - }); - $('button[data-target="#uploadHealthSheetModal"]').click(function() { - let modalBody = $("#uploadHealthSheetModal div.modal-body"); - if (!modalBody.html().trim()) - modalBody.load("{% url "registration:upload_user_health_sheet" pk=user_object.registration.pk %} #form-content"); - }); - $('button[data-target="#uploadParentalAuthorizationModal"]').click(function() { - let modalBody = $("#uploadParentalAuthorizationModal div.modal-body"); - if (!modalBody.html().trim()) - modalBody.load("{% url "registration:upload_user_parental_authorization" pk=user_object.registration.pk %} #form-content"); - }); + {% if user_object.registration.team and not user_object.registration.team.participation.valid %} + $('button[data-target="#uploadPhotoAuthorizationModal"]').click(function() { + let modalBody = $("#uploadPhotoAuthorizationModal div.modal-body"); + if (!modalBody.html().trim()) + modalBody.load("{% url "registration:upload_user_photo_authorization" pk=user_object.registration.pk %} #form-content"); + }); + $('button[data-target="#uploadHealthSheetModal"]').click(function() { + let modalBody = $("#uploadHealthSheetModal div.modal-body"); + if (!modalBody.html().trim()) + modalBody.load("{% url "registration:upload_user_health_sheet" pk=user_object.registration.pk %} #form-content"); + }); + $('button[data-target="#uploadParentalAuthorizationModal"]').click(function() { + let modalBody = $("#uploadParentalAuthorizationModal div.modal-body"); + if (!modalBody.html().trim()) + modalBody.load("{% url "registration:upload_user_parental_authorization" pk=user_object.registration.pk %} #form-content"); + }); + {% endif %} + {% if user_object.registration.team.participation.valid %} + $('button[data-target="#updatePaymentModal"]').click(function() { + let modalBody = $("#updatePaymentModal div.modal-body"); + if (!modalBody.html().trim()) + modalBody.load("{% url "registration:update_payment" pk=user_object.registration.payment.pk %} #form-content"); + }); + {% endif %} }); {% endblock %} diff --git a/apps/registration/urls.py b/apps/registration/urls.py index 663efa9..e6ec201 100644 --- a/apps/registration/urls.py +++ b/apps/registration/urls.py @@ -3,10 +3,10 @@ from django.urls import path -from .views import AddOrganizerView, MyAccountDetailView, ResetAdminView, SignupView, UserDetailView,\ - UserImpersonateView, UserListView, UserResendValidationEmailView, UserUpdateView, UserUploadHealthSheetView, \ - UserUploadParentalAuthorizationView, UserUploadPhotoAuthorizationView, UserValidateView, \ - UserValidationEmailSentView +from .views import AddOrganizerView, MyAccountDetailView, PaymentUpdateView, ResetAdminView, SignupView, \ + UserDetailView, UserImpersonateView, UserListView, UserResendValidationEmailView, UserUpdateView, \ + UserUploadHealthSheetView, UserUploadParentalAuthorizationView, UserUploadPhotoAuthorizationView, \ + UserValidateView, UserValidationEmailSentView app_name = "registration" @@ -26,6 +26,7 @@ urlpatterns = [ name="upload_user_health_sheet"), path("user//upload-parental-authorization/", UserUploadParentalAuthorizationView.as_view(), name="upload_user_parental_authorization"), + path("update-payment//", PaymentUpdateView.as_view(), name="update_payment"), path("user//impersonate/", UserImpersonateView.as_view(), name="user_impersonate"), path("user/list/", UserListView.as_view(), name="user_list"), path("reset-admin/", ResetAdminView.as_view(), name="reset_admin"), diff --git a/apps/registration/views.py b/apps/registration/views.py index 37fd404..5b89d56 100644 --- a/apps/registration/views.py +++ b/apps/registration/views.py @@ -15,8 +15,7 @@ from django.shortcuts import redirect, resolve_url from django.template.loader import render_to_string from django.urls import reverse_lazy from django.utils.crypto import get_random_string -from django.utils.encoding import force_bytes -from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode +from django.utils.http import urlsafe_base64_decode from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, RedirectView, TemplateView, UpdateView, View from django_tables2 import SingleTableView @@ -26,9 +25,9 @@ from tfjm.tokens import email_validation_token from tfjm.views import AdminMixin, UserMixin, VolunteerMixin from .forms import AddOrganizerForm, AdminRegistrationForm, CoachRegistrationForm, HealthSheetForm, \ - ParentalAuthorizationForm, PhotoAuthorizationForm, SignupForm, StudentRegistrationForm, UserForm, \ + ParentalAuthorizationForm, PaymentForm, PhotoAuthorizationForm, SignupForm, StudentRegistrationForm, UserForm, \ VolunteerRegistrationForm -from .models import ParticipantRegistration, Registration, StudentRegistration +from .models import ParticipantRegistration, Payment, Registration, StudentRegistration from .tables import RegistrationTable @@ -381,6 +380,31 @@ class UserUploadParentalAuthorizationView(UserMixin, UpdateView): return reverse_lazy("registration:user_detail", args=(self.object.user.pk,)) +class PaymentUpdateView(LoginRequiredMixin, UpdateView): + model = Payment + form_class = PaymentForm + + def dispatch(self, request, *args, **kwargs): + if not self.request.user.is_authenticated or \ + not self.request.user.registration.is_admin \ + and (self.request.user != self.get_object().registration.user + or self.get_object().valid is not False): + return self.handle_no_permission() + return super().dispatch(request, *args, **kwargs) + + def get_form(self, form_class=None): + form = super().get_form(form_class) + if not self.request.user.registration.is_admin: + del form.fields["type"].widget.choices[-1] + del form.fields["valid"] + return form + + def form_valid(self, form): + if not self.request.user.registration.is_admin: + form.instance.valid = None + return super().form_valid(form) + + class PhotoAuthorizationView(LoginRequiredMixin, View): """ Display the sent photo authorization.