Created
November 30, 2009 20:39
-
-
Save SmileyChris/245725 to your computer and use it in GitHub Desktop.
a Django site_settings app
This file contains 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.conf import settings | |
from django.contrib.sites.models import Site | |
from django.core.cache import cache | |
from project.apps.site_settings import models | |
KEY = 'site-settings-%d' % settings.SITE_ID | |
def get_settings(from_cache=True): | |
""" | |
Return the current site settings, firstly trying from cache, next trying to | |
retrieve it from the database, finally just returning an unsaved | |
SiteSettings model. If the model is retrieved from the database, it is be | |
cached for future requests. | |
>>> cache.set(KEY, None) | |
# When no site settings exist, an unsaved SiteSettings model is returned. | |
>>> s = get_settings() | |
>>> print s.pk | |
None | |
# Retrieve the settings from the database. | |
>>> s = models.SiteSettings.objects.create() | |
>>> get_settings().pk == s.pk | |
True | |
# Further requests are retrieved it from cache. | |
>>> print get_settings().archive_days | |
30 | |
>>> s.archive_days = 90 | |
>>> s.save() | |
>>> print get_settings().archive_days | |
30 | |
""" | |
if from_cache: | |
site_settings = cache.get(KEY) | |
if site_settings: | |
return site_settings | |
try: | |
site_settings = models.SiteSettings.objects.get( | |
site__pk=settings.SITE_ID) | |
except models.SiteSettings.DoesNotExist: | |
site = Site.objects.get_current() | |
site_settings = models.SiteSettings(site=site) | |
else: | |
cache.set(KEY, site_settings) | |
return site_settings | |
def flush_settings(): | |
""" | |
Clear the current site settings from cache. | |
>>> cache.set(KEY, None) | |
# Save SiteSettings, then cache it. | |
>>> s = models.SiteSettings.objects.create() | |
>>> print get_settings().archive_days | |
30 | |
# Now change a property. | |
>>> s.archive_days = 90 | |
>>> s.save() | |
# get_settings will still be returning the cached one. | |
>>> print get_settings(None).entries_open | |
30 | |
# Flushing will make get_settings retrieve the record from the database | |
# again. | |
>>> flush_settings() | |
>>> print get_settings(None).entries_open | |
30 | |
""" | |
cache.set(KEY, None) |
This file contains 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 import admin | |
from project.apps.site_settings import models | |
admin.site.register(models.SiteSettings) |
This file contains 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.db import models | |
class SiteSettings(models.Model): | |
site = models.OneToOneField('sites.Site', unique=True, | |
related_name='site_settings') | |
# Your settings fields go here (with default values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment