From b237cdbaaee6dcd84f1f2b17712f29e336f1485c Mon Sep 17 00:00:00 2001 From: Skia Date: Wed, 25 Nov 2015 14:45:18 +0100 Subject: [PATCH] Refactor page view with Django's black magic powered DetailView&co --- core/models.py | 9 +- core/templates/core/page.html | 44 ++------ core/templates/core/page_detail.html | 16 +++ core/templates/core/page_edit.html | 13 +++ core/templates/core/page_list.html | 21 ++++ core/templates/core/page_prop.html | 13 +++ core/tests.py | 10 +- core/urls.py | 24 ++--- core/views/forms.py | 39 ------- core/views/page.py | 155 ++++++++++++--------------- core/views/site.py | 4 - 11 files changed, 163 insertions(+), 185 deletions(-) create mode 100644 core/templates/core/page_detail.html create mode 100644 core/templates/core/page_edit.html create mode 100644 core/templates/core/page_list.html create mode 100644 core/templates/core/page_prop.html diff --git a/core/models.py b/core/models.py index cd6134bd..18abe694 100644 --- a/core/models.py +++ b/core/models.py @@ -4,6 +4,7 @@ from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from django.core import validators from django.core.exceptions import ValidationError +from django.core.urlresolvers import reverse from datetime import datetime class User(AbstractBaseUser, PermissionsMixin): @@ -188,8 +189,6 @@ class Page(models.Model): p = p.parent return l - - def save(self, *args, **kwargs): self.full_clean() # This reset the full_name just before saving to maintain a coherent field quicker for queries than the @@ -200,6 +199,12 @@ class Page(models.Model): c.save() super(Page, self).save(*args, **kwargs) + def get_absolute_url(self): + """ + This is needed for black magic powered UpdateView's children + """ + return reverse('core:page', kwargs={'page_name': self.full_name}) + def __str__(self): return self.get_full_name() diff --git a/core/templates/core/page.html b/core/templates/core/page.html index b0f9d413..dcb2932b 100644 --- a/core/templates/core/page.html +++ b/core/templates/core/page.html @@ -6,48 +6,16 @@ {% elif page_list %} Page list {% else %} - {{ title }} + Not found {% endif %} {% endblock %} {% block content %} - {% if view_page %} -

Page

-

Back to list

- {% if user.is_superuser %} -

Edit

-

Prop

- {% endif %} -

You're seeing the page {{ page.get_display_name }}

-

{{ page.title }}

-

{{ page.content }}

- {% elif page_list %} -

Page list

- - {% elif new_page %} -

{{ title }}

-

Create it?

- {% elif page_prop %} -

Page properties

-
- {% csrf_token %} - {{ page_prop }} -

-
- {% elif page_edit %} -

Edit page

-
- {% csrf_token %} - {{ page_edit }} -

-
+ {% if page %} + {% block page %} + {% endblock %} {% else %} - There is no page in this website. +

Page does not exist

+

Create it?

{% endif %} {% endblock %} - - diff --git a/core/templates/core/page_detail.html b/core/templates/core/page_detail.html new file mode 100644 index 00000000..e8089973 --- /dev/null +++ b/core/templates/core/page_detail.html @@ -0,0 +1,16 @@ +{% extends "core/page.html" %} + +{% block page %} +

Page

+

Back to list

+ {% if user.is_superuser %} +

Edit

+

Prop

+ {% endif %} +

You're seeing the page {{ page.get_display_name }}

+

{{ page.title }}

+

{{ page.content }}

+{% endblock %} + + + diff --git a/core/templates/core/page_edit.html b/core/templates/core/page_edit.html new file mode 100644 index 00000000..fa0f8bc4 --- /dev/null +++ b/core/templates/core/page_edit.html @@ -0,0 +1,13 @@ +{% extends "core/page.html" %} + +{% block page %} +

Edit page

+
+ {% csrf_token %} + {{ form.as_p }} +

+
+{% endblock %} + + + diff --git a/core/templates/core/page_list.html b/core/templates/core/page_list.html new file mode 100644 index 00000000..9c9fdbaa --- /dev/null +++ b/core/templates/core/page_list.html @@ -0,0 +1,21 @@ +{% extends "core/base.html" %} + +{% block title %} + Page list +{% endblock %} + +{% block content %} + {% if page_list %} +

Page list

+ + {% else %} + There is no page in this website. + {% endif %} +{% endblock %} + + + diff --git a/core/templates/core/page_prop.html b/core/templates/core/page_prop.html new file mode 100644 index 00000000..98436b94 --- /dev/null +++ b/core/templates/core/page_prop.html @@ -0,0 +1,13 @@ +{% extends "core/page.html" %} + +{% block content %} +

Page properties

+
+ {% csrf_token %} + {{ form.as_p }} +

+
+{% endblock %} + + + diff --git a/core/tests.py b/core/tests.py index 140950b0..989569ab 100644 --- a/core/tests.py +++ b/core/tests.py @@ -161,7 +161,7 @@ class UserRegistrationTest(SimpleTestCase): Should create a page correctly """ c = Client() - response = c.post(reverse('core:page_edit', kwargs={'page_name': 'guy'}), {'parent': '', + response = c.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {'parent': '', 'name': 'guy', 'title': 'Guy', 'Content': 'Guyéuyuyé', @@ -174,12 +174,12 @@ class UserRegistrationTest(SimpleTestCase): Should create a page correctly """ c = Client() - c.post(reverse('core:page_edit', kwargs={'page_name': 'guy'}), {'parent': '', + c.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {'parent': '', 'name': 'guy', 'title': 'Guy', 'Content': 'Guyéuyuyé', }) - response = c.post(reverse('core:page_edit', kwargs={'page_name': 'guy/bibou'}), {'parent': '1', + response = c.post(reverse('core:page_prop', kwargs={'page_name': 'guy/bibou'}), {'parent': '1', 'name': 'bibou', 'title': 'Bibou', 'Content': @@ -193,12 +193,12 @@ class UserRegistrationTest(SimpleTestCase): Should display a page correctly """ c = Client() - c.post(reverse('core:page_edit', kwargs={'page_name': 'guy'}), {'parent': '', + c.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {'parent': '', 'name': 'guy', 'title': 'Guy', 'Content': 'Guyéuyuyé', }) - c.post(reverse('core:page_edit', kwargs={'page_name': 'guy/bibou'}), {'parent': '1', + c.post(reverse('core:page_prop', kwargs={'page_name': 'guy/bibou'}), {'parent': '1', 'name': 'bibou', 'title': 'Bibou', 'Content': diff --git a/core/urls.py b/core/urls.py index a33f540b..032531c1 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,18 +1,18 @@ from django.conf.urls import url -from . import views +from core.views import * urlpatterns = [ - url(r'^$', views.index, name='index'), - url(r'^login$', views.login, name='login'), - url(r'^logout$', views.logout, name='logout'), - url(r'^register$', views.register, name='register'), - url(r'^user/$', views.user, name='user_list'), - url(r'^user/(?P[0-9]+)/$', views.user, name='user_profile'), - url(r'^user/(?P[0-9]+)/edit$', views.user_edit, name='user_edit'), - url(r'^page/$', views.page, name='page_list'), - url(r'^page/(?P[a-z0-9/]*)/$', views.page, name='page'), - url(r'^page/(?P[a-z0-9/]*)/edit$', views.page_edit, name='page_edit'), - url(r'^page/(?P[a-z0-9/]*)/prop$', views.page_prop, name='page_prop'), + url(r'^$', index, name='index'), + url(r'^login$', login, name='login'), + url(r'^logout$', logout, name='logout'), + url(r'^register$', register, name='register'), + url(r'^user/$', user, name='user_list'), + url(r'^user/(?P[0-9]+)/$', user, name='user_profile'), + url(r'^user/(?P[0-9]+)/edit$', user_edit, name='user_edit'), + url(r'^page/$', PageListView.as_view(), name='page_list'), + url(r'^page/(?P[a-z0-9/]*)/$', PageView.as_view(), name='page'), + url(r'^page/(?P[a-z0-9/]*)/edit$', PageEditView.as_view(), name='page_edit'), + url(r'^page/(?P[a-z0-9/]*)/prop$', PagePropView.as_view(), name='page_prop'), ] diff --git a/core/views/forms.py b/core/views/forms.py index ff7dc211..b4912e7f 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -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 - - diff --git a/core/views/page.py b/core/views/page.py index 76fc5ca6..90f03906 100644 --- a/core/views/page.py +++ b/core/views/page.py @@ -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 + diff --git a/core/views/site.py b/core/views/site.py index 7f237928..17fc13a9 100644 --- a/core/views/site.py +++ b/core/views/site.py @@ -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!'})