Fix payment view

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-02-11 22:59:25 +01:00
parent c1ce7cb70f
commit 672529382d
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 9 additions and 2 deletions

View File

@ -228,6 +228,7 @@ class PaymentForm(forms.ModelForm):
self.fields["valid"].widget.choices[0] = ('unknown', _("Pending"))
def clean_scholarship_file(self):
print(self.files)
if "scholarship_file" in self.files:
file = self.files["scholarship_file"]
if file.size > 2e6:
@ -240,7 +241,7 @@ class PaymentForm(forms.ModelForm):
cleaned_data = super().clean()
if "type" in cleaned_data and cleaned_data["type"] == "scholarship" \
and "scholarship_file" not in cleaned_data and not self.instance.scholarship_file:
and "scholarship_file" not in self.files and not self.instance.scholarship_file:
self.add_error("scholarship_file", _("You must upload your scholarship attestation."))
return cleaned_data

View File

@ -456,13 +456,19 @@ class PaymentUpdateView(LoginRequiredMixin, UpdateView):
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]
from django.forms.widgets import Select
widget: Select
form.fields["type"].widget.choices = list(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
old_instance = Payment.objects.get(pk=self.object.pk)
if old_instance.scholarship_file:
old_instance.scholarship_file.delete()
old_instance.save()
return super().form_valid(form)