From 36b26e0baa847bd503a4c654f3492413b2ccee64 Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Tue, 14 Apr 2020 03:41:26 +0200
Subject: [PATCH] Validation form (only front)

---
 apps/member/forms.py                  |   4 +-
 apps/wei/admin.py                     |  11 +++
 apps/wei/forms.py                     |  26 ++++-
 apps/wei/models.py                    |  22 +++--
 apps/wei/tables.py                    |   2 +-
 apps/wei/urls.py                      |   3 +-
 apps/wei/views.py                     |  27 +++++-
 templates/member/club_info.html       |   4 +-
 templates/wei/weiclub_info.html       |  12 ++-
 templates/wei/weimembership_form.html | 133 ++++++++++++++++++++++++++
 10 files changed, 225 insertions(+), 19 deletions(-)
 create mode 100644 templates/wei/weimembership_form.html

diff --git a/apps/member/forms.py b/apps/member/forms.py
index 6fe95f5a..89ce3993 100644
--- a/apps/member/forms.py
+++ b/apps/member/forms.py
@@ -9,7 +9,7 @@ from note.models import NoteSpecial
 from note_kfet.inputs import Autocomplete, AmountInput, DatePickerInput
 from permission.models import PermissionMask
 
-from .models import Profile, Club, Membership
+from .models import Profile, Club, Membership, Role
 
 
 class CustomAuthenticationForm(AuthenticationForm):
@@ -50,6 +50,8 @@ class ClubForm(forms.ModelForm):
 
 
 class MembershipForm(forms.ModelForm):
+    roles = forms.ModelMultipleChoiceField(queryset=Role.objects.filter(weirole=None).all())
+
     soge = forms.BooleanField(
         label=_("Inscription paid by Société Générale"),
         required=False,
diff --git a/apps/wei/admin.py b/apps/wei/admin.py
index 4e945ad5..4ebcec3a 100644
--- a/apps/wei/admin.py
+++ b/apps/wei/admin.py
@@ -1,2 +1,13 @@
 # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
 # SPDX-License-Identifier: GPL-3.0-or-later
+
+from django.contrib import admin
+
+from .models import WEIClub, WEIRegistration, WEIRole, Bus, BusTeam
+
+
+admin.site.register(WEIClub)
+admin.site.register(WEIRegistration)
+admin.site.register(WEIRole)
+admin.site.register(Bus)
+admin.site.register(BusTeam)
diff --git a/apps/wei/forms.py b/apps/wei/forms.py
index e4816cc2..976284f2 100644
--- a/apps/wei/forms.py
+++ b/apps/wei/forms.py
@@ -6,7 +6,7 @@ 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
+from .models import WEIClub, WEIRegistration, Bus, BusTeam, WEIMembership, WEIRole
 
 
 class WEIForm(forms.ModelForm):
@@ -45,6 +45,30 @@ class WEIRegistrationForm(forms.ModelForm):
         }
 
 
+class WEIMembershipForm(forms.ModelForm):
+    roles = forms.ModelMultipleChoiceField(queryset=WEIRole.objects)
+
+    class Meta:
+        model = WEIMembership
+        fields = ('roles', 'bus', 'team',)
+        widgets = {
+            "bus": Autocomplete(
+                Bus,
+                attrs={
+                    'api_url': '/api/wei/bus/',
+                    'placeholder': 'Bus ...',
+                }
+            ),
+            "team": Autocomplete(
+                BusTeam,
+                attrs={
+                    'api_url': '/api/wei/team/',
+                    'placeholder': 'Équipe ...',
+                }
+            ),
+        }
+
+
 class BusForm(forms.ModelForm):
     class Meta:
         model = Bus
diff --git a/apps/wei/models.py b/apps/wei/models.py
index 634038bb..24a1667d 100644
--- a/apps/wei/models.py
+++ b/apps/wei/models.py
@@ -33,6 +33,10 @@ class WEIClub(Club):
         """
         return
 
+    class Meta:
+        verbose_name = _("WEI")
+        verbose_name_plural = _("WEI")
+
 
 class Bus(models.Model):
     """
@@ -60,6 +64,8 @@ class Bus(models.Model):
         return self.name
 
     class Meta:
+        verbose_name = _("Bus")
+        verbose_name_plural = _("Buses")
         unique_together = ('wei', 'name',)
 
 
@@ -102,14 +108,10 @@ class WEIRole(Role):
     """
     A Role for the WEI can be bus chief, team chief, free electron, ...
     """
