from django.views.generic import TemplateView, UpdateView, CreateView, DetailView, DeleteView from django.urls import reverse_lazy, reverse from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin from django.contrib.admin.views.decorators import staff_member_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 users.models import School from .models import SiteSettings, StaticPage from .forms import SelectUserForm class SettingsView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView): template_name = "settings/settings.html" permission_required = 'settings.change_sitesettings' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['categories'] = Category.objects.all() context['site_settings'], _ = SiteSettings.objects.get_or_create() context['schools'] = School.objects.all() context['settings'] = True context['administrators'] = User.objects.filter(is_staff=True) context['static_pages'] = StaticPage.objects.all() return context class EditSiteSettingsView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView): template_name = "edit.html" model = SiteSettings fields = '__all__' success_url = reverse_lazy('settings:index') permission_required = 'settings.change_sitesettings' def get_object(self, queryset=None): obj,_ = self.model.objects.get_or_create() return obj def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = "Édition des " + self.object.PRETTY_NAME return context class CreateStaticPageView(LoginRequiredMixin, CreateView): template_name = "edit.html" model = StaticPage fields = '__all__' success_url = reverse_lazy('settings:index') def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["title"] = "Création de page statique" context["validate"] = "Créer" return context @classmethod def as_view(self, *args, **kwargs): view = super().as_view(*args, **kwargs) return staff_member_required(view) class StaticPageView(DetailView): template_name = "settings/static_page.html" model = StaticPage fields = '__all__' @classmethod def as_view(self, *args, **kwargs): view = super().as_view(*args, **kwargs) return staff_member_required(view) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['page'] = self.object return context class DeleteStaticPageView(DeleteView): template_name = "confirm_delete.html" model = StaticPage success_url = reverse_lazy('settings:index') @classmethod def as_view(self, *args, **kwargs): view = super().as_view(*args, **kwargs) return staff_member_required(view) class EditStaticPageView(UpdateView): template_name = "edit.html" model = StaticPage success_url = reverse_lazy('settings:index') fields = '__all__' @classmethod def as_view(self, *args, **kwargs): view = super().as_view(*args, **kwargs) return staff_member_required(view) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["title"] = "Édition de page statique" context["validate"] = "Éditer" return context @staff_member_required def degrade_user(request, pk): user = get_object_or_404(User, pk=pk) user.is_staff = False user.save() messages.success(request, user.username + ' a été enlevé des administrateurs du site') return redirect(reverse('settings:index')) @staff_member_required def promote_user(request): user_form = SelectUserForm(request.POST or None) user_form.populate() if user_form.is_valid(): user=user_form.get_user() user.is_staff = True 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" })