Allow BDS staff members to update a user's account

This commit is contained in:
Martin Pépin 2020-06-08 18:30:40 +02:00
parent f6e1f39cff
commit 8191d72d7c
No known key found for this signature in database
GPG key ID: E7520278B1774448
8 changed files with 170 additions and 10 deletions

30
bds/forms.py Normal file
View file

@ -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",
]

View file

@ -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;
}

View file

@ -10,6 +10,7 @@
{# CSS #}
<link rel="stylesheet" href="{% static "bds/css/bds.css" %}">
<link rel="stylesheet" href="{% static "shared/css/messages.css" %}">
{# Javascript #}
<script src="{% static 'vendor/jquery/jquery-3.3.1.min.js' %}"></script>
@ -17,6 +18,7 @@
</head>
<body>
{% include "bds/nav.html" %}
{% include "bds/messages.html" %}
{% block content %}{% endblock %}
</body>

View file

@ -0,0 +1,9 @@
{% if messages %}
{% for message in messages %}
<div class="messages">
<div class="alert alert-{{ message.level_tag }} alert-dismissible fade in{% if message.tags %} {{ message.tags }}{% endif %}">
{{ message }}
</div>
</div>
{% endfor %}
{% endif %}

View file

@ -9,7 +9,7 @@
{% for user in members %}
{% if forloop.counter < 5 %}
<li class="autocomplete-value">
<a class="autocomplete-item" href="#TODO">
<a class="autocomplete-item" href="{% url "bds:user.update" user.id %}">
{{ user|highlight_user:q }}
</a>
</li>
@ -28,7 +28,7 @@
{% for user in others %}
{% if forloop.counter < 5 %}
<li class="autocomplete-value">
<a class="autocomplete-item" href="#TODO">
<a class="autocomplete-item" href="{% url "bds:user.update" user.id %}">
{{ user|highlight_user:q }}
</a>
</li>

View file

@ -0,0 +1,31 @@
{% extends "bds/base.html" %}
{% load i18n %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% for field in user_form %}
<div class="form-field">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
{% for field in profile_form %}
<div class="form-field">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
<input type="submit" value="{% trans "Enregistrer" %}">
</form>
{% endblock %}

View file

@ -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/<pk>", views.UserUpdateView.as_view(), name="user.update"),
]

View file

@ -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("")