diff --git a/dbtemplates/admin.py b/dbtemplates/admin.py index 4f732e0..0979971 100644 --- a/dbtemplates/admin.py +++ b/dbtemplates/admin.py @@ -15,13 +15,15 @@ if settings.USE_REVERSION: else: from django.contrib.admin import ModelAdmin as TemplateModelAdmin + class CodeMirrorTextArea(forms.Textarea): """ - A custom widget for the CodeMirror browser editor to be used with the + A custom widget for the CodeMirror browser editor to be used with the content field of the Template model. """ class Media: - css = dict(screen=[posixpath.join(settings.MEDIA_PREFIX, 'css/editor.css')]) + css = dict(screen=[ + posixpath.join(settings.MEDIA_PREFIX, 'css/editor.css')]) js = [posixpath.join(settings.MEDIA_PREFIX, 'js/codemirror.js')] def render(self, name, value, attrs=None): @@ -51,10 +53,12 @@ else: if settings.AUTO_POPULATE_CONTENT: content_help_text = _("Leaving this empty causes Django to look for a " - "template with the given name and populate this field with its content.") + "template with the given name and populate this field with its " + "content.") else: content_help_text = "" + class TemplateAdminForm(forms.ModelForm): """ Custom AdminForm to make the content textarea wider. @@ -95,7 +99,8 @@ class TemplateAdmin(TemplateModelAdmin): "Cache of %(count)d templates successfully invalidated.", len(queryset)) self.message_user(request, message % {'count': len(queryset)}) - invalidate_cache.short_description = _("Invalidate cache of selected templates") + invalidate_cache.short_description = _("Invalidate cache of " + "selected templates") def repopulate_cache(self, request, queryset): for template in queryset: @@ -105,10 +110,11 @@ class TemplateAdmin(TemplateModelAdmin): "Cache successfully repopulated with %(count)d templates.", len(queryset)) self.message_user(request, message % {'count': len(queryset)}) - repopulate_cache.short_description = _("Repopulate cache with selected templates") + repopulate_cache.short_description = _("Repopulate cache with " + "selected templates") def site_list(self, template): - return ", ".join([site.name for site in template.sites.all()]) + return ", ".join([site.name for site in template.sites.all()]) site_list.short_description = _('sites') admin.site.register(Template, TemplateAdmin) diff --git a/dbtemplates/loader.py b/dbtemplates/loader.py index d316cfe..8912ba4 100644 --- a/dbtemplates/loader.py +++ b/dbtemplates/loader.py @@ -1,3 +1,4 @@ +import warnings from django import VERSION from django.conf import settings from django.contrib.sites.models import Site @@ -6,6 +7,7 @@ from django.template import TemplateDoesNotExist from dbtemplates.models import Template from dbtemplates.utils import cache, get_cache_key + def load_template_source(template_name, template_dirs=None, annoy=True): """ A custom template loader to load templates from the database. @@ -17,12 +19,9 @@ def load_template_source(template_name, template_dirs=None, annoy=True): """ if VERSION[:2] >= (1, 2) and annoy: # For backward compatibility - import warnings warnings.warn( "`dbtemplates.loader.load_template_source` is deprecated; " - "use `dbtemplates.loader.Loader` instead.", - DeprecationWarning - ) + "use `dbtemplates.loader.Loader` instead.", DeprecationWarning) site = Site.objects.get_current() display_name = 'db:%s:%s:%s' % (settings.DATABASE_ENGINE, template_name, site.domain) @@ -56,5 +55,5 @@ if VERSION[:2] >= (1, 2): is_usable = True def load_template_source(self, template_name, template_dirs=None): - return load_template_source(template_name, template_dirs, annoy=False) - + return load_template_source( + template_name, template_dirs, annoy=False) diff --git a/dbtemplates/management/commands/create_error_templates.py b/dbtemplates/management/commands/create_error_templates.py index dd625de..37c6167 100644 --- a/dbtemplates/management/commands/create_error_templates.py +++ b/dbtemplates/management/commands/create_error_templates.py @@ -1,3 +1,4 @@ +import sys from optparse import make_option from django.core.management.base import CommandError, NoArgsCommand @@ -24,12 +25,13 @@ TEMPLATES = { """, } + class Command(NoArgsCommand): - help = "Creates the 404.html and 500.html error templates as database template objects." + help = "Creates the default error templates as database template objects." option_list = NoArgsCommand.option_list + ( make_option("-f", "--force", action="store_true", dest="force", - default=False, help="overwrite existing database templates"), - ) + default=False, help="overwrite existing database templates"),) + def handle_noargs(self, **options): force = options.get('force') try: @@ -47,7 +49,9 @@ class Command(NoArgsCommand): template.save() template.sites.add(site) if verbosity >= 1: - self.stdout.write("Created database template for %s errors.\n" % error_code) + sys.stdout.write("Created database template " + "for %s errors.\n" % error_code) else: if verbosity >= 1: - self.stderr.write("A template for %s errors already exists.\n" % error_code) + sys.stderr.write("A template for %s errors " + "already exists.\n" % error_code) diff --git a/dbtemplates/management/commands/sync_templates.py b/dbtemplates/management/commands/sync_templates.py index cdd1ac3..c4330e4 100644 --- a/dbtemplates/management/commands/sync_templates.py +++ b/dbtemplates/management/commands/sync_templates.py @@ -19,10 +19,12 @@ class Command(NoArgsCommand): make_option("-f", "--force", action="store_true", dest="force", default=False, help="overwrite existing database templates"), make_option("-o", "--overwrite", action="store", dest="overwrite", - default='0', help="'0' - ask always, '1' - overwrite database templates from template files, '2' - overwrite template files from database templates"), + default='0', help="'0' - ask always, '1' - overwrite database " + "templates from template files, '2' - overwrite template " + "files from database templates"), make_option("-a", "--app-first", action="store_true", dest="app_first", - default=False, help="look for templates in applications directories before project templates"), - ) + default=False, help="look for templates in applications " + "directories before project templates")) def handle_noargs(self, **options): extension = options.get('ext') @@ -60,8 +62,8 @@ class Command(NoArgsCommand): except Template.DoesNotExist: if not force: confirm = raw_input( - "\nA '%s' template doesn't exist in the database.\n" - "Create it with '%s'?" + "\nA '%s' template doesn't exist in the " + "database.\nCreate it with '%s'?" " (y/[n]): """ % (name, path)) if force or confirm.lower().startswith('y'): t = Template(name=name, @@ -81,7 +83,8 @@ class Command(NoArgsCommand): path, t.__repr__())) else: confirm = overwrite - if confirm == '' or confirm in (FILES_TO_DATABASE, DATABASE_TO_FILES): + if confirm == '' or confirm in ( + FILES_TO_DATABASE, DATABASE_TO_FILES): if confirm == FILES_TO_DATABASE: t.content = open(path, 'r').read() t.save() diff --git a/dbtemplates/settings.py b/dbtemplates/settings.py index 677260f..5354b89 100644 --- a/dbtemplates/settings.py +++ b/dbtemplates/settings.py @@ -16,7 +16,8 @@ CACHE_BACKEND = cache ADD_DEFAULT_SITE = getattr(settings, 'DBTEMPLATES_ADD_DEFAULT_SITE', True) -AUTO_POPULATE_CONTENT = getattr(settings, 'DBTEMPLATES_AUTO_POPULATE_CONTENT', True) +AUTO_POPULATE_CONTENT = getattr( + settings, 'DBTEMPLATES_AUTO_POPULATE_CONTENT', True) base_url = getattr(settings, "STATIC_URL", None) if base_url is None: diff --git a/dbtemplates/utils.py b/dbtemplates/utils.py index 34e1e48..8531748 100644 --- a/dbtemplates/utils.py +++ b/dbtemplates/utils.py @@ -7,6 +7,7 @@ from django.contrib.sites.models import Site from dbtemplates import settings + def get_cache_backend(): if "://" in settings.CACHE_BACKEND: cache = get_cache(settings.CACHE_BACKEND) @@ -16,6 +17,7 @@ def get_cache_backend(): cache = get_cache_backend() + def add_default_site(instance, **kwargs): """ Called via Django's signals to cache the templates, if the template