From 2d870c3eae327f6fb5547cd79df87000ab0e0a53 Mon Sep 17 00:00:00 2001 From: Noah Langenheim <104438947+Noah-marc@users.noreply.github.com> Date: Wed, 13 Mar 2024 21:21:47 +0100 Subject: [PATCH] Fixed alumni newsletter opt out without filling in extra information (#3643) * changed when fields are required * Clean up docstring --------- Co-authored-by: Dirk Doesburg --- website/members/forms.py | 23 ++++++++++++------- website/members/views.py | 5 ++++ .../templates/registrations/renewal.html | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/website/members/forms.py b/website/members/forms.py index ad2d85812..b963ec249 100644 --- a/website/members/forms.py +++ b/website/members/forms.py @@ -13,7 +13,12 @@ class ProfileForm(forms.ModelForm): - """Form with all the user editable fields of a Profile model.""" + """Form with all the user editable fields of a Profile model. + + If the profile is minimized, no fields are required, unless the `require_address` + keyword argument is True, which is set if a user is filling a minimized profile + in order to be able to create a Renewal. + """ birthday = forms.DateField() @@ -43,8 +48,9 @@ class Meta: ] model = Profile - def __init__(self, *args, **kwargs): + def __init__(self, *args, require_address=False, **kwargs): super().__init__(*args, **kwargs) + user = Member.objects.get(pk=kwargs["instance"].user_id) for field in [ "birthday", "address_street", @@ -53,16 +59,17 @@ def __init__(self, *args, **kwargs): "address_city", "address_country", ]: - self.fields[field].required = True + if require_address or not user.profile.is_minimized: + self.fields[field].required = True + else: + self.fields[field].required = False + if not kwargs["instance"].user.is_staff: self.fields["email_gsuite_only"].widget = self.fields[ "email_gsuite_only" ].hidden_widget() - if ( - not Member.objects.get(pk=kwargs["instance"].user_id).has_been_member() - and not kwargs["instance"].receive_oldmembers - ): + if not user.has_been_member() and not kwargs["instance"].receive_oldmembers: self.fields["receive_oldmembers"].disabled = True self.fields["receive_oldmembers"].help_text = ( "If you are a past member, receive emails about Thalia events aimed at alumni. " @@ -72,7 +79,7 @@ def __init__(self, *args, **kwargs): ) self.fields["birthday"].widget.input_type = "date" - if not Member.objects.get(pk=kwargs["instance"].user_id).profile.is_minimized: + if not user.profile.is_minimized: self.fields["birthday"].disabled = True self.render_app_specific_profile_form_fields() diff --git a/website/members/views.py b/website/members/views.py index 1d484e62b..fe5d367a2 100644 --- a/website/members/views.py +++ b/website/members/views.py @@ -182,6 +182,11 @@ class UserProfileUpdateView(RedirectURLMixin, SuccessMessageMixin, UpdateView): def get_object(self, queryset=None) -> Profile: return get_object_or_404(models.Profile, user=self.request.user) + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs["require_address"] = bool(self.request.GET.get("require_address", False)) + return kwargs + @method_decorator(login_required, "dispatch") class StatisticsView(TemplateView): diff --git a/website/registrations/templates/registrations/renewal.html b/website/registrations/templates/registrations/renewal.html index dfc367611..9aaffde68 100644 --- a/website/registrations/templates/registrations/renewal.html +++ b/website/registrations/templates/registrations/renewal.html @@ -187,7 +187,7 @@

{% trans "Membership history" %}

You have a minimized profile. You can't renew your membership until you have filled in all the required - fields. Click here to complete your profile. + fields. Click here to complete your profile.

{% endif %}