-    bus = models.ForeignKey(
-        Bus,
-        on_delete=models.CASCADE,
-        null=True,
-        default=None,
-        related_name="roles",
-        verbose_name=_("bus"),
-    )
+
+    class Meta:
+        verbose_name = _("WEI Role")
+        verbose_name_plural = _("WEI Roles")
 
 
 class WEIRegistration(models.Model):
@@ -262,3 +264,7 @@ class WEIMembership(Membership):
         related_name="membership",
         verbose_name=_("WEI registration"),
     )
+
+    class Meta:
+        verbose_name = _("WEI membership")
+        verbose_name_plural = _("WEI memberships")
diff --git a/apps/wei/tables.py b/apps/wei/tables.py
index 69c7babd..457b2cf1 100644
--- a/apps/wei/tables.py
+++ b/apps/wei/tables.py
@@ -48,7 +48,7 @@ class WEIRegistrationTable(tables.Table):
         }
     )
     validate = tables.LinkColumn(
-        'wei:wei_detail',
+        'wei:validate_registration',
         args=[A('pk')],
         verbose_name=_("Validate"),
         text=_("Validate"),
diff --git a/apps/wei/urls.py b/apps/wei/urls.py
index ade2bae8..673a4d36 100644
--- a/apps/wei/urls.py
+++ b/apps/wei/urls.py
@@ -5,7 +5,7 @@ from django.urls import path
 
 from .views import WEIListView, WEICreateView, WEIDetailView, WEIUpdateView,\
     BusCreateView, BusManageView, BusUpdateView, BusTeamCreateView, BusTeamManageView, BusTeamUpdateView,\
-    WEIRegisterView, WEIUpdateRegistrationView
+    WEIRegisterView, WEIUpdateRegistrationView, WEIValidateRegistrationView
 
 
 app_name = 'wei'
@@ -22,4 +22,5 @@ urlpatterns = [
     path('update-bus-team/<int:pk>/', BusTeamUpdateView.as_view(), name="update_bus_team"),
     path('register/<int:wei_pk>/', WEIRegisterView.as_view(), name="wei_register"),
     path('edit-registration/<int:pk>/', WEIUpdateRegistrationView.as_view(), name="wei_update_registration"),
+    path('validate/<int:pk>/', WEIValidateRegistrationView.as_view(), name="validate_registration"),
 ]
diff --git a/apps/wei/views.py b/apps/wei/views.py
index 5ecd4195..d21b1680 100644
--- a/apps/wei/views.py
+++ b/apps/wei/views.py
@@ -16,7 +16,7 @@ from permission.backends import PermissionBackend
 from permission.views import ProtectQuerysetMixin
 
 from .models import WEIClub, WEIRegistration, WEIMembership, Bus, BusTeam
-from .forms import WEIForm, WEIRegistrationForm, BusForm, BusTeamForm
+from .forms import WEIForm, WEIRegistrationForm, BusForm, BusTeamForm, WEIMembershipForm
 from .tables import WEITable, WEIRegistrationTable, BusTable, BusTeamTable, WEIMembershipTable
 
 
@@ -257,6 +257,7 @@ 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["payment_method"]
         return form
 
     def form_valid(self, form):
@@ -278,8 +279,32 @@ class WEIUpdateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Update
     def get_form(self, form_class=None):
         form = super().get_form(form_class)
         del form.fields["user"]
+        del form.fields["payment_method"]
         return form
 
     def get_success_url(self):
         self.object.refresh_from_db()
         return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk})
+
+
+class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
+    """
+    Validate WEI Registration
+    """
+    model = WEIMembership
+    form_class = WEIMembershipForm
+
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data(**kwargs)
+
+        registration = WEIRegistration.objects.get(pk=self.kwargs["pk"])
+        context["registration"] = registration
+        context["club"] = registration.wei
+        context["fee"] = registration.wei.membership_fee_paid if registration.user.profile.paid \
+            else registration.wei.membership_fee_unpaid
+
+        return context
+
+    def get_success_url(self):
+        self.object.refresh_from_db()
+        return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk})
diff --git a/templates/member/club_info.html b/templates/member/club_info.html
index eeebc823..59259f10 100644
--- a/templates/member/club_info.html
+++ b/templates/member/club_info.html
@@ -42,11 +42,11 @@
 
             {% if "note.view_note"|has_perm:club.note %}
                 <dt class="col-xl-6">{% trans 'balance'|capfirst %}</dt>
