Gestion des utilisateurs

This commit is contained in:
Hugo LEVY-FALK 2018-03-10 14:48:48 +01:00
parent 764a2671c8
commit 29f9440f25
6 changed files with 108 additions and 20 deletions

View file

@ -80,6 +80,8 @@
<th>Nombre de catégories</th> <th>Nombre de catégories</th>
<th>Numéro de téléphone</th> <th>Numéro de téléphone</th>
<th>Inscription</th> <th>Inscription</th>
<th>Administrateur</th>
<th></th>
{% for school in schools %} {% for school in schools %}
<tr> <tr>
<th><a href="{{school.get_absolute_url}}">{{school.name}}</a></th> <th><a href="{{school.get_absolute_url}}">{{school.name}}</a></th>
@ -99,9 +101,27 @@
</span> </span>
{% endif %} {% endif %}
</td> </td>
<td>
{% if school.admin %}
{{school.admin.first_name}} {{school.admin.last_name}} ({{school.admin.username}})
{% else %}
Non défini
{% endif %}
</td>
<td>
<a href="{% url 'users:edit-admin' school.pk %}" class="btn btn-outline-primary btn-sm">
<i class="fa fa-edit"></i>
Définir
</a>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
<h2>Utilisateurs</h2>
<a class="btn btn-success btn-lg" href="{% url 'users:new-user' %}">
<i class="fa fa-plus"></i>
Ajouter un utilisateur
</a>
<h2>Réglages</h2> <h2>Réglages</h2>
<a class="btn btn-primary btn-sm" href="{% url 'settings:site-settings' %}"> <a class="btn btn-primary btn-sm" href="{% url 'settings:site-settings' %}">
<i class="fas fa-edit"></i> <i class="fas fa-edit"></i>

View file

@ -40,9 +40,6 @@
<li class="nav-item {% if active == 4 %}active{% endif %}"> <li class="nav-item {% if active == 4 %}active{% endif %}">
<a class="nav-link" href="{% url 'users:login' %}">Connexion<span class="sr-only">(current)</span></a> <a class="nav-link" href="{% url 'users:login' %}">Connexion<span class="sr-only">(current)</span></a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="{% url 'users:new-user' %}">Inscription<span class="sr-only">(current)</span></a>
</li>
{% endif %} {% endif %}
</ul> </ul>

View file

