151 lines
5.1 KiB
Python
151 lines
5.1 KiB
Python
from django.views import generic
|
|
from django.urls import reverse, reverse_lazy
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
|
from django.contrib import messages
|
|
|
|
from .models import Content, Category
|
|
from users.models import School
|
|
from . import forms
|
|
from settings.models import SiteSettings
|
|
|
|
|
|
class ViewCategory(generic.DetailView):
|
|
"""Affiche une catégorie."""
|
|
model = Category
|
|
template_name = "content/content_list.html"
|
|
|
|
|
|
class CreateCategory(PermissionRequiredMixin, generic.CreateView):
|
|
"""Création de catégorie."""
|
|
model = Category
|
|
fields = '__all__'
|
|
template_name = "edit.html"
|
|
permission_required = 'content.add_category'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(generic.CreateView, self).get_context_data(**kwargs)
|
|
context['title'] = "Création de catégorie"
|
|
return context
|
|
|
|
|
|
class DeleteCategory(PermissionRequiredMixin, generic.DeleteView):
|
|
"""Suppression de catégorie"""
|
|
model = Category
|
|
success_url = reverse_lazy('settings:index')
|
|
template_name = "confirm_delete.html"
|
|
permission_required = 'content.delete_category'
|
|
|
|
|
|
class EditCategory(PermissionRequiredMixin, generic.UpdateView):
|
|
"""Édition de catégorie."""
|
|
model = Category
|
|
fields = '__all__'
|
|
template_name = "edit.html"
|
|
permission_required = 'content.change_category'
|
|
def get_context_data(self, **kwargs):
|
|
context = super(generic.UpdateView, self).get_context_data(**kwargs)
|
|
context['title'] = "Édition de " + self.object.name
|
|
return context
|
|
|
|
|
|
class CreateContent(PermissionRequiredMixin, generic.CreateView):
|
|
"""Création de contenu."""
|
|
model = Content
|
|
fields = [
|
|
'name',
|
|
'category',
|
|
'file'
|
|
]
|
|
template_name = "edit.html"
|
|
extra_context = {
|
|
'title' : 'Envoi de contenu',
|
|
'validate' : 'Envoyer'
|
|
}
|
|
|
|
def has_permission(self):
|
|
return self.request.user.has_perm('users.manage_'+str(self.request.user.userprofile.school.group.pk))
|
|
|
|
def get_success_url(self):
|
|
return self.object.school_owner.get_absolute_url()
|
|
|
|
def form_valid(self, form):
|
|
form.instance.school_owner = self.request.user.userprofile.school
|
|
r = super().form_valid(form)
|
|
return r
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
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"))
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
class DeleteContent(PermissionRequiredMixin, generic.DeleteView):
|
|
"""Suppression de contenu"""
|
|
model = Content
|
|
template_name = "confirm_delete.html"
|
|
|
|
def get_success_url(self):
|
|
return self.object.school_owner.get_absolute_url()
|
|
|
|
def has_permission(self):
|
|
school = get_object_or_404(Content, pk=self.kwargs['pk']).school_owner
|
|
return self.request.user.has_perm('users.manage_'+str(school.group.pk))
|
|
|
|
|
|
class EditContent(PermissionRequiredMixin, generic.UpdateView):
|
|
"""Édition d'un contenu"""
|
|
model = Content
|
|
template_name = "edit.html"
|
|
fields = [
|
|
'name',
|
|
'category',
|
|
'file'
|
|
]
|
|
template_name = "edit.html"
|
|
extra_context = {
|
|
'title' : 'Édition de contenu',
|
|
'validate' : 'Envoyer'
|
|
}
|
|
|
|
def get_success_url(self):
|
|
return self.object.school_owner.get_absolute_url()
|
|
|
|
def has_permission(self):
|
|
school = get_object_or_404(Content, pk=self.kwargs['pk']).school_owner
|
|
return self.request.user.has_perm('users.manage_'+str(school.group.pk))
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
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"))
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
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'
|
|
})
|