Code

Ticket #632: dbtemplates.diff

File dbtemplates.diff, 4.7 KB (added by Jannis Leidel <jl@…>, 7 years ago)

new django.contrib.dbtemplates with template model

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