Refactor page view with Django's black magic powered DetailView&co

This commit is contained in:
Skia
2015-11-25 14:45:18 +01:00
parent d72b18c120
commit b237cdbaae
11 changed files with 163 additions and 185 deletions

View File

@ -56,42 +56,3 @@ class UserEditForm(UserChangeForm):
"""We never handle password in this form"""
return
class PagePropForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
parent = forms.ModelChoiceField(queryset=Page.objects.all())
def __init__(self, *args, **kwargs):
super(PagePropForm, self).__init__(*args, **kwargs)
self.fields['parent'].required = False
class Meta:
model = Page
fields = ['parent', 'name', 'owner_group', 'edit_group', 'view_group']
def save(self, commit=True):
page = super(PagePropForm, self).save(commit=False)
if commit:
page.save()
return page
class PageEditForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
def __init__(self, *args, **kwargs):
super(PageEditForm, self).__init__(*args, **kwargs)
class Meta:
model = Page
fields = ['title', 'content', ]
def save(self, commit=True):
page = super(PageEditForm, self).save(commit=False)
if commit:
page.save()
return page

View File

@ -1,94 +1,79 @@
# This file contains all the views that concern the page model
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView
from core.models import Page
from core.views.forms import PageEditForm, PagePropForm
def page(request, page_name=None):
"""
This view displays a page or the link to create it if 404
"""
context = {'title': 'View a Page'}
if page_name == None:
context['page_list'] = Page.objects.all
return render(request, "core/page.html", context)
p = Page.get_page_by_full_name(page_name)
if p is not None:
context['view_page'] = True
if request.user.has_perm('can_view', p):
context['title'] = p.title
context['page'] = p
context['tests'] = "PAGE_FOUND : "+p.title
else:
context['title'] = "Access denied"
context['tests'] = "PAGE_FOUND_BUT_DENIED"
context['page'] = Page(title="Denied", content="You have no access to this page")
else:
context['title'] = "This page does not exist"
context['new_page'] = page_name
context['tests'] = "PAGE_NOT_FOUND"
return render(request, "core/page.html", context)
class PageListView(ListView):
model = Page
def page_edit(request, page_name=None):
"""
page_edit view, able to create a page, save modifications, and display the page ModelForm
"""
context = {'title': 'Edit a page',
'page_name': page_name}
p = Page.get_page_by_full_name(page_name)
# New page
if p == None:
parent_name = '/'.join(page_name.split('/')[:-1])
name = page_name.split('/')[-1]
if parent_name == "":
p = Page(name=name)
else:
parent = Page.get_page_by_full_name(parent_name)
p = Page(name=name, parent=parent)
# Saving page
if request.method == 'POST':
f = PageEditForm(request.POST, instance=p)
if f.is_valid():
f.save()
context['tests'] = "PAGE_SAVED"
else:
context['tests'] = "PAGE_NOT_SAVED"
# Default: display the edit form without change
else:
context['tests'] = "POST_NOT_RECEIVED"
f = PageEditForm(instance=p)
context['page'] = p
context['page_edit'] = f.as_p()
return render(request, 'core/page.html', context)
def get_context_data(self, **kwargs):
context = super(PageListView, self).get_context_data(**kwargs)
return context
def page_prop(request, page_name=None):
"""
page_prop view, able to change a page's properties
"""
context = {'title': 'Page properties',
'page_name': page_name}
p = Page.get_page_by_full_name(page_name)
# New page
if p == None:
parent_name = '/'.join(page_name.split('/')[:-1])
name = page_name.split('/')[-1]
if parent_name == "":
p = Page(name=name)
class PageView(DetailView):
model = Page
def get_object(self):
self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
return self.page
def get_context_data(self, **kwargs):
context = super(PageView, self).get_context_data(**kwargs)
if "page" in context.keys():
context['tests'] = "PAGE_FOUND : "+context['page'].title
else:
parent = Page.get_page_by_full_name(parent_name)
p = Page(name=name, parent=parent)
# Saving page
if request.method == 'POST':
f = PagePropForm(request.POST, instance=p)
if f.is_valid():
f.save()
context['tests'] = "PAGE_SAVED"
context['tests'] = "PAGE_NOT_FOUND"
context['new_page'] = self.kwargs['page_name']
return context
class PagePropView(UpdateView):
model = Page
fields = ['parent', 'name', 'owner_group', 'edit_group', 'view_group', ]
template_name_suffix = '_prop'
def __init__(self, *args, **kwargs):
super(PagePropView, self).__init__(*args, **kwargs)
def get_object(self):
page_name = self.kwargs['page_name']
p = Page.get_page_by_full_name(page_name)
if p == None:
parent_name = '/'.join(page_name.split('/')[:-1])
name = page_name.split('/')[-1]
if parent_name == "":
p = Page(name=name)
else:
parent = Page.get_page_by_full_name(parent_name)
p = Page(name=name, parent=parent)
self.page = p
return self.page
def get_context_data(self, **kwargs):
context = super(PagePropView, self).get_context_data(**kwargs)
if "page" in context.keys():
context['tests'] = "PAGE_FOUND : "+context['page'].title
else:
context['tests'] = "PAGE_NOT_SAVED"
# Default: display the edit form without change
else:
context['tests'] = "POST_NOT_RECEIVED"
f = PagePropForm(instance=p)
context['page'] = p
context['page_prop'] = f.as_p()
return render(request, 'core/page.html', context)
context['tests'] = "PAGE_NOT_FOUND"
context['new_page'] = self.kwargs['page_name']
return context
class PageEditView(UpdateView):
model = Page
fields = ['title', 'content',]
template_name_suffix = '_edit'
def get_object(self):
self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
return self.page
def get_context_data(self, **kwargs):
context = super(PageEditView, self).get_context_data(**kwargs)
if "page" in context.keys():
context['tests'] = "PAGE_FOUND : "+context['page'].title
else:
context['tests'] = "PAGE_NOT_FOUND"
context['new_page'] = self.kwargs['page_name']
return context

View File

@ -2,10 +2,6 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.db import models
import logging
logging.basicConfig(level=logging.DEBUG)
def index(request, context=None):
if context == None:
return render(request, "core/index.html", {'title': 'Bienvenue!'})