This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TitleManager(models.Manager): | |
def get_title(self, page, language, language_fallback=False, latest_by='creation_date'): | |
""" | |
Gets the latest content for a particular page and language. Falls back | |
to another language if wanted. | |
""" | |
try: | |
title = self.get(language=language, page=page) | |
return title | |
except self.model.DoesNotExist: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Title(models.Model): | |
language = models.CharField(_("language"), max_length=3, db_index=True) | |
title = models.CharField(_("title"), max_length=255) | |
slug = models.SlugField(_("slug"), max_length=255, db_index=True, unique=False) | |
page = models.ForeignKey(Page, verbose_name=_("page"), related_name="title_set") | |
creation_date = models.DateTimeField(_("creation date"), editable=False, default=datetime.now) | |
objects = TitleManager() | |
def __unicode__(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if settings.CMS_PERMISSION: | |
class PagePermission(models.Model): | |
""" | |
Page permission object | |
""" | |
TYPES = ( | |
(0, _('All')), | |
(1, _('This page only')), | |
(2, _('This page and all childrens')), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PageManager(models.Manager): | |
def on_site(self, site=None): | |
if hasattr(site, 'domain'): | |
return self.filter(**{'sites__domain__exact': site.domain}) | |
return self | |
def root(self, site=None): | |
""" | |
Return a queryset with pages that don't have parents, a.k.a. root. | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.sites.models import Site, RequestSite, SITE_CACHE | |
def get_site_from_request(request, check_subdomain=True): | |
""" | |
Returns the ``Site`` which matches the host name retreived from | |
``request``. | |
If no match is found and ``check_subdomain`` is ``True``, the sites are | |
searched again for sub-domain matches. | |
If still no match, or if more than one ``Site`` matched the host name, a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Page(models.Model): | |
""" | |
A simple hierarchical page model | |
""" | |
# some class constants to refer to, e.g. Page.DRAFT | |
DRAFT = 0 | |
PUBLISHED = 1 | |
EXPIRED = 2 | |
STATUSES = ( | |
(DRAFT, _('Draft')), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def auto_render(func): | |
"""Decorator that put automaticaly the template path in the context dictionary | |
and call the render_to_response shortcut""" | |
def _dec(request, *args, **kwargs): | |
t = None | |
if kwargs.get('only_context', False): | |
# return only context dictionary | |
del(kwargs['only_context']) | |
response = func(request, *args, **kwargs) | |
if isinstance(response, HttpResponse) or isinstance(response, HttpResponseRedirect): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_template_from_request(request, obj=None): | |
""" | |
Gets a valid template from different sources or falls back to the default | |
template. | |
""" | |
if settings.CMS_TEMPLATES is None: | |
return settings.DEFAULT_CMS_TEMPLATE | |
template = request.REQUEST.get('template', None) | |
if template is not None and \ | |
template in dict(settings.CMS_TEMPLATES).keys(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_language_from_request(request, current_page=None): | |
""" | |
Return the most obvious language according the request | |
""" | |
language = get_language_in_settings(request.REQUEST.get('language', None)) | |
if language is None: | |
language = getattr(request, 'LANGUAGE_CODE', None) | |
if language is None: | |
# in last resort, get the first language available in the page | |
if current_page: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cms import settings | |
from cms.models import Page, Content, Title | |
from cms.utils import auto_render, get_template_from_request, get_language_from_request | |
def details(request, page_id=None, slug=None, template_name=settings.DEFAULT_CMS_TEMPLATE): | |
lang = get_language_from_request(request) | |
site = request.site | |
pages = Page.objects.root(site).order_by("tree_id") | |
if pages: |
NewerOlder