2018-02-22 19:55:10 +00:00
|
|
|
from django.views.generic import TemplateView, UpdateView
|
2018-03-02 10:16:42 +00:00
|
|
|
from django.urls import reverse_lazy, reverse
|
2018-03-01 10:25:34 +00:00
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
|
2018-03-09 10:44:49 +00:00
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2018-03-02 10:16:42 +00:00
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.contrib import messages
|
|
|
|
|
2018-01-30 20:39:44 +00:00
|
|
|
from content.models import Category
|
2018-03-08 22:16:03 +00:00
|
|
|
from users.models import School
|
2018-02-28 20:25:44 +00:00
|
|
|
from .models import SiteSettings
|
2018-03-02 10:16:42 +00:00
|
|
|
from .forms import SelectUserForm
|
2018-01-14 12:19:11 +00:00
|
|
|
|
2018-02-22 19:55:10 +00:00
|
|
|
|
2018-03-01 10:25:34 +00:00
|
|
|
class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
|
2018-01-30 20:39:44 +00:00
|
|
|
template_name = "settings/settings.html"
|
2018-03-01 10:25:34 +00:00
|
|
|
permission_required = 'settings.change_sitesettings'
|
2018-02-22 19:55:10 +00:00
|
|
|
|
2018-01-30 20:39:44 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['categories'] = Category.objects.all()
|
2018-02-22 19:55:10 +00:00
|
|
|
context['site_settings'], _ = SiteSettings.objects.get_or_create()
|
2018-03-08 22:16:03 +00:00
|
|
|
context['schools'] = School.objects.all()
|
2018-02-22 19:55:10 +00:00
|
|
|
context['settings'] = True
|
2018-03-09 10:44:49 +00:00
|
|
|
context['administrators'] = User.objects.filter(is_staff=True)
|
2018-01-30 20:39:44 +00:00
|
|
|
return context
|
2018-02-22 19:55:10 +00:00
|
|
|
|
2018-03-01 10:25:34 +00:00
|
|
|
class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
|
2018-02-22 19:55:10 +00:00
|
|
|
template_name = "edit.html"
|
|
|
|
model = SiteSettings
|
|
|
|
fields = '__all__'
|
|
|
|
success_url = reverse_lazy('settings:index')
|
2018-03-01 10:25:34 +00:00
|
|
|
permission_required = 'settings.change_sitesettings'
|
2018-02-22 19:55:10 +00:00
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
obj,_ = self.model.objects.get_or_create()
|
|
|
|
return obj
|
2018-02-28 14:06:55 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(UpdateView, self).get_context_data(**kwargs)
|
|
|
|
context['title'] = "Édition des " + self.object.PRETTY_NAME
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-03-09 10:44:49 +00:00
|
|
|
@staff_member_required
|
2018-03-02 10:16:42 +00:00
|
|
|
def degrade_user(request, pk):
|
|
|
|
user = get_object_or_404(User, pk=pk)
|
2018-03-09 10:44:49 +00:00
|
|
|
user.is_staff = False
|
2018-03-02 10:16:42 +00:00
|
|
|
user.save()
|
|
|
|
messages.success(request, user.username + ' a été enlevé des administrateurs du site')
|
|
|
|
return redirect(reverse('settings:index'))
|
|
|
|
|
|
|
|
|
2018-03-09 10:44:49 +00:00
|
|
|
@staff_member_required
|
2018-03-02 10:16:42 +00:00
|
|
|
def promote_user(request):
|
|
|
|
user_form = SelectUserForm(request.POST or None)
|
|
|
|
user_form.populate()
|
|
|
|
if user_form.is_valid():
|
|
|
|
user=user_form.get_user()
|
2018-03-09 10:44:49 +00:00
|
|
|
user.is_staff = True
|
2018-03-02 10:16:42 +00:00
|
|
|
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"
|
|
|
|
})
|