site_tps/content/views.py

137 lines
4.5 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
from django.shortcuts import get_object_or_404, redirect
2018-03-02 09:20:50 +00:00
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
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
from settings.models import SiteSettings
2018-01-24 10:33:05 +00:00
2018-01-30 21:47:47 +00:00
class ContentCategoryList(generic.ListView):
2018-01-24 10:33:05 +00:00
"""Affiche les contenus d'une catégorie."""
model = Content
context_object_name = "contents"
template_name = "content/content_list.html"
def get_queryset(self):
2018-01-30 22:07:08 +00:00
pk = self.kwargs['pk']
category = get_object_or_404(Category, pk=pk)
2018-01-24 10:33:05 +00:00
return Content.objects.filter(category=category)
def get_context_data(self, **kwargs):
2018-01-30 21:47:47 +00:00
context = super(generic.ListView, self).get_context_data(**kwargs)
2018-01-30 22:07:08 +00:00
pk = self.kwargs['pk']
category = get_object_or_404(Category, pk=pk)
2018-01-24 10:33:05 +00:00
context['category'] = category
return context
2018-01-30 22:04:06 +00:00
2018-03-02 09:20:50 +00:00
class CreateCategory(PermissionRequiredMixin, 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-02 09:20:50 +00:00
permission_required = 'content.add_category'
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-02 09:20:50 +00:00
class DeleteCategory(PermissionRequiredMixin, 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-02 10:51:26 +00:00
permission_required = 'content.delete_category'
2018-01-30 22:04:06 +00:00
2018-03-02 09:20:50 +00:00
class EditCategory(PermissionRequiredMixin, generic.UpdateView):
2018-01-30 22:04:06 +00:00
"""Édition de catégorie."""
model = Category
fields = '__all__'
template_name = "edit.html"
2018-03-02 09:20:50 +00:00
permission_required = 'content.change_category'
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 CreateContent(PermissionRequiredMixin, generic.CreateView):
2018-02-28 20:25:44 +00:00
"""Création de contenu."""
model = Content
fields = [
'name',
'category',
'file'
]
template_name = "edit.html"
extra_context = {
'title' : 'Envoi de contenu',
'validate' : 'Envoyer'
}
2018-03-02 09:20:50 +00:00
def has_permission(self):
return self.request.user.has_perm('users.manage_'+str(self.request.user.userprofile.school.group.pk))
2018-02-28 20:25:44 +00:00
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)
2018-02-28 20:25:44 +00:00
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
return self.request.user.has_perm('users.manage_'+str(school.group.pk))
2018-02-28 20:25:44 +00:00
2018-03-02 09:20:50 +00:00
class EditContent(PermissionRequiredMixin, generic.UpdateView):
2018-02-28 20:25:44 +00:00
"""É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()
2018-03-02 09:20:50 +00:00
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)