Code

Ticket #632: database-loader.diff

File database-loader.diff, 4.6 KB (added by Jannis Leidel <jl@…>, 7 years ago)

Database loader based on http://code.google.com/p/django-databasetemplateloader/

Line 
1Index: django/template/loaders/database/__init__.py
2===================================================================
3--- django/template/loaders/database/__init__.py        (Revision 0)
4+++ django/template/loaders/database/__init__.py        (Revision 0)
5@@ -0,0 +1,25 @@
6+# Wrapper for loading templates from the database.
7+
8+from django.template import TemplateDoesNotExist
9+from django.contrib.sites.models import Site
10+from django.conf import settings
11+from django.template.models import Template
12+
13+try:
14+    site = Site.objects.get_current()
15+except:
16+    site = None
17+
18+def load_template_source(template_name, template_dirs=None):
19+    """
20+    Loads templates from the database by querying the database field ``name``
21+    with a template path and ``sites`` with the current site.
22+    """
23+    if site is not None:
24+        try:
25+            t = Template.objects.get(name__exact=template_name, sites__pk=site.id)
26+            return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
27+        except:
28+            pass
29+    raise TemplateDoesNotExist, template_name
30+load_template_source.is_usable = True
31Index: django/template/loaders/database/models.py
32===================================================================
33--- django/template/loaders/database/models.py  (Revision 0)
34+++ django/template/loaders/database/models.py  (Revision 0)
35@@ -0,0 +1,28 @@
36+from django.db import models
37+from django.core import validators
38+from django.contrib.sites.models import Site
39+from django.utils.translation import gettext_lazy as _
40+
41+class Template(models.Model):
42+    """
43+    Defines a template model for use with the database template loader.
44+    The field ``name`` is the equivalent to the filename of a static template.
45+    """
46+    name = models.CharField(_('name'), unique=True, maxlength=100, help_text=_("Example: 'flatpages/default.html'"))
47+    content = models.TextField(_('content'))
48+    sites = models.ManyToManyField(Site)
49+    creation_date = models.DateTimeField(_('creation date'), auto_now_add=True)
50+    last_changed = models.DateTimeField(_('last changed'), auto_now=True)
51+    class Meta:
52+        db_table = 'django_template'
53+        verbose_name = _('template')
54+        verbose_name_plural = _('templates')
55+        ordering = ('name',)
56+    class Admin:
57+        fields = ((None, {'fields': ('name', 'content', 'sites')}),)
58+        list_display = ('name', 'creation_date', 'last_changed')
59+        list_filter = ('sites',)
60+        search_fields = ('name','content')
61+
62+    def __str__(self):
63+        return self.name
64Index: docs/templates_python.txt
65===================================================================
66--- docs/templates_python.txt   (Revision 4563)
67+++ docs/templates_python.txt   (Arbeitskopie)
68@@ -538,6 +538,41 @@
69     Just like ``app_directories`` above, but it loads templates from Python
70     eggs rather than from the filesystem.
71 
72+``django.template.loaders.database.load_template_source``
73+    Loads templates from the database and is represented by a standard Django
74+    model living in `django/template/models.py`_.
75+   
76+    Installation:
77+   
78+    1. Put ``'django.template.loaders.database'``,  ``'django.contrib.admin'``
79+       and ``'django.contrib.sites'`` in your ``INSTALLED_APPS`` setting.
80+    2. Add ``'django.template.loaders.database.load_template_source' to
81+        ``TEMPLATE_LOADERS``
82+    2. Run the command ``manage.py syncdb``.
83+       
84+    .. _INSTALLED_APPS: ../settings/#installed-apps
85+    .. _TEMPLATE_LOADERS: ../settings/#template-loaders
86+   
87+    ``manage.py syncdb`` creates two tables in your database:
88+    ``django_template`` and ``django_template_sites``. ``django_template`` is
89+    a simple lookup table that maps a template name to the text content of the
90+    template. The template name is the equivalent to the filename in the
91+    filesystem template loader. ``django_template_sites`` associates a
92+    template with a site.
93+   
94+    Templates are loaded by querying the database field ``name`` with a
95+    template path as if they were static files. A template with the name
96+    ``foo.html`` can be loaded with ``get_template('foo.html')``.
97+   
98+    You can find a new "Template" section on the admin index page. Edit
99+    templates as you edit any other object in the system.
100+
101+    You can access template database objects via the `Django database API`_.
102+
103+    .. _Django model: ../model_api/
104+    .. _django/template/loaders/database/models.py: http://code.djangoproject.com/browser/django/trunk/django/template/loaders/database/models.py
105+    .. _Django database API: ../db_api/
106+
107 Django uses the template loaders in order according to the ``TEMPLATE_LOADERS``
108 setting. It uses each loader until a loader finds a match.
109