2015-07-06 20:16:43 +00:00
|
|
|
from dbtemplates.conf import settings
|
2025-02-16 20:08:16 +00:00
|
|
|
from dbtemplates.utils.cache import (
|
|
|
|
|
add_template_to_cache,
|
|
|
|
|
remove_cached_template,
|
|
|
|
|
)
|
2015-07-06 20:16:43 +00:00
|
|
|
from dbtemplates.utils.template import get_template_source
|
2025-02-16 20:08:16 +00:00
|
|
|
|
2015-07-06 20:16:43 +00:00
|
|
|
from django.contrib.sites.managers import CurrentSiteManager
|
|
|
|
|
from django.contrib.sites.models import Site
|
2007-07-20 14:30:48 +00:00
|
|
|
from django.db import models
|
2008-11-02 00:05:38 +00:00
|
|
|
from django.db.models import signals
|
2008-10-24 07:56:51 +00:00
|
|
|
from django.template import TemplateDoesNotExist
|
2025-02-16 20:08:16 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2012-05-07 08:57:23 +00:00
|
|
|
|
2010-06-24 09:43:41 +00:00
|
|
|
|
2025-05-29 21:31:49 +00:00
|
|
|
class Template(models.Model):
|
2007-07-20 14:30:48 +00:00
|
|
|
"""
|
|
|
|
|
Defines a template model for use with the database template loader.
|
|
|
|
|
The field ``name`` is the equivalent to the filename of a static template.
|
|
|
|
|
"""
|
2025-05-26 18:13:03 +00:00
|
|
|
id = models.AutoField(primary_key=True, verbose_name=_('ID'),
|
|
|
|
|
serialize=False, auto_created=True)
|
2010-12-14 00:32:27 +00:00
|
|
|
name = models.CharField(_('name'), max_length=100,
|
2009-04-11 09:13:33 +00:00
|
|
|
help_text=_("Example: 'flatpages/default.html'"))
|
2008-10-24 07:56:51 +00:00
|
|
|
content = models.TextField(_('content'), blank=True)
|
2022-06-15 12:43:13 +00:00
|
|
|
sites = models.ManyToManyField(Site, verbose_name=_('sites'),
|
2015-07-06 20:16:43 +00:00
|
|
|
blank=True)
|
2025-05-26 17:59:18 +00:00
|
|
|
creation_date = models.DateTimeField(_('creation date'), auto_now_add=True)
|
|
|
|
|
last_changed = models.DateTimeField(_('last changed'), auto_now=True)
|
2008-05-13 23:42:57 +00:00
|
|
|
|
2008-12-26 17:39:48 +00:00
|
|
|
objects = models.Manager()
|
|
|
|
|
on_site = CurrentSiteManager('sites')
|
|
|
|
|
|
2007-07-20 14:30:48 +00:00
|
|
|
class Meta:
|
2025-05-29 21:31:49 +00:00
|
|
|
db_table = 'django_template'
|
2007-07-20 14:30:48 +00:00
|
|
|
verbose_name = _('template')
|
|
|
|
|
verbose_name_plural = _('templates')
|
|
|
|
|
ordering = ('name',)
|
2008-05-13 23:42:57 +00:00
|
|
|
|
2018-02-12 19:12:38 +00:00
|
|
|
def __str__(self):
|
2007-07-20 14:30:48 +00:00
|
|
|
return self.name
|
2010-09-21 12:21:30 +00:00
|
|
|
|
|
|
|
|
def populate(self, name=None):
|
|
|
|
|
"""
|
|
|
|
|
Tries to find a template with the same name and populates
|
|
|
|
|
the content field if found.
|
|
|
|
|
"""
|
|
|
|
|
if name is None:
|
|
|
|
|
name = self.name
|
|
|
|
|
try:
|
2011-04-11 17:18:55 +00:00
|
|
|
source = get_template_source(name)
|
2010-09-21 12:21:30 +00:00
|
|
|
if source:
|
|
|
|
|
self.content = source
|
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
|
2008-10-04 10:01:49 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2008-11-02 00:05:38 +00:00
|
|
|
# If content is empty look for a template with the given name and
|
|
|
|
|
# populate the template instance with its content.
|
2011-07-01 13:44:09 +00:00
|
|
|
if settings.DBTEMPLATES_AUTO_POPULATE_CONTENT and not self.content:
|
2010-09-21 12:21:30 +00:00
|
|
|
self.populate()
|
2022-06-15 12:43:13 +00:00
|
|
|
super().save(*args, **kwargs)
|
2007-07-20 14:30:48 +00:00
|
|
|
|
2009-10-19 00:30:52 +00:00
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
def add_default_site(instance, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Called via Django's signals to cache the templates, if the template
|
|
|
|
|
in the database was added or changed, only if
|
|
|
|
|
DBTEMPLATES_ADD_DEFAULT_SITE setting is set.
|
|
|
|
|
"""
|
|
|
|
|
if not settings.DBTEMPLATES_ADD_DEFAULT_SITE:
|
|
|
|
|
return
|
|
|
|
|
current_site = Site.objects.get_current()
|
|
|
|
|
if current_site not in instance.sites.all():
|
|
|
|
|
instance.sites.add(current_site)
|
|
|
|
|
|
|
|
|
|
|
2009-10-19 00:30:52 +00:00
|
|
|
signals.post_save.connect(add_default_site, sender=Template)
|
2011-04-11 17:18:55 +00:00
|
|
|
signals.post_save.connect(add_template_to_cache, sender=Template)
|
|
|
|
|
signals.pre_delete.connect(remove_cached_template, sender=Template)
|