Ajout/suppression d'admin du site
This commit is contained in:
parent
a507c550c6
commit
56076ab391
4 changed files with 99 additions and 2 deletions
19
settings/forms.py
Normal file
19
settings/forms.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
from django import forms
|
||||||
|
from django.contrib.auth.models import User, Group
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
|
class SelectUserForm(forms.Form):
|
||||||
|
pk = forms.ChoiceField(
|
||||||
|
label="Utilisateur",
|
||||||
|
help_text="Sera ajouté aux administrateurs du site.",
|
||||||
|
validators=[],
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_user(self):
|
||||||
|
pk = self.cleaned_data['pk']
|
||||||
|
return get_object_or_404(User, pk=pk)
|
||||||
|
|
||||||
|
def populate(self):
|
||||||
|
admins,_ = Group.objects.get_or_create(name='admins')
|
||||||
|
choices = [(u.pk, u.first_name + ' ' + u.last_name + '(' + u.username + ')') for u in User.objects.all()]
|
||||||
|
self.fields['pk'].choices = choices
|
|
@ -1,6 +1,40 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Page d'administration du site</h1>
|
<h1>Page d'administration du site</h1>
|
||||||
|
<h2>Administrateurs du site</h2>
|
||||||
|
<a class="btn btn-success btn-sm" role="button" href="{% url 'settings:promote-user' %}">
|
||||||
|
<i class="fas fa-plus"></i>
|
||||||
|
Ajouter un administrateur
|
||||||
|
</a>
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
<table class="table table-striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Prénom</th>
|
||||||
|
<th>Nom</th>
|
||||||
|
<th>Pseudo</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for admin in administrators %}
|
||||||
|
<tr>
|
||||||
|
<td>{{admin.first_name}}</td>
|
||||||
|
<td>{{admin.last_name}}</td>
|
||||||
|
<td>{{admin.username}}</td>
|
||||||
|
<td>
|
||||||
|
<a class="btn btn-outline-danger btn-sm" href="{% url 'settings:degrade-user' admin.pk %}">
|
||||||
|
<i class="fa fa-trash"></i>
|
||||||
|
Enlever le privilège Administrateur
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
<h2>Liste des catégories</h2>
|
<h2>Liste des catégories</h2>
|
||||||
<a class="btn btn-success btn-sm" role="button" href="{% url 'content:category-new' %}">
|
<a class="btn btn-success btn-sm" role="button" href="{% url 'content:category-new' %}">
|
||||||
<i class="fas fa-plus"></i>
|
<i class="fas fa-plus"></i>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from .views import SettingsView, EditSiteSettingsView
|
from .views import SettingsView, EditSiteSettingsView, degrade_user, promote_user
|
||||||
|
|
||||||
app_name = 'settings'
|
app_name = 'settings'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
@ -13,4 +13,15 @@ urlpatterns = [
|
||||||
EditSiteSettingsView.as_view(),
|
EditSiteSettingsView.as_view(),
|
||||||
name='site-settings'
|
name='site-settings'
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
'degrade_user/<int:pk>',
|
||||||
|
degrade_user,
|
||||||
|
name='degrade-user',
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
'promote_user',
|
||||||
|
promote_user,
|
||||||
|
name='promote-user',
|
||||||
|
)
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
from django.views.generic import TemplateView, UpdateView
|
from django.views.generic import TemplateView, UpdateView
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy, reverse
|
||||||
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
|
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
|
||||||
|
from django.contrib.auth.decorators import permission_required
|
||||||
|
from django.contrib.auth.models import Group, User
|
||||||
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
from django.contrib import messages
|
||||||
|
|
||||||
from content.models import Category
|
from content.models import Category
|
||||||
from users.models import SchoolProfile
|
from users.models import SchoolProfile
|
||||||
from .models import SiteSettings
|
from .models import SiteSettings
|
||||||
|
from .forms import SelectUserForm
|
||||||
|
|
||||||
|
|
||||||
class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
|
class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
|
||||||
|
@ -16,6 +22,7 @@ class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
|
||||||
context['site_settings'], _ = SiteSettings.objects.get_or_create()
|
context['site_settings'], _ = SiteSettings.objects.get_or_create()
|
||||||
context['schools'] = SchoolProfile.objects.all()
|
context['schools'] = SchoolProfile.objects.all()
|
||||||
context['settings'] = True
|
context['settings'] = True
|
||||||
|
context['administrators'] = Group.objects.get(name='admins').user_set.all()
|
||||||
return context
|
return context
|
||||||
|
|
||||||
class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
|
class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
|
||||||
|
@ -35,3 +42,29 @@ class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateVi
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
@permission_required('auth.change_user')
|
||||||
|
def degrade_user(request, pk):
|
||||||
|
user = get_object_or_404(User, pk=pk)
|
||||||
|
admins,_ = Group.objects.get_or_create(name='admins')
|
||||||
|
user.groups.remove(admins)
|
||||||
|
user.save()
|
||||||
|
messages.success(request, user.username + ' a été enlevé des administrateurs du site')
|
||||||
|
return redirect(reverse('settings:index'))
|
||||||
|
|
||||||
|
|
||||||
|
@permission_required('auth.change_user')
|
||||||
|
def promote_user(request):
|
||||||
|
user_form = SelectUserForm(request.POST or None)
|
||||||
|
user_form.populate()
|
||||||
|
if user_form.is_valid():
|
||||||
|
user=user_form.get_user()
|
||||||
|
admins,_ = Group.objects.get_or_create(name='admins')
|
||||||
|
user.groups.add(admins)
|
||||||
|
user.save()
|
||||||
|
messages.success(request, user.username + ' a été ajouté des administrateurs du site')
|
||||||
|
return redirect(reverse('settings:index'))
|
||||||
|
return render(request, 'edit.html', {
|
||||||
|
'form' : user_form,
|
||||||
|
'title' : "Ajout d'un administrateur",
|
||||||
|
'validate' : "Ajouter"
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in a new issue