From 96ad5385b0c0da6789a7f75a8d58eecdaba54d3d Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 17 Apr 2020 00:48:54 +0200 Subject: [PATCH] Distinguish new and old members --- apps/registration/views.py | 17 ++++++++++++++--- apps/wei/forms.py | 1 - apps/wei/models.py | 15 +++++++-------- apps/wei/tables.py | 5 +---- apps/wei/views.py | 24 +++++++++++++++++++++--- templates/wei/weimembership_form.html | 11 ++++++++--- 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/apps/registration/views.py b/apps/registration/views.py index 31163524..6a9e91f4 100644 --- a/apps/registration/views.py +++ b/apps/registration/views.py @@ -5,7 +5,7 @@ from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.core.exceptions import ValidationError -from django.db.models import Q, BooleanField +from django.db.models import Q from django.shortcuts import resolve_url, redirect from django.urls import reverse_lazy from django.utils.http import urlsafe_base64_decode @@ -33,7 +33,6 @@ class UserCreateView(CreateView): """ form_class = SignUpForm - success_url = reverse_lazy('registration:email_validation_sent') template_name = 'registration/signup.html' second_form = ProfileForm @@ -46,6 +45,7 @@ class UserCreateView(CreateView): wei_form = WEIRegistrationForm() del wei_form.fields["user"] del wei_form.fields["caution_check"] + del wei_form.fields["first_year"] context["wei_form"] = wei_form context["wei_registration_form"] = WEISignupForm() @@ -62,6 +62,7 @@ class UserCreateView(CreateView): return self.form_invalid(form) wei_form = None + self.wei = False if "wei" in settings.INSTALLED_APPS: wei_signup_form = WEISignupForm(self.request.POST) @@ -70,10 +71,13 @@ class UserCreateView(CreateView): wei_form = WEIRegistrationForm(self.request.POST) del wei_form.fields["user"] del wei_form.fields["caution_check"] + del wei_form.fields["first_year"] if not wei_form.is_valid(): return self.form_invalid(wei_form) + self.wei = True + # Save the user and the profile user = form.save(commit=False) user.is_active = False @@ -87,15 +91,22 @@ class UserCreateView(CreateView): user.profile.send_email_validation_link() - if wei_form is not None: + if self.wei: wei_registration = wei_form.instance wei_registration.user = user wei_registration.wei = WEIClub.objects.order_by('date_start').last() wei_registration.caution_check = False + wei_registration.first_year = True wei_registration.save() return super().form_valid(form) + def get_success_url(self): + if self.wei: + return reverse_lazy('registration:email_validation_sent') # TODO Load WEI survey + else: + return reverse_lazy('registration:email_validation_sent') + class UserValidateView(TemplateView): """ diff --git a/apps/wei/forms.py b/apps/wei/forms.py index df403ffc..834f56f6 100644 --- a/apps/wei/forms.py +++ b/apps/wei/forms.py @@ -3,7 +3,6 @@ from django import forms from django.contrib.auth.models import User -from django.utils.translation import ugettext_lazy as _ from note_kfet.inputs import AmountInput, DatePickerInput, Autocomplete, ColorWidget from .models import WEIClub, WEIRegistration, Bus, BusTeam, WEIMembership, WEIRole diff --git a/apps/wei/models.py b/apps/wei/models.py index 4c32af4d..b45f31c3 100644 --- a/apps/wei/models.py +++ b/apps/wei/models.py @@ -7,7 +7,7 @@ from django.contrib.auth.models import User from django.db import models from django.utils.translation import gettext_lazy as _ from member.models import Role, Club, Membership -from note.models import NoteSpecial, MembershipTransaction +from note.models import MembershipTransaction class WEIClub(Club): @@ -188,6 +188,12 @@ class WEIRegistration(models.Model): verbose_name=_("Register on the mailing list to stay informed of the art events of the campus (1 mail/week)"), ) + first_year = models.BooleanField( + default=False, + verbose_name=_("first year"), + help_text=_("Tells if the user is new in the school.") + ) + information_json = models.TextField( default="{}", verbose_name=_("registration information"), @@ -210,13 +216,6 @@ class WEIRegistration(models.Model): """ self.information_json = json.dumps(information) - @property - def is_first_year(self): - """ - We assume that a user is a new member if it not fully registered yet. - """ - return not self.user.profile.registration_valid - def __str__(self): return str(self.user) diff --git a/apps/wei/tables.py b/apps/wei/tables.py index 457b2cf1..41df47a2 100644 --- a/apps/wei/tables.py +++ b/apps/wei/tables.py @@ -71,16 +71,13 @@ class WEIRegistrationTable(tables.Table): }, ) - def render_is_first_year(self, value): - return _("yes") if value else _("no") - class Meta: attrs = { 'class': 'table table-condensed table-striped table-hover' } model = WEIRegistration template_name = 'django_tables2/bootstrap4.html' - fields = ('user', 'is_first_year',) + fields = ('user', 'first_year',) row_attrs = { 'class': 'table-row', 'id': lambda record: "row-" + str(record.pk), diff --git a/apps/wei/views.py b/apps/wei/views.py index 93d89913..5952608b 100644 --- a/apps/wei/views.py +++ b/apps/wei/views.py @@ -16,7 +16,7 @@ from note.tables import HistoryTable from permission.backends import PermissionBackend from permission.views import ProtectQuerysetMixin -from .models import WEIClub, WEIRegistration, WEIMembership, Bus, BusTeam +from .models import WEIClub, WEIRegistration, WEIMembership, Bus, BusTeam, WEIRole from .forms import WEIForm, WEIRegistrationForm, BusForm, BusTeamForm, WEIMembershipForm from .tables import WEITable, WEIRegistrationTable, BusTable, BusTeamTable, WEIMembershipTable @@ -77,7 +77,10 @@ class WEIDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): context['member_list'] = membership_table pre_registrations = WEIRegistration.objects.filter( - PermissionBackend.filter_queryset(self.request.user, WEIRegistration, "view")).filter(membership=None) + PermissionBackend.filter_queryset(self.request.user, WEIRegistration, "view")).filter( + membership=None, + wei=club + ) pre_registrations_table = WEIRegistrationTable(data=pre_registrations, prefix="pre-registration-") pre_registrations_table.paginate(per_page=20, page=self.request.GET.get('membership-page', 1)) context['pre_registrations'] = pre_registrations_table @@ -258,10 +261,12 @@ class WEIRegisterView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): def get_form(self, form_class=None): form = super().get_form(form_class) form.fields["user"].initial = self.request.user + del form.fields["first_year"] return form def form_valid(self, form): form.instance.wei = WEIClub.objects.get(pk=self.kwargs["wei_pk"]) + form.instance.first_year = False return super().form_valid(form) def get_success_url(self): @@ -310,6 +315,12 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea return context + def get_form(self, form_class=None): + form = super().get_form(form_class) + if WEIRegistration.objects.get(pk=self.kwargs["pk"]).first_year: + del form.fields["roles"] + return form + def form_valid(self, form): """ Create membership, check that all is good, make transactions @@ -336,7 +347,7 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea _("This user don't have enough money to join this club, and can't have a negative balance.")) return super().form_invalid(form) - if not registration.caution_check and True: # TODO: Replace it with "is 2A+" + if not registration.caution_check and not registration.first_year: form.add_error('bus', _("This user didn't give her/his caution check.")) return super().form_invalid(form) @@ -347,6 +358,13 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea # Now, all is fine, the membership can be created. + if registration.first_year: + membership = form.instance + membership.save() + membership.refresh_from_db() + membership.roles.set(WEIRole.objects.filter(name="1A").all()) + membership.save() + return super().form_valid(form) def get_success_url(self): diff --git a/templates/wei/weimembership_form.html b/templates/wei/weimembership_form.html index c7add31f..df6c25ec 100644 --- a/templates/wei/weimembership_form.html +++ b/templates/wei/weimembership_form.html @@ -47,6 +47,9 @@
+
{% trans 'first year'|capfirst %}
+
{{ registration.first_year|yesno }}
+
{% trans 'gender'|capfirst %}
{{ registration.gender }}
@@ -74,8 +77,10 @@
{% trans 'Payment from Société générale' %}
{{ registration.soge_credit|yesno }}
-
{% trans 'caution check given'|capfirst %}
-
{{ registration.caution_check|yesno }}
+ {% if not registration.first_year %} +
{% trans 'caution check given'|capfirst %}
+
{{ registration.caution_check|yesno }}
+ {% endif %}