site_tps/content/views.py

126 lines
4.2 KiB
Python
Raw Normal View History

2018-01-30 21:47:47 +00:00
from django.views import generic
from django.urls import reverse, reverse_lazy
2018-03-09 12:18:03 +00:00
from django.shortcuts import get_object_or_404, redirect, render
2018-03-02 09:20:50 +00:00
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
2018-03-09 12:18:03 +00:00
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib import messages
2018-01-14 12:19:11 +00:00
2018-01-24 10:33:05 +00:00
from .models import Content, Category
2018-03-09 12:18:03 +00:00
from users.models import School
from . import forms
from settings.models import SiteSettings
2018-01-24 10:33:05 +00:00
class ViewCategory(generic.DetailView):
"""Affiche une catégorie."""
model = Category
2018-01-24 10:33:05 +00:00
template_name = "content/content_list.html"
2018-01-30 22:04:06 +00:00
2018-03-12 09:18:58 +00:00
class CreateCategory(generic.CreateView):
2018-01-30 22:04:06 +00:00
"""Création de catégorie."""
model = Category
fields = '__all__'
2018-02-28 14:06:55 +00:00
template_name = "edit.html"
2018-03-12 09:18:58 +00:00
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
2018-02-28 14:06:55 +00:00
def get_context_data(self, **kwargs):
context = super(generic.CreateView, self).get_context_data(**kwargs)
context['title'] = "Création de catégorie"
return context
2018-01-30 21:47:47 +00:00
2018-01-30 22:04:06 +00:00
2018-03-12 09:18:58 +00:00
class DeleteCategory(generic.DeleteView):
2018-01-30 22:04:06 +00:00
"""Suppression de catégorie"""
2018-01-30 21:47:47 +00:00
model = Category
success_url = reverse_lazy('settings:index')
template_name = "confirm_delete.html"
2018-03-12 09:18:58 +00:00
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
2018-01-30 22:04:06 +00:00
2018-03-12 09:18:58 +00:00
class EditCategory(generic.UpdateView):
2018-01-30 22:04:06 +00:00
"""Édition de catégorie."""
model = Category
fields = '__all__'
template_name = "edit.html"
2018-03-12 09:18:58 +00:00
@classmethod
def as_view(self, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return staff_member_required(view)
2018-02-28 14:06:55 +00:00
def get_context_data(self, **kwargs):
context = super(generic.UpdateView, self).get_context_data(**kwargs)
context['title'] = "Édition de " + self.object.name
return context
2018-03-02 09:20:50 +00:00
class DeleteContent(PermissionRequiredMixin, generic.DeleteView):
2018-02-28 20:25:44 +00:00
"""Suppression de contenu"""
model = Content
template_name = "confirm_delete.html"
def get_success_url(self):
return self.object.school_owner.get_absolute_url()
2018-03-02 09:20:50 +00:00
def has_permission(self):
school = get_object_or_404(Content, pk=self.kwargs['pk']).school_owner
2018-03-10 08:28:50 +00:00
return self.request.user.is_staff or self.request.user == school.admin
2018-03-09 12:18:03 +00:00
def create_content(request, school_pk):
settings,_ = SiteSettings.objects.get_or_create()
if not settings.allow_upload :
messages.error(request, "Le téléversement de contenu n'est pas autorisé actuellement.")
return redirect(reverse("home"))
school = get_object_or_404(School, pk=school_pk)
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('home'))
content_form = forms.CreateContent(school, request.POST or None, request.FILES or None)
content_form.instance.school_owner = school
if content_form.is_valid():
content_form.save()
messages.success(request, "Contenu ajouté.")
return redirect(school.get_absolute_url())
return render(request, 'edit.html', {
'form' : content_form,
'title' : 'Ajout de contenu',
'validate' : 'Ajouter'
})
2018-03-10 08:28:50 +00:00
def edit_content(request, pk):
settings,_ = SiteSettings.objects.get_or_create()
if not settings.allow_upload :
messages.error(request, "Le téléversement de contenu n'est pas autorisé actuellement.")
return redirect(reverse("home"))
content = get_object_or_404(Content, pk=pk)
school = content.school_owner
can = request.user.is_staff or request.user == school.admin
if not can:
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
return redirect(reverse('home'))
content_form = forms.ContentEdit(request.POST or None, request.FILES or None, instance=content)
if content_form.is_valid():
content_form.save()
messages.success(request, "Contenu modifié.")
return redirect(school.get_absolute_url())
return render(request, 'edit.html', {
'form' : content_form,
'title' : 'Modifier un contenu',
'validate' : 'Modifier'
})