Skip to content

Instantly share code, notes, and snippets.

@fiee
Created October 28, 2010 09:36
Show Gist options
  • Select an option

  • Save fiee/651015 to your computer and use it in GitHub Desktop.

Select an option

Save fiee/651015 to your computer and use it in GitHub Desktop.
site dependency for FeinCMS pages (feincms.modules.page.extensions)
# -*- coding: utf-8 -*-
"""
Extension for FeinCMS Page
"""
from django.conf import settings
from django.db import models
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.utils.translation import ugettext_lazy as _
from feincms.module.page.models import Page, PageManager
from feincms._internal import monkeypatch_method
class PageSiteManager(PageManager, CurrentSiteManager):
"""
Limit objects to those belonging to current site (via model.site.id==settings.SITE_ID)
`site_field_name` is the name of the model's field that's a foreign key to `django.contrib.sites.models.Site`
"""
def __init__(self, site_field_name='site'):
super(PageSiteManager, self).__init__(site_field_name)
self.add_to_active_filters(models.Q(site=Site.objects.get_current(), deleted=False))
def get_query_set(self):
"""Return all objects that belong to the current site"""
return super(PageSiteManager, self).get_query_set()
def register(cls, admin_cls):
cls.add_to_class('site',
models.ForeignKey(Site,
verbose_name=_(u'tenant’s site'),
#editable=False,
default=settings.SITE_ID,
help_text=_(u'site of the related customer/project/team')))
cls.add_to_class('objects', PageSiteManager())
# next two lines by Bohan Mihelac:
PageManager.add_to_active_filters(models.Q(site=Site.objects.get_current()))
admin_cls.list_display.extend(['site'])
@monkeypatch_method(admin_cls)
def save_model(self, request, obj, form, change):
obj.site = Site.objects.get_current()
obj.save()
@monkeypatch_method(cls)
def save(self, *args, **kwargs):
self.site = Site.objects.get_current()
super(Page, self).save(*args, **kwargs)
# from here this is a copy of the original
cached_page_urls = {}
# determine own URL
if self.override_url:
self._cached_url = self.override_url
elif self.is_root_node():
self._cached_url = u'/%s/' % self.slug
else:
self._cached_url = u'%s%s/' % (self.parent._cached_url, self.slug)
cached_page_urls[self.id] = self._cached_url
super(cls, self).save(*args, **kwargs)
# Okay, we changed the URL -- remove the old stale entry from the cache
if settings.FEINCMS_USE_CACHE:
ck = path_to_cache_key( self._original_cached_url.strip('/') )
django_cache.delete(ck)
# If our cached URL changed we need to update all descendants to
# reflect the changes. Since this is a very expensive operation
# on large sites we'll check whether our _cached_url actually changed
# or if the updates weren't navigation related:
if self._cached_url == self._original_cached_url:
return
pages = self.get_descendants().order_by('lft')
for page in pages:
if page.override_url:
page._cached_url = page.override_url
else:
# cannot be root node by definition
page._cached_url = u'%s%s/' % (
cached_page_urls[page.parent_id],
page.slug)
cached_page_urls[page.id] = page._cached_url
super(cls, page).save() # do not recurse
@fiee
Copy link
Copy Markdown
Author

fiee commented Apr 7, 2011

Bojan Mihelac enhanced this extension and made it part of his fork of FeinCMS (will hopefully land in trunk soon):
bmihelac/feincms@2423ba6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment