site_tps/content/views.py
2018-02-28 21:25:44 +01:00

106 lines
2.8 KiB
Python

from django.views import generic
from django.urls import reverse, reverse_lazy
from django.shortcuts import get_object_or_404
from .models import Content, Category
class ContentCategoryList(generic.ListView):
"""Affiche les contenus d'une catégorie."""
model = Content
context_object_name = "contents"
template_name = "content/content_list.html"
def get_queryset(self):
pk = self.kwargs['pk']
category = get_object_or_404(Category, pk=pk)
return Content.objects.filter(category=category)
def get_context_data(self, **kwargs):
context = super(generic.ListView, self).get_context_data(**kwargs)
pk = self.kwargs['pk']
category = get_object_or_404(Category, pk=pk)
context['category'] = category
return context
class CreateCategory(generic.CreateView):
"""Création de catégorie."""
model = Category
fields = '__all__'
template_name = "edit.html"
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(generic.DeleteView):
"""Suppression de catégorie"""
model = Category
success_url = reverse_lazy('settings:index')
template_name = "confirm_delete.html"
class EditCategory(generic.UpdateView):
"""Édition de catégorie."""
model = Category
fields = '__all__'
template_name = "edit.html"
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(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 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
class DeleteContent(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()
class EditContent(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()