Édition de contenu

This commit is contained in:
Hugo LEVY-FALK 2018-03-10 09:28:50 +01:00
parent 04db2ab9f3
commit 764a2671c8
3 changed files with 36 additions and 67 deletions

View file

@ -18,3 +18,13 @@ class CreateContent(forms.ModelForm):
already_created = map(lambda x:x.category.pk, school.content_set.select_related('category'))
self.fields['category'].queryset = Category.objects.exclude(pk__in=already_created)
class ContentEdit(forms.ModelForm):
class Meta:
model = Content
fields = [
'name',
'file'
]

View file

@ -7,9 +7,7 @@ from .views import (
ViewCategory,
DeleteCategory,
EditCategory,
CreateContent,
DeleteContent,
EditContent,
)
app_name = 'content'
@ -46,7 +44,7 @@ urlpatterns = [
),
path(
'<int:pk>/edit',
EditContent.as_view(),
views.edit_content,
name="content-edit",
),

View file

@ -50,39 +50,6 @@ class EditCategory(PermissionRequiredMixin, generic.UpdateView):
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
@ -93,37 +60,7 @@ class DeleteContent(PermissionRequiredMixin, generic.DeleteView):
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)
return self.request.user.is_staff or self.request.user == school.admin
def create_content(request, school_pk):
@ -149,3 +86,27 @@ def create_content(request, school_pk):
'title' : 'Ajout de contenu',
'validate' : 'Ajouter'
})
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'
})