Skip to content
Browse files

Replaced page view with classed based view

- added FiberPageMixin
- updated advanced-usage.rst with example of how to use the FiberPageMixin
- removed context_processors
- removed documentation for Django-1.2 (no longer support)
- this commit is backward incompatible with django==1.2
- this commit is backward incompatible with templates that rely on the old fiber context-processor
- issue #72
  • Loading branch information...
1 parent d554f0a commit c12362512b4189c3c6d1826e245fdbdbc6137ae2 @mvdwaeter mvdwaeter committed Jun 27, 2012
Showing with 56 additions and 209 deletions.
  1. +0 −1 README.rst
  2. +0 −113 docs/INSTALL-Django-1.2.x.rst
  3. +18 −1 docs/advanced-usage.rst
  4. +0 −67 fiber/context_processors.py
  5. +38 −27 fiber/views.py
View
1 README.rst
@@ -51,7 +51,6 @@ settings.py
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
- 'fiber.context_processors.page_info',
)
INSTALLED_APPS = (
View
113 docs/INSTALL-Django-1.2.x.rst
@@ -1,113 +0,0 @@
-==========================================
-Installation instructions for Django 1.2.x
-==========================================
-
-
-Installation:
-=============
-
-These are the installation instructions for Django 1.2.x.
-
-::
-
- $ pip install django-fiber
-
-
-Requirements:
-=============
-
-These dependencies are automatically installed:
-
-::
-
- PIL>=1.1.7
- djangorestframework==0.3.3
- django-compressor>=0.7.1
-
-You need to install one dependency by hand when you're using Django 1.2.x:
-
-::
-
- django-staticfiles>=1.0.1
-
-
-Settings:
-=========
-
-settings.py
------------
-
-::
-
- import django.conf.global_settings as DEFAULT_SETTINGS
-
- MIDDLEWARE_CLASSES = DEFAULT_SETTINGS.MIDDLEWARE_CLASSES + (
- 'fiber.middleware.ObfuscateEmailAddressMiddleware',
- 'fiber.middleware.AdminPageMiddleware',
- 'fiber.middleware.PageFallbackMiddleware',
- )
-
- TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
- 'django.core.context_processors.request',
- 'staticfiles.context_processors.static_url',
- 'fiber.context_processors.page_info',
- )
-
- INSTALLED_APPS = (
- ...
- 'staticfiles',
- 'piston',
- 'mptt',
- 'compressor',
- 'fiber',
- ...
- )
-
- import os
- BASE_DIR = os.path.abspath(os.path.dirname(__file__))
-
- STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- STATIC_URL = '/static/'
- STATICFILES_FINDERS = (
- 'staticfiles.finders.FileSystemFinder',
- 'staticfiles.finders.AppDirectoriesFinder',
- 'compressor.finders.CompressorFinder',
- )
-
-urls.py
--------
-
-::
-
- from django.conf import settings
-
- urlpatterns = patterns('',
- ...
- (r'^api/v2/', include('fiber.rest_api.urls')),
- (r'^admin/fiber/', include('fiber.admin_urls')),
- (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', {'packages': ('fiber',),}),
- ...
- )
-
- if settings.DEBUG:
- urlpatterns += patterns('staticfiles.views',
- url(r'^static/(?P<path>.*)$', 'serve'),
- )
-
-
-Post-installation:
-==================
-
-Create database tables::
-
- $ python manage.py syncdb
-
-All static Fiber files need to be symlinked in (or copied to) your media folder::
-
- $ python manage.py collectstatic --link
-
-
-Further documentation:
-======================
-
-You can find usage instructions, and other documentation, in the docs folder.
View
19 docs/advanced-usage.rst
@@ -48,6 +48,23 @@ In this example, the news_item_detail view looks up the Page of the news_item_li
return HttpResponse(t.render(c))
+Set or override fiber_page in the classed based view:
+=====================================================
+
+In this example, the NewsItemDetailView's context is enriched with fiber_page and fiber_current_pages.
+
+::
+ from django.core.urlresolvers import reverse
+ from django.views.generic import DetailView
+ from fiber.views import FiberPageMixin
+
+
+ class NewsItemDetailView(FiberPageMixin, DetailView):
+
+ def get_fiber_page_url(self):
+ return reverse('news_item_list')
+
+
Templates:
==========
@@ -150,4 +167,4 @@ You can also override the entire CKEditor toolbar, by setting the variable:
window.CKEDITOR_CONFIG_TOOLBAR
To see how this works, check the fiber.ckeditor.js file in the Django Fiber source:
-https://github.com/ridethepony/django-fiber/blob/master/fiber/static/fiber/js/fiber.ckeditor.js
+https://github.com/ridethepony/django-fiber/blob/master/fiber/static/fiber/js/fiber.ckeditor.js
View
67 fiber/context_processors.py
@@ -1,67 +0,0 @@
-import re
-
-from app_settings import EXCLUDE_URLS
-from models import Page
-
-
-def page_info(request):
- context = {}
- page = None
- current_pages = []
-
- url = request.path_info
-
- """
- Avoid further processing or database queries if page is in EXCLUDE_URLS.
- """
- if EXCLUDE_URLS:
- for exclude_url in EXCLUDE_URLS:
- if re.search(exclude_url, url.lstrip('/')):
- return context
-
- page = Page.objects.get_by_url(url)
-
- """
- Block access to pages that the current user isn't supposed to see.
- """
- if page:
- if not page.is_public_for_user(request.user):
- page = None
-
- """
- Find pages that should be marked as current in menus.
- """
- if page:
- """
- The current page should be marked as current, obviously,
- as well as all its ancestors.
- """
- current_pages.append(page)
- current_pages.extend(page.get_ancestors())
-
- """
- For all pages that are not already current_pages,
- check if one of the `mark_current_regexes` matches the requested URL.
- If so, add the page and all its ancestors to the current_pages list.
- """
- current_page_candidates = Page.objects.exclude(mark_current_regexes__exact='')
- for current_page_candidate in list(set(current_page_candidates) - set(current_pages)):
- for mark_current_regex in current_page_candidate.mark_current_regexes.strip().splitlines():
- if re.match(mark_current_regex, url):
- current_pages.append(current_page_candidate)
- current_pages.extend(current_page_candidate.get_ancestors())
- break
-
- """
- Order current_pages for use with tree_info template tag,
- and remove the root node in the process.
- """
- current_pages = sorted(current_pages, key=lambda current_page: current_page.lft)[1:]
-
- if page:
- context['fiber_page'] = page
-
- if current_pages:
- context['fiber_current_pages'] = current_pages
-
- return context
View
65 fiber/views.py
@@ -1,33 +1,44 @@
from django.conf import settings
-from django.core.xheaders import populate_xheaders
-from django.http import HttpResponse, HttpResponsePermanentRedirect, Http404
-from django.template import loader, RequestContext
+from django.http import HttpResponsePermanentRedirect, Http404
+from django.views.generic.base import TemplateView
-from app_settings import DEFAULT_TEMPLATE
-from models import Page
+from .app_settings import DEFAULT_TEMPLATE
+from .mixins import FiberPageMixin
-def page(request):
- url = request.path_info
+class FiberTemplateView(FiberPageMixin, TemplateView):
- context = RequestContext(request)
- if 'fiber_page' not in context:
- """
- Take care of Django's CommonMiddleware redirect if the request URL doesn't end in a slash, and APPEND_SLASH=True
- https://docs.djangoproject.com/en/dev/ref/settings/#append-slash
- """
- if not url.endswith('/') and settings.APPEND_SLASH:
- return HttpResponsePermanentRedirect('%s/' % url)
+ def get_fiber_page_url(self):
+ return self.request.path_info
+
+ def get_template_names(self):
+ if self.get_fiber_page() and self.get_fiber_page().template_name not in [None, '']:
+ return self.get_fiber_page().template_name
+ else:
+ return DEFAULT_TEMPLATE
+
+ def render_to_response(self, *args, **kwargs):
+ if self.get_fiber_page() == None:
+ """
+ Take care of Django's CommonMiddleware redirect if the request URL doesn't end in a slash, and APPEND_SLASH=True
+ https://docs.djangoproject.com/en/dev/ref/settings/#append-slash
+ """
+ url = self.get_fiber_page_url()
+
+ if not url.endswith('/') and settings.APPEND_SLASH:
+ return HttpResponsePermanentRedirect('%s/' % url)
+ else:
+ raise Http404
else:
- raise Http404
- else:
- page = context['fiber_page']
- if page.redirect_page and page.redirect_page != page: #prevent redirecting to itself
- return HttpResponsePermanentRedirect(page.redirect_page.get_absolute_url())
-
- t = loader.get_template(page.template_name or DEFAULT_TEMPLATE)
- context['page'] = page
-
- response = HttpResponse(t.render(context))
- populate_xheaders(request, response, Page, page.id)
- return response
+ """
+ Block access to pages that the current user isn't supposed to see.
+ """
+ if not self.get_fiber_page().is_public_for_user(self.request.user):
+ raise Http404
+
+ if self.get_fiber_page().redirect_page and self.get_fiber_page().redirect_page != self.get_fiber_page(): # prevent redirecting to itself
+ return HttpResponsePermanentRedirect(self.get_fiber_page().redirect_page.get_absolute_url())
+
+ return super(FiberTemplateView, self).render_to_response(*args, **kwargs)
+
+page = FiberTemplateView.as_view()

0 comments on commit c123625

Please sign in to comment.
Something went wrong with that request. Please try again.