@ -19,7 +19,17 @@ class EditName(forms.ModelForm):
class CreateUser(forms.ModelForm): class CreateUser(forms.ModelForm):
class Meta: class Meta:
model = User model = User
fields = '__all__' fields = [
'username',
'first_name',
'last_name',
'email',
'groups',
'password',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['password'].widget = forms.PasswordInput()
class EditPhone(forms.ModelForm): class EditPhone(forms.ModelForm):

View file

@ -1,16 +1,43 @@
from django.urls import path, include from django.urls import path
from django.contrib.auth import views as auth_views
from . import views from . import views
app_name = 'users' app_name = 'users'
urlpatterns = [ urlpatterns = [
path( path(
'user/new', 'new',
views.create_user, views.create_user,
name='new-user' name='new-user'
), ),
path( path(
'user/', 'login',
include('django.contrib.auth.urls') auth_views.LoginView.as_view(template_name="edit.html"),
name='login'
),
path(
'logout',
auth_views.LogoutView.as_view(),
name='logout'
),
path(
'password_change',
auth_views.PasswordChangeView.as_view(template_name="edit.html"),
name='password-change'
),
path(
'password_change/done',
auth_views.PasswordChangeDoneView.as_view(template_name="base.html"),
name='password-change-done'
),
path(
'reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name="edit.html"),
name='password-reset-confirm'
),
path(
'reset/done/',
auth_views.PasswordResetCompleteView.as_view(template_name="base.html"),
name='password-reset-done'
), ),
path( path(
'school/new', 'school/new',
@ -47,6 +74,11 @@ urlpatterns = [
views.edit_jury_2, views.edit_jury_2,
name='edit-jury-2' name='edit-jury-2'
), ),
path(
'school/<int:pk>/edit_admin',
views.edit_admin,
name='edit-admin'
),
path( path(
'school/<int:pk>/validate', 'school/<int:pk>/validate',
views.validate, views.validate,

View file

@ -3,6 +3,7 @@ from django.urls import reverse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.contrib.admin.views.decorators import staff_member_required from django.contrib.admin.views.decorators import staff_member_required
from settings.forms import SelectUserForm
from content.models import Category from content.models import Category
from .models import School from .models import School
@ -29,7 +30,8 @@ def create_school(request):
school_form = forms.CreateSchool(request.POST or None) school_form = forms.CreateSchool(request.POST or None)
if school_form.is_valid(): if school_form.is_valid():
s = school_form.save() s = school_form.save()
messages.success(request, "L'école {} a bien été créée.".format(s.name)) messages.success(
request, "L'école {} a bien été créée.".format(s.name))
return redirect(reverse('settings:index')) return redirect(reverse('settings:index'))
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': school_form, 'form': school_form,
@ -56,7 +58,8 @@ def edit_phone(request, pk):
school_form = forms.EditPhone(request.POST or None, instance=school) school_form = forms.EditPhone(request.POST or None, instance=school)
if school_form.is_valid(): if school_form.is_valid():
s = school_form.save() s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) messages.success(
request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(s.get_absolute_url()) return redirect(s.get_absolute_url())
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': school_form, 'form': school_form,
@ -74,7 +77,8 @@ def edit_logo(request, pk):
school_form = forms.EditLogo(request.POST or None, instance=school) school_form = forms.EditLogo(request.POST or None, instance=school)
if school_form.is_valid(): if school_form.is_valid():
s = school_form.save() s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) messages.success(
request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(s.get_absolute_url()) return redirect(s.get_absolute_url())
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': school_form, 'form': school_form,
@ -92,7 +96,8 @@ def edit_mail(request, pk):
user_form = forms.EditMail(request.POST or None, instance=school.admin) user_form = forms.EditMail(request.POST or None, instance=school.admin)
if user_form.is_valid(): if user_form.is_valid():
s = user_form.save() s = user_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.school.name)) messages.success(
request, "L'école {} a bien été modifiée.".format(s.school.name))
return redirect(s.school.get_absolute_url()) return redirect(s.school.get_absolute_url())
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': user_form, 'form': user_form,
@ -110,7 +115,8 @@ def edit_jury_1(request, pk):
school_form = forms.EditJury1(request.POST or None, instance=school) school_form = forms.EditJury1(request.POST or None, instance=school)
if school_form.is_valid(): if school_form.is_valid():
s = school_form.save() s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) messages.success(
request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(s.get_absolute_url()) return redirect(s.get_absolute_url())
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': school_form, 'form': school_form,
@ -128,7 +134,8 @@ def edit_jury_2(request, pk):
school_form = forms.EditJury2(request.POST or None, instance=school) school_form = forms.EditJury2(request.POST or None, instance=school)
if school_form.is_valid(): if school_form.is_valid():
s = school_form.save() s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) messages.success(
request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(s.get_absolute_url()) return redirect(s.get_absolute_url())
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': school_form, 'form': school_form,
@ -146,7 +153,8 @@ def edit_name(request, pk):
school_form = forms.EditName(request.POST or None, instance=school) school_form = forms.EditName(request.POST or None, instance=school)
if school_form.is_valid(): if school_form.is_valid():
s = school_form.save() s = school_form.save()
messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) messages.success(
request, "L'école {} a bien été modifiée.".format(s.name))
return redirect(s.get_absolute_url()) return redirect(s.get_absolute_url())
return render(request, 'edit.html', { return render(request, 'edit.html', {
'form': school_form, 'form': school_form,
@ -155,6 +163,26 @@ def edit_name(request, pk):
}) })
@staff_member_required
def edit_admin(request, pk):
school = get_object_or_404(School, pk=pk)
user_form = SelectUserForm(request.POST or None)
user_form.populate()
if user_form.is_valid():
user = user_form.get_user()
school.admin = user
school.save()
user.save()
messages.success(request, user.username +
' a été nommé admin de ' + school.name)
return redirect(reverse('settings:index'))
return render(request, 'edit.html', {
'form': user_form,
'title': "Définir l'administrateur de {}".format(school.name),
'validate': 'Ajouter'
})
def validate(request, pk): def validate(request, pk):
school = get_object_or_404(School, pk=pk) school = get_object_or_404(School, pk=pk)
can = request.user.is_staff or request.user == school.admin can = request.user.is_staff or request.user == school.admin
@ -189,7 +217,8 @@ def validate(request, pk):
for category in Category.objects.all(): for category in Category.objects.all():
if not category.content_set.filter(school_owner=school): if not category.content_set.filter(school_owner=school):
messages.warning(request, 'Pas de contenu dans la catégorie {}.'.format(category.name)) messages.warning(
request, 'Pas de contenu dans la catégorie {}.'.format(category.name))
if phone_ok and mail_ok: if phone_ok and mail_ok:
school.validated = True school.validated = True