2010-06-24 13:56:41 +00:00
|
|
|
import posixpath
|
2008-11-02 00:05:26 +00:00
|
|
|
from django import forms
|
2008-07-19 10:48:07 +00:00
|
|
|
from django.contrib import admin
|
2009-10-09 12:01:57 +00:00
|
|
|
from django.utils.translation import ungettext, ugettext_lazy as _
|
2010-06-24 13:56:41 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2008-07-19 10:48:07 +00:00
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
from dbtemplates.conf import settings
|
2011-04-11 17:18:55 +00:00
|
|
|
from dbtemplates.models import (Template,
|
|
|
|
|
remove_cached_template, add_template_to_cache)
|
2011-08-02 19:11:28 +00:00
|
|
|
from dbtemplates.utils.template import check_template_syntax
|
2008-07-19 10:48:07 +00:00
|
|
|
|
2008-11-02 00:05:26 +00:00
|
|
|
# Check if django-reversion is installed and use reversions' VersionAdmin
|
|
|
|
|
# as the base admin class if yes
|
2011-07-01 13:44:09 +00:00
|
|
|
if settings.DBTEMPLATES_USE_REVERSION:
|
2008-11-02 00:05:26 +00:00
|
|
|
from reversion.admin import VersionAdmin as TemplateModelAdmin
|
2009-01-21 10:29:26 +00:00
|
|
|
else:
|
2008-11-02 00:05:26 +00:00
|
|
|
from django.contrib.admin import ModelAdmin as TemplateModelAdmin
|
|
|
|
|
|
2011-04-11 21:13:39 +00:00
|
|
|
|
2010-06-24 13:56:41 +00:00
|
|
|
class CodeMirrorTextArea(forms.Textarea):
|
|
|
|
|
"""
|
2011-04-11 21:13:39 +00:00
|
|
|
A custom widget for the CodeMirror browser editor to be used with the
|
2010-06-24 13:56:41 +00:00
|
|
|
content field of the Template model.
|
|
|
|
|
"""
|
|
|
|
|
class Media:
|
2011-07-01 13:50:04 +00:00
|
|
|
css = dict(screen=[posixpath.join(
|
|
|
|
|
settings.DBTEMPLATES_MEDIA_PREFIX, 'css/editor.css')])
|
2011-07-01 13:44:09 +00:00
|
|
|
js = [posixpath.join(settings.DBTEMPLATES_MEDIA_PREFIX, 'js/codemirror.js')]
|
2010-06-24 13:56:41 +00:00
|
|
|
|
|
|
|
|
def render(self, name, value, attrs=None):
|
|
|
|
|
result = []
|
|
|
|
|
result.append(
|
|
|
|
|
super(CodeMirrorTextArea, self).render(name, value, attrs))
|
2010-07-07 10:38:54 +00:00
|
|
|
result.append(u"""
|
2010-06-24 13:56:41 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
|
var editor = CodeMirror.fromTextArea('id_%(name)s', {
|
|
|
|
|
path: "%(media_prefix)sjs/",
|
|
|
|
|
parserfile: "parsedjango.js",
|
|
|
|
|
stylesheet: "%(media_prefix)scss/django.css",
|
|
|
|
|
continuousScanning: 500,
|
|
|
|
|
height: "40.2em",
|
|
|
|
|
tabMode: "shift",
|
|
|
|
|
indentUnit: 4,
|
|
|
|
|
lineNumbers: true
|
|
|
|
|
});
|
|
|
|
|
</script>
|
2011-07-01 13:44:09 +00:00
|
|
|
""" % dict(media_prefix=settings.DBTEMPLATES_MEDIA_PREFIX, name=name))
|
2010-06-24 13:56:41 +00:00
|
|
|
return mark_safe(u"".join(result))
|
|
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
if settings.DBTEMPLATES_USE_CODEMIRROR:
|
2010-07-07 10:38:54 +00:00
|
|
|
TemplateContentTextArea = CodeMirrorTextArea
|
|
|
|
|
else:
|
|
|
|
|
TemplateContentTextArea = forms.Textarea
|
|
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
if settings.DBTEMPLATES_AUTO_POPULATE_CONTENT:
|
2010-09-21 12:25:14 +00:00
|
|
|
content_help_text = _("Leaving this empty causes Django to look for a "
|
2011-04-11 21:13:39 +00:00
|
|
|
"template with the given name and populate this field with its "
|
|
|
|
|
"content.")
|
2010-09-21 12:25:14 +00:00
|
|
|
else:
|
|
|
|
|
content_help_text = ""
|
2010-06-24 13:56:41 +00:00
|
|
|
|
2011-04-11 21:13:39 +00:00
|
|
|
|
2008-11-02 00:05:26 +00:00
|
|
|
class TemplateAdminForm(forms.ModelForm):
|
|
|
|
|
"""
|
|
|
|
|
Custom AdminForm to make the content textarea wider.
|
|
|
|
|
"""
|
|
|
|
|
content = forms.CharField(
|
2010-07-07 10:38:54 +00:00
|
|
|
widget=TemplateContentTextArea({'rows': '24'}),
|
2010-09-21 12:25:14 +00:00
|
|
|
help_text=content_help_text, required=False)
|
2008-11-02 00:05:26 +00:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Template
|
|
|
|
|
|
2010-06-24 13:56:41 +00:00
|
|
|
|
2008-11-02 00:05:26 +00:00
|
|
|
class TemplateAdmin(TemplateModelAdmin):
|
|
|
|
|
form = TemplateAdminForm
|
2008-07-19 10:48:07 +00:00
|
|
|
fieldsets = (
|
2008-11-02 00:05:26 +00:00
|
|
|
(None, {
|
2010-06-24 14:15:06 +00:00
|
|
|
'fields': ('name', 'content'),
|
2008-11-02 00:05:26 +00:00
|
|
|
'classes': ('monospace',),
|
|
|
|
|
}),
|
2010-06-24 14:15:06 +00:00
|
|
|
(_('Advanced'), {
|
|
|
|
|
'fields': (('sites'),),
|
|
|
|
|
}),
|
2008-11-16 02:02:10 +00:00
|
|
|
(_('Date/time'), {
|
2008-11-02 00:05:26 +00:00
|
|
|
'fields': (('creation_date', 'last_changed'),),
|
|
|
|
|
'classes': ('collapse',),
|
2008-07-19 10:48:07 +00:00
|
|
|
}),
|
|
|
|
|
)
|
2010-12-14 00:46:33 +00:00
|
|
|
filter_horizontal = ('sites',)
|
2009-01-03 11:55:22 +00:00
|
|
|
list_display = ('name', 'creation_date', 'last_changed', 'site_list')
|
2008-11-02 00:05:26 +00:00
|
|
|
list_filter = ('sites',)
|
2010-12-14 01:05:05 +00:00
|
|
|
save_as = True
|
2008-07-19 10:48:07 +00:00
|
|
|
search_fields = ('name', 'content')
|
2011-08-15 11:05:26 +00:00
|
|
|
actions = ['invalidate_cache', 'repopulate_cache', 'check_syntax']
|
2009-10-09 12:01:57 +00:00
|
|
|
|
|
|
|
|
def invalidate_cache(self, request, queryset):
|
|
|
|
|
for template in queryset:
|
|
|
|
|
remove_cached_template(template)
|
2011-08-02 19:11:28 +00:00
|
|
|
count = queryset.count()
|
2009-10-09 12:01:57 +00:00
|
|
|
message = ungettext(
|
|
|
|
|
"Cache of one template successfully invalidated.",
|
|
|
|
|
"Cache of %(count)d templates successfully invalidated.",
|
2011-08-02 19:11:28 +00:00
|
|
|
count)
|
|
|
|
|
self.message_user(request, message % {'count': count})
|
2011-04-11 21:13:39 +00:00
|
|
|
invalidate_cache.short_description = _("Invalidate cache of "
|
|
|
|
|
"selected templates")
|
2009-10-09 12:01:57 +00:00
|
|
|
|
|
|
|
|
def repopulate_cache(self, request, queryset):
|
|
|
|
|
for template in queryset:
|
|
|
|
|
add_template_to_cache(template)
|
2011-08-02 19:11:28 +00:00
|
|
|
count = queryset.count()
|
2009-10-09 12:01:57 +00:00
|
|
|
message = ungettext(
|
|
|
|
|
"Cache successfully repopulated with one template.",
|
|
|
|
|
"Cache successfully repopulated with %(count)d templates.",
|
2011-08-02 19:11:28 +00:00
|
|
|
count)
|
|
|
|
|
self.message_user(request, message % {'count': count})
|
2011-04-11 21:13:39 +00:00
|
|
|
repopulate_cache.short_description = _("Repopulate cache with "
|
|
|
|
|
"selected templates")
|
2008-07-19 10:48:07 +00:00
|
|
|
|
2011-08-15 11:05:26 +00:00
|
|
|
def check_syntax(self, request, queryset):
|
2011-08-02 19:11:28 +00:00
|
|
|
errors = []
|
|
|
|
|
for template in queryset:
|
|
|
|
|
valid, error = check_template_syntax(template)
|
|
|
|
|
if not valid:
|
|
|
|
|
errors.append('%s: %s' % (template.name, error))
|
|
|
|
|
if errors:
|
|
|
|
|
count = len(errors)
|
|
|
|
|
message = ungettext(
|
|
|
|
|
"Template syntax check FAILED for %(names)s.",
|
|
|
|
|
"Template syntax check FAILED for %(count)d templates: %(names)s.",
|
|
|
|
|
count)
|
|
|
|
|
self.message_user(request, message %
|
|
|
|
|
{'count': count, 'names': ', '.join(errors)})
|
|
|
|
|
else:
|
|
|
|
|
count = queryset.count()
|
|
|
|
|
message = ungettext(
|
|
|
|
|
"Template syntax OK.",
|
|
|
|
|
"Template syntax OK for %(count)d templates.", count)
|
|
|
|
|
self.message_user(request, message % {'count': count})
|
2011-08-15 11:05:26 +00:00
|
|
|
check_syntax.short_description = _("Check template syntax")
|
2011-08-02 19:11:28 +00:00
|
|
|
|
2009-01-03 11:55:22 +00:00
|
|
|
def site_list(self, template):
|
2011-04-11 21:13:39 +00:00
|
|
|
return ", ".join([site.name for site in template.sites.all()])
|
2009-01-03 11:55:22 +00:00
|
|
|
site_list.short_description = _('sites')
|
|
|
|
|
|
2008-07-19 10:48:07 +00:00
|
|
|
admin.site.register(Template, TemplateAdmin)
|