From 8191d72d7c53f8fd130b8395feb26ddaabc8c4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 8 Jun 2020 18:30:40 +0200 Subject: [PATCH] Allow BDS staff members to update a user's account --- bds/forms.py | 30 +++++++++++++++ bds/static/bds/css/bds.css | 53 +++++++++++++++++++++++---- bds/templates/bds/base.html | 2 + bds/templates/bds/messages.html | 9 +++++ bds/templates/bds/search_results.html | 4 +- bds/templates/bds/user_update.html | 31 ++++++++++++++++ bds/urls.py | 1 + bds/views.py | 50 ++++++++++++++++++++++++- 8 files changed, 170 insertions(+), 10 deletions(-) create mode 100644 bds/forms.py create mode 100644 bds/templates/bds/messages.html create mode 100644 bds/templates/bds/user_update.html diff --git a/bds/forms.py b/bds/forms.py new file mode 100644 index 00000000..6428672e --- /dev/null +++ b/bds/forms.py @@ -0,0 +1,30 @@ +from django import forms +from django.contrib.auth import get_user_model + +from bds.models import BDSProfile + +User = get_user_model() + + +class BDSUserForm(forms.ModelForm): + class Meta: + model = User + fields = ["first_name", "last_name", "email"] + + +class BDSProfileForm(forms.ModelForm): + class Meta: + model = BDSProfile + fields = [ + "phone", + "occupation", + "departement", + "birthdate", + "mails_bds", + "has_certificate", + "ASPSL_number", + "FFSU_number", + "is_member", + "cotisation_period", + "cotisation_type", + ] diff --git a/bds/static/bds/css/bds.css b/bds/static/bds/css/bds.css index fe9b2fa2..8e5a54f7 100644 --- a/bds/static/bds/css/bds.css +++ b/bds/static/bds/css/bds.css @@ -5,6 +5,12 @@ html, body { font-size: 18px; } +input { + font-size: 18px; + border: 0; + padding: 10px; +} + a { text-decoration: none; color: #a82305; @@ -26,16 +32,9 @@ nav a, nav a img { height: 100%; } -input[type="text"] { - font-size: 18px; -} - #search_autocomplete { flex: 1; width: 480px; - margin: 0; - border: 0; - padding: 10px 10px; } .highlight { @@ -80,3 +79,43 @@ input[type="text"] { .autocomplete-value, .autocomplete-new, .autocomplete-more { background: white; } + +/* Forms */ + +form { + width: 50%; + margin: auto; +} + +.form-field { + width: 90%; + margin: auto; + padding-top: 1em; +} + +.form-field label { + display: inline-block; + padding 0; +} + +.form-field input { + display: inline-block; + width: 99%; + padding: 1% 0.5%; +} + +.form-field input[type="checkbox"] { + width: 1em; +} + +form input[type="submit"] { + display: block; + width: 30%; + margin: auto; + margin-top: 1em; + margin-bottom: 1em; +} + +form input[type="submit"]:hover { + background: #e8554e; +} diff --git a/bds/templates/bds/base.html b/bds/templates/bds/base.html index 0bf34287..d4ad0f26 100644 --- a/bds/templates/bds/base.html +++ b/bds/templates/bds/base.html @@ -10,6 +10,7 @@ {# CSS #} + {# Javascript #} @@ -17,6 +18,7 @@ {% include "bds/nav.html" %} + {% include "bds/messages.html" %} {% block content %}{% endblock %} diff --git a/bds/templates/bds/messages.html b/bds/templates/bds/messages.html new file mode 100644 index 00000000..5d6ab06d --- /dev/null +++ b/bds/templates/bds/messages.html @@ -0,0 +1,9 @@ +{% if messages %} + {% for message in messages %} +
+
+ {{ message }} +
+
+ {% endfor %} +{% endif %} diff --git a/bds/templates/bds/search_results.html b/bds/templates/bds/search_results.html index b1c46622..3ba9b0d8 100644 --- a/bds/templates/bds/search_results.html +++ b/bds/templates/bds/search_results.html @@ -9,7 +9,7 @@ {% for user in members %} {% if forloop.counter < 5 %}
  • - + {{ user|highlight_user:q }}
  • @@ -28,7 +28,7 @@ {% for user in others %} {% if forloop.counter < 5 %}
  • - + {{ user|highlight_user:q }}
  • diff --git a/bds/templates/bds/user_update.html b/bds/templates/bds/user_update.html new file mode 100644 index 00000000..224d3dd4 --- /dev/null +++ b/bds/templates/bds/user_update.html @@ -0,0 +1,31 @@ +{% extends "bds/base.html" %} +{% load i18n %} + +{% block content %} +
    + {% csrf_token %} + + {% for field in user_form %} +
    + {{ field.errors }} + {{ field.label_tag }} + {{ field }} + {% if field.help_text %} +

    {{ field.help_text|safe }}

    + {% endif %} +
    + {% endfor %} + + {% for field in profile_form %} +
    + {{ field.errors }} + {{ field.label_tag }} {{ field }} + {% if field.help_text %} +

    {{ field.help_text|safe }}

    + {% endif %} +
    + {% endfor %} + + +
    +{% endblock %} diff --git a/bds/urls.py b/bds/urls.py index fbddccc6..ae841fe0 100644 --- a/bds/urls.py +++ b/bds/urls.py @@ -6,4 +6,5 @@ app_name = "bds" urlpatterns = [ path("", views.Home.as_view(), name="home"), path("autocomplete", views.AutocompleteView.as_view(), name="autocomplete"), + path("user/update/", views.UserUpdateView.as_view(), name="user.update"), ] diff --git a/bds/views.py b/bds/views.py index a2ba3a2c..972cbb73 100644 --- a/bds/views.py +++ b/bds/views.py @@ -1,9 +1,16 @@ -from django.http import Http404 +from django.contrib import messages +from django.contrib.auth import get_user_model +from django.http import Http404, HttpResponseRedirect +from django.shortcuts import get_object_or_404 +from django.utils.translation import gettext_lazy as _ from django.views.generic import TemplateView from bds.autocomplete import bds_search +from bds.forms import BDSProfileForm, BDSUserForm from bds.mixins import StaffRequiredMixin +User = get_user_model() + class AutocompleteView(StaffRequiredMixin, TemplateView): template_name = "bds/search_results.html" @@ -22,3 +29,44 @@ class AutocompleteView(StaffRequiredMixin, TemplateView): class Home(StaffRequiredMixin, TemplateView): template_name = "bds/home.html" + + +class UserUpdateView(StaffRequiredMixin, TemplateView): + template_name = "bds/user_update.html" + + def setup(self, *args, **kwargs): + super().setup(*args, **kwargs) + self.user = get_object_or_404(User, pk=self.kwargs["pk"]) + + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) + + form_data = self.request.POST if self.request.method == "POST" else None + profile = getattr(self.user, "bds", None) + ctx["user_form"] = BDSUserForm(prefix="user", intance=self.user, data=form_data) + ctx["profile_form"] = BDSProfileForm( + prefix="profile", instance=profile, data=form_data + ) + + return ctx + + def post(self, *args, **kwargs): + ctx = self.get_context_data() + user_form = ctx["user_form"] + profile_form = ctx["profile_form"] + + if user_form.is_valid() and profile_form.is_valid(): + user_form.save() + profile_form.save() + messages.add_message( + self.request, messages.SUCCESS, _("Compte mis à jour avec succès !") + ) + else: + messages.add_message( + self.request, + messages.ERROR, + _("Le formulaire de mise à jour du compte contient des erreurs."), + ) + + # Redirect here. + return HttpResponseRedirect("")