-                <dd class="col-xl-6">{{ object.note.balance | pretty_money }}</dd>
+                <dd class="col-xl-6">{{ club.note.balance | pretty_money }}</dd>
             {% endif %}
             
             <dt class="col-xl-6"><a href="{% url 'member:club_alias' club.pk %}">{% trans 'aliases'|capfirst %}</a></dt>
-            <dd class="col-xl-6 text-truncate">{{ object.note.alias_set.all|join:", " }}</dd>
+            <dd class="col-xl-6 text-truncate">{{ club.note.alias_set.all|join:", " }}</dd>
 
             <dt class="col-xl-4">{% trans 'email'|capfirst %}</dt>
             <dd class="col-xl-8"><a href="mailto:{{ club.email }}">{{ club.email }}</a></dd>
diff --git a/templates/wei/weiclub_info.html b/templates/wei/weiclub_info.html
index 5e62fe29..1128c4ed 100644
--- a/templates/wei/weiclub_info.html
+++ b/templates/wei/weiclub_info.html
@@ -27,11 +27,15 @@
                     <dt class="col-xl-6">{% trans 'membership fee'|capfirst %}</dt>
                     <dd class="col-xl-6">{{ club.membership_fee_paid|pretty_money }}</dd>
                 {% else %}
-                    <dt class="col-xl-6">{% trans 'fee (paid students)'|capfirst %}</dt>
-                    <dd class="col-xl-6">{{ club.membership_fee_paid|pretty_money }}</dd>
+                    {% with bde_kfet_fee=club.parent_club.membership_fee_paid|add:club.parent_club.parent_club.membership_fee_paid %}
+                        <dt class="col-xl-6">{% trans 'WEI fee / including BDE and Kfet fee (paid students)'|capfirst %}</dt>
+                        <dd class="col-xl-6">{{ club.membership_fee_paid|pretty_money }} / {{ club.membership_fee_paid|add:bde_kfet_fee|pretty_money }}</dd>
+                    {% endwith %}
 
-                    <dt class="col-xl-6">{% trans 'fee (unpaid students)'|capfirst %}</dt>
-                    <dd class="col-xl-6">{{ club.membership_fee_unpaid|pretty_money }}</dd>
+                    {% with bde_kfet_fee=club.parent_club.membership_fee_unpaid|add:club.parent_club.parent_club.membership_fee_unpaid %}
+                        <dt class="col-xl-6">{% trans 'WEI fee / including BDE and Kfet fee (unpaid students)'|capfirst %}</dt>
+                        <dd class="col-xl-6">{{ club.membership_fee_unpaid|pretty_money }} / {{ club.membership_fee_unpaid|add:bde_kfet_fee|pretty_money }}</dd>
+                    {% endwith %}
                 {% endif %}
             {% endif %}
 
