Ticket #632: dbtemplates4.diff
File dbtemplates4.diff, 7.2 KB (added by , 17 years ago) |
---|
-
django/contrib/dbtemplates/sync_templates.py
1 """ 2 Helper function for syncing templates in TEMPLATES_DIRS with the dbtemplates 3 contrib app. 4 """ 5 6 from django.conf import settings 7 from django.template import TemplateDoesNotExist 8 from django.contrib.dbtemplates.models import Template 9 from django.contrib.sites.models import Site 10 11 import os 12 import sys 13 14 try: 15 site = Site.objects.get_current() 16 except: 17 site = None 18 19 def synctemplates(extension=".html", overwrite=False): 20 """ 21 Helper function for syncing templates in TEMPLATES_DIRS with the 22 dbtemplates contrib app. 23 """ 24 tried = [] 25 synced = [] 26 existing = [] 27 overwritten = [] 28 29 if site is not None: 30 for template_dir in settings.TEMPLATE_DIRS: 31 if os.path.isdir(template_dir): 32 for dirpath, subdirs, filenames in os.walk(template_dir): 33 for file in filenames: 34 if file.endswith(extension) and not file.startswith("."): 35 filepath = os.path.join(dirpath, file) 36 filename = filepath.split(template_dir)[1][1:] 37 try: 38 try: 39 t = Template.objects.get(name__exact=filename) 40 except Template.DoesNotExist: 41 filecontent = open(filepath, "r").read() 42 t = Template(name=filename, content=filecontent) 43 t.save() 44 t.sites.add(site) 45 synced.append(filename) 46 else: 47 if overwrite: 48 t.content = open(filepath, "r").read() 49 t.save() 50 t.sites.add(site) 51 overwritten.append(t.name) 52 else: 53 existing.append(t.name) 54 except IOError: 55 tried.append(filepath) 56 except: 57 raise TemplateDoesNotExist 58 59 if len(existing) > 0: 60 print "\nAlready existing templates:" 61 for _existing in existing: 62 print _existing 63 64 if len(overwritten) > 0: 65 print "\nOverwritten existing templates:" 66 for _replaced in overwritten: 67 print _replaced 68 69 if len(synced) > 0: 70 print "\nSuccessfully synced templates:" 71 for _synced in synced: 72 print _synced 73 74 if len(tried) > 0: 75 print "\nTried to sync but failed:" 76 for _tried in tried: 77 print _tried 78 79 if __name__ == "__main__": 80 synctemplates() -
django/contrib/dbtemplates/models.py
1 # -*- coding: utf-8 -*- 2 from django.db import models 3 from django.template import loader, Context 4 from django.core import validators 5 from django.contrib.sites.models import Site 6 from django.utils.translation import gettext_lazy as _ 7 8 class Template(models.Model): 9 """ 10 Defines a template model for use with the database template loader. 11 The field ``name`` is the equivalent to the filename of a static template. 12 """ 13 name = models.CharField(_('name'), unique=True, maxlength=100, help_text=_("Example: 'flatpages/default.html'")) 14 content = models.TextField(_('content')) 15 sites = models.ManyToManyField(Site) 16 creation_date = models.DateTimeField(_('creation date'), auto_now_add=True) 17 last_changed = models.DateTimeField(_('last changed'), auto_now=True) 18 class Meta: 19 db_table = 'django_template' 20 verbose_name = _('template') 21 verbose_name_plural = _('templates') 22 ordering = ('name',) 23 class Admin: 24 fields = ((None, {'fields': ('name', 'content', 'sites')}),) 25 list_display = ('name', 'creation_date', 'last_changed') 26 list_filter = ('sites',) 27 search_fields = ('name','content') 28 29 def __str__(self): 30 return self.name 31 32 __test__ = {'API_TESTS':""" 33 >>> test_site = Site.objects.get(pk=1) 34 >>> test_site 35 <Site: example.com> 36 >>> t1 = Template(name='base.html', content="<html><head></head><body>{% block content %}Welcome at {{ title }}{% endblock %}</body></html>") 37 >>> t1.save() 38 >>> t1.sites.add(test_site) 39 >>> t1 40 <Template: base.html> 41 >>> t2 = Template(name='sub.html', content='{% extends "base.html" %}{% block content %}This is {{ title }}{% endblock %}') 42 >>> t2.save() 43 >>> t2.sites.add(test_site) 44 >>> t2 45 <Template: sub.html> 46 >>> Template.objects.filter(sites=test_site) 47 [<Template: base.html>, <Template: sub.html>] 48 >>> t2.sites.all() 49 [<Site: example.com>] 50 >>> from django.contrib.dbtemplates.loader import load_template_source 51 >>> loader.template_source_loaders = [load_template_source] 52 >>> loader.get_template("base.html").render(Context({'title':'MainPage'})) 53 '<html><head></head><body>Welcome at MainPage</body></html>' 54 >>> loader.get_template("sub.html").render(Context({'title':'SubPage'})) 55 '<html><head></head><body>This is SubPage</body></html>' 56 """} -
django/contrib/dbtemplates/loader.py
1 # Wrapper for loading templates from the database. 2 3 from django.conf import settings 4 from django.template import TemplateDoesNotExist 5 from django.contrib.dbtemplates.models import Template 6 from django.contrib.sites.models import Site 7 8 try: 9 site = Site.objects.get_current() 10 except: 11 site = None 12 13 def load_template_source(template_name, template_dirs=None): 14 """ 15 Loads templates from the database by querying the database field ``name`` 16 with a template path and ``sites`` with the current site. 17 """ 18 if site is not None: 19 try: 20 t = Template.objects.get(name__exact=template_name, sites__pk=site.id) 21 return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name)) 22 except: 23 pass 24 raise TemplateDoesNotExist, template_name 25 load_template_source.is_usable = True -
tests/runtests.py
24 24 'django.contrib.sessions', 25 25 'django.contrib.comments', 26 26 'django.contrib.admin', 27 'django.contrib.dbtemplates', 27 28 ] 28 29 29 30 def get_test_models():