initial commit

git-svn-id: https://django-dbtemplates.googlecode.com/svn/trunk@2 cfb8ba98-e953-0410-9cff-959ffddf5974

committer: leidel <leidel@cfb8ba98-e953-0410-9cff-959ffddf5974>

--HG--
extra : convert_revision : 0394ad2382d7c7fc9d0ea40cf43777f7621d2ec3
This commit is contained in:
leidel 2007-02-23 19:03:20 +00:00
parent 5b790d69c2
commit f4e0431359
5 changed files with 87 additions and 0 deletions

0
template/__init__.py Normal file
View file

View file

View file

@ -0,0 +1,23 @@
from django.template import TemplateDoesNotExist
from django.contrib.sites.models import Site
from django.conf import settings
from template.models import Template
try:
site = Site.objects.get_current()
except:
site = None
def load_template_source(template_name, template_dirs=None):
"""
Loader which gets the template content from the database depending on
the current ``Site``.
"""
if site is not None:
try:
t = Template.objects.get(name__exact=template_name, sites__pk=site.id)
return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
except:
pass
raise TemplateDoesNotExist, template_name
load_template_source.is_usable = True

36
template/management.py Normal file
View file

@ -0,0 +1,36 @@
"""
Creates the default database template objects.
Don't know if it works.
"""
from django.dispatch import dispatcher
from django.db.models import signals
from django.contrib.sites.models import Site
from template.models import Template
from template import models as template_app
def create_default_templates(app, created_models, verbosity):
try:
site = Site.objects.get_current()
except Site.DoesNotExist:
site = None
if site is not None:
if Template in created_models:
if verbosity >= 2:
print "Creating example database templates for error 404 and error 500"
template404 = Template(name="404.html",content="""
{% load i18n %}<h2>{% trans 'Page not found' %}</h2>
<p>{% trans "We're sorry, but the requested page could not be found." %}</p>""")
template404.save()
template404.sites.add(site)
template500 = Template(name="500.html",content="""{% load i18n %}
<h1>{% trans 'Server Error <em>(500)</em>' %}</h1>
<p>{% trans "There's been an error." %}</p>""")
template500.save()
template500.sites.add(site)
dispatcher.connect(create_default_templates, sender=template_app, signal=signals.post_syncdb)

28
template/models.py Normal file
View file

@ -0,0 +1,28 @@
from django.db import models
from django.core import validators
from django.contrib.sites.models import Site
from django.utils.translation import gettext_lazy as _
class Template(models.Model):
"""
Defines a template model for use with the database template loader.
The field ``name`` is the equivalent to the filename of a static template.
"""
name = models.CharField(_('name'), unique=True, maxlength=100, help_text=_("Example: 'flatpages/default.html'"))
content = models.TextField(_('content'))
sites = models.ManyToManyField(Site)
creation_date = models.DateTimeField(_('creation date'), auto_now_add=True)
last_changed = models.DateTimeField(_('last changed'), auto_now=True)
class Meta:
db_table = 'django_template'
verbose_name = _('template')
verbose_name_plural = _('templates')
ordering = ('name',)
class Admin:
fields = ((None, {'fields': ('name', 'content', 'sites')}),)
list_display = ('name', 'creation_date', 'last_changed')
list_filter = ('sites',)
search_fields = ('name','content')
def __str__(self):
return self.name