diff --git a/templates/wei/weimembership_form.html b/templates/wei/weimembership_form.html
new file mode 100644
index 00000000..1006bf78
--- /dev/null
+++ b/templates/wei/weimembership_form.html
@@ -0,0 +1,133 @@
+{% extends "member/noteowner_detail.html" %}
+{% load crispy_forms_tags %}
+{% load i18n %}
+{% load pretty_money %}
+{% load perms %}
+
+{% block profile_info %}
+    {% include "wei/weiclub_info.html" %}
+{% endblock %}
+
+{% block profile_content %}
+    <div class="card bg-light shadow">
+        <div class="card-header text-center" >
+            <h4>{% trans "Review registration" %}</h4>
+        </div>
+        <div class="card-body" id="profile_infos">
+            <dl class="row">
+                <dt class="col-xl-6">{% trans 'name'|capfirst %}, {% trans 'first name' %}</dt>
+                <dd class="col-xl-6">{{ registration.user.last_name }} {{ registration.user.first_name }}</dd>
+
+                <dt class="col-xl-6">{% trans 'username'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.user.username }}</dd>
+
+                <dt class="col-xl-6">{% trans 'email'|capfirst %}</dt>
+                <dd class="col-xl-6"><a href="mailto:{{ registration.user.email }}">{{ registration.user.email }}</a></dd>
+
+                {% if not registration.user.profile.email_confirmed and "member.change_profile_email_confirmed"|has_perm:registration.user.profile %}
+                    <dd class="col-xl-12">
+                        <div class="alert alert-warning">
+                            {% trans "This user doesn't have confirmed his/her e-mail address." %}
+                            <a href="{% url "registration:email_validation_resend" pk=registration.user.pk %}">{% trans "Click here to resend a validation link." %}</a>
+                        </div>
+                    </dd>
+                {% endif %}
+
+                <dt class="col-xl-6">{% trans 'section'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.user.profile.section }}</dd>
+
+                <dt class="col-xl-6">{% trans 'address'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.user.profile.address }}</dd>
+
+                <dt class="col-xl-6">{% trans 'phone number'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.user.profile.phone_number }}</dd>
+
+                <dt class="col-xl-6">{% trans 'paid'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.user.profile.paid|yesno }}</dd>
+
+                <hr>
+
+                <dt class="col-xl-6">{% trans 'gender'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.gender }}</dd>
+
+                <dt class="col-xl-6">{% trans 'birth date'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.birth_date }}</dd>
+
+                <dt class="col-xl-6">{% trans 'health issues'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.health_issues }}</dd>
+
+                <dt class="col-xl-6">{% trans 'emergency contact name'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.emergency_contact_name }}</dd>
+
+                <dt class="col-xl-6">{% trans 'emergency contact phone'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.emergency_contact_phone }}</dd>
+
+                <dt class="col-xl-6">{% trans 'Register on the mailing list to stay informed of the events of the campus (1 mail/week)' %}</dt>
+                <dd class="col-xl-6">{{ registration.ml_events_registration|yesno }}</dd>
+
+                <dt class="col-xl-6">{% trans 'Register on the mailing list to stay informed of the sport events of the campus (1 mail/week)' %}</dt>
+                <dd class="col-xl-6">{{ registration.ml_sport_registration|yesno }}</dd>
+
+                <dt class="col-xl-6">{% trans 'Register on the mailing list to stay informed of the art events of the campus (1 mail/week)' %}</dt>
+                <dd class="col-xl-6">{{ registration.ml_art_registration|yesno }}</dd>
+
+                <dt class="col-xl-6">{% trans 'Payment from Société générale' %}</dt>
+                <dd class="col-xl-6">{{ registration.soge_credit|yesno }}</dd>
+
+                <dt class="col-xl-6">{% trans 'caution check given'|capfirst %}</dt>
+                <dd class="col-xl-6">{{ registration.caution_check|yesno }}</dd>
+            </dl>
+        </div>
+        <div class="card-footer text-center">
+            <a class="btn btn-primary btn-sm" href="{% url 'member:user_update_profile' registration.user.pk %}">{% trans 'Update Profile' %}</a>
+            <a class="btn btn-primary btn-sm" href="{% url 'wei:wei_update_registration' registration.user.pk %}">{% trans 'Update registration' %}</a>
+        </div>
+    </div>
+
+    <hr>
+
+    <div class="card bg-light shadow">
+        <form method="post">
+            <div class="card-header text-center" >
+                <h4> {% trans "Validate registration" %}</h4>
+            </div>
+            {% if registration.soge_credit %}
+                <div class="alert alert-warning">
+                    {% blocktrans %}
+                        The WEI will be paid by Société générale. The membership will be created even if the bank didn't pay the BDE yet.
+                        The membership transaction will be created but will be invalid. You will have to validate it once the bank
+                        validated the creation of the account, or to change the payment method.
+                    {% endblocktrans %}
+                </div>
+            {% else %}
+                {% if registration.user.note.balance < fee %}
+                    <div class="alert alert-danger">
+                        {% with pretty_fee=fee|pretty_money %}
+                        {% blocktrans with balance=registration.user.note.balance|pretty_money %}
+                            The note don't have enough money ({{ balance }}, {{ pretty_fee }} required). The registration may fail.
+                        {% endblocktrans %}
+                        {% endwith %}
+                    </div>
+                {% else %}
+                    <div class="alert alert-success">
+                        {% trans "The note has enough money." %}
+                    </div>
+                {% endif %}
+            {% endif %}
+
+            {% if not registration.caution_check %}
+                <div class="alert alert-danger">
+                    {% trans "The user didn't give her/his caution check." %}
+                </div>
+            {% endif %}
+
+            <div class="card-body" id="profile_infos">
+                {% csrf_token %}
+                {{ form|crispy }}
+            </div>
+            <div class="card-footer text-center">
+                <button class="btn btn-success btn-sm">{% trans 'Validate registration' %}</button>
+            </div>
+        </form>
+    </div>
+{% endblock %}
-- 
GitLab