PD-5187 PD-5295 Changes for Django 1.8 compatibility

Convert to a Django migrated app
Change loader and template code to work with Django 1.8
This commit is contained in:
Matthew Ghantous 2015-10-23 11:56:46 -04:00
parent b371cc6518
commit 9480726c5e
8 changed files with 141 additions and 67 deletions

View file

@ -11,7 +11,7 @@ before_script:
- flake8 dbtemplates --ignore=E501
script:
- coverage run --branch --source=dbtemplates `which django-admin.py` test dbtemplates
- coverage report --omit="dbtemplates/test*,dbtemplates/migrations*"
- coverage report --omit="dbtemplates/test*,dbtemplates/south_migrations*,,dbtemplates/south_migrations*"
env:
- DJANGO=1.3.7
- DJANGO=1.4.5

View file

@ -7,8 +7,13 @@ from dbtemplates.utils.cache import (cache, get_cache_key,
set_and_return, get_cache_notfound_key)
from django.template.loader import BaseLoader
# from django.template.base import Loader
# django.template.loaders.base.Loader
class Loader(BaseLoader):
from django.template.loaders.base import Loader
class Loader(Loader):
"""
A custom template loader to load templates from the database.

View file

@ -1,55 +1,38 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
import django.db.models.manager
import django.contrib.sites.managers
class Migration(SchemaMigration):
class Migration(migrations.Migration):
def forwards(self, orm):
dependencies = [
('sites', '0001_initial'),
]
# Adding model 'Template'
db.create_table('django_template', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(unique=True, max_length=100)),
('content', self.gf('django.db.models.fields.TextField')(blank=True)),
('creation_date', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
('last_changed', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
))
db.send_create_signal('dbtemplates', ['Template'])
# Adding M2M table for field sites on 'Template'
db.create_table('django_template_sites', (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('template', models.ForeignKey(orm['dbtemplates.template'], null=False)),
('site', models.ForeignKey(orm['sites.site'], null=False))
))
db.create_unique('django_template_sites', ['template_id', 'site_id'])
def backwards(self, orm):
# Deleting model 'Template'
db.delete_table('django_template')
# Removing M2M table for field sites on 'Template'
db.delete_table('django_template_sites')
models = {
'dbtemplates.template': {
'Meta': {'ordering': "('name',)", 'object_name': 'Template', 'db_table': "'django_template'"},
'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_changed': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
'sites': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['sites.Site']", 'symmetrical': 'False'})
},
'sites.site': {
'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
}
}
complete_apps = ['dbtemplates']
operations = [
migrations.CreateModel(
name='Template',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(help_text="Example: 'flatpages/default.html'", max_length=100, verbose_name='name')),
('content', models.TextField(verbose_name='content', blank=True)),
('creation_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='creation date')),
('last_changed', models.DateTimeField(default=django.utils.timezone.now, verbose_name='last changed')),
('sites', models.ManyToManyField(to='sites.Site', verbose_name='sites', blank=True)),
],
options={
'ordering': ('name',),
'db_table': 'django_template',
'verbose_name': 'template',
'verbose_name_plural': 'templates',
},
managers=[
('objects', django.db.models.manager.Manager()),
('on_site', django.contrib.sites.managers.CurrentSiteManager(b'sites')),
],
),
]

View file

@ -27,7 +27,7 @@ class Template(models.Model):
help_text=_("Example: 'flatpages/default.html'"))
content = models.TextField(_('content'), blank=True)
sites = models.ManyToManyField(Site, verbose_name=_(u'sites'),
blank=True, null=True)
blank=True)
creation_date = models.DateTimeField(_('creation date'),
default=now)
last_changed = models.DateTimeField(_('last changed'),

View file

@ -0,0 +1,55 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Template'
db.create_table('django_template', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(unique=True, max_length=100)),
('content', self.gf('django.db.models.fields.TextField')(blank=True)),
('creation_date', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
('last_changed', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
))
db.send_create_signal('dbtemplates', ['Template'])
# Adding M2M table for field sites on 'Template'
db.create_table('django_template_sites', (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('template', models.ForeignKey(orm['dbtemplates.template'], null=False)),
('site', models.ForeignKey(orm['sites.site'], null=False))
))
db.create_unique('django_template_sites', ['template_id', 'site_id'])
def backwards(self, orm):
# Deleting model 'Template'
db.delete_table('django_template')
# Removing M2M table for field sites on 'Template'
db.delete_table('django_template_sites')
models = {
'dbtemplates.template': {
'Meta': {'ordering': "('name',)", 'object_name': 'Template', 'db_table': "'django_template'"},
'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_changed': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
'sites': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['sites.Site']", 'symmetrical': 'False'})
},
'sites.site': {
'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
}
}
complete_apps = ['dbtemplates']

View file

View file

@ -1,22 +1,53 @@
from django import VERSION
import django
from django.template import (Template, TemplateDoesNotExist,
TemplateSyntaxError)
from django.utils.importlib import import_module
def get_loaders():
from django.template.loader import template_source_loaders
if template_source_loaders is None:
try:
from django.template.loader import find_template as finder
except ImportError:
from django.template.loader import find_template_source as finder # noqa
try:
source, name = finder('test')
except TemplateDoesNotExist:
pass
if django.VERSION < (1, 8):
from django.template.loader import template_source_loaders
return template_source_loaders or []
if template_source_loaders is None:
try:
from django.template.loader import (
find_template as finder_func)
except ImportError:
from django.template.loader import (
find_template_source as finder_func) # noqa
try:
# Force django to calculate template_source_loaders from
# TEMPLATE_LOADERS settings, by asking to find a dummy template
source, name = finder_func('test')
except django.template.TemplateDoesNotExist:
pass
# Reload template_source_loaders now that it has been calculated ;
# it should contain the list of valid, instanciated template loaders
# to use.
from django.template.loader import template_source_loaders
else:
from django.template import engines
template_source_loaders = []
for e in engines.all():
template_source_loaders.extend(e.engine.get_template_loaders(e.engine.loaders))
loaders = []
# If template loader is CachedTemplateLoader, return the loaders
# that it wraps around. So if we have
# TEMPLATE_LOADERS = (
# ('django.template.loaders.cached.Loader', (
# 'django.template.loaders.filesystem.Loader',
# 'django.template.loaders.app_directories.Loader',
# )),
# )
# The loaders will return django.template.loaders.filesystem.Loader
# and django.template.loaders.app_directories.Loader
# The cached Loader and similar ones include a 'loaders' attribute
# so we look for that.
for loader in template_source_loaders:
if hasattr(loader, 'loaders'):
loaders.extend(loader.loaders)
else:
loaders.append(loader)
return loaders
def get_template_source(name):
@ -37,7 +68,7 @@ def get_template_source(name):
pass
except TemplateDoesNotExist:
pass
if source is None and VERSION[:2] < (1, 2):
if source is None and django.VERSION[:2] < (1, 2):
# Django supported template source extraction still :/
try:
from django.template.loader import find_template_source