diff options
Diffstat (limited to 'sublab_project')
-rw-r--r-- | sublab_project/news/admin.py | 5 | ||||
-rw-r--r-- | sublab_project/preview_admin/__init__.py | 1 | ||||
-rw-r--r-- | sublab_project/preview_admin/admin.py | 47 |
3 files changed, 52 insertions, 1 deletions
diff --git a/sublab_project/news/admin.py b/sublab_project/news/admin.py index ea422e4..1f89f25 100644 --- a/sublab_project/news/admin.py +++ b/sublab_project/news/admin.py @@ -1,14 +1,17 @@ from django.contrib import admin from sorl.thumbnail.admin import AdminImageMixin +from preview_admin import PreviewAdmin from news.models import News +from utils import render_creole -class NewsAdmin(AdminImageMixin, admin.ModelAdmin): +class NewsAdmin(AdminImageMixin, PreviewAdmin): prepopulated_fields = {'slug': ('title',)} list_display = ('title', 'date_created', 'date_updated') list_filter = ('author',) search_fields = ['title', 'content'] + preview_fields = { 'content': render_creole } date_hierarchy = 'date_updated' def save_model(self, request, obj, form, change): diff --git a/sublab_project/preview_admin/__init__.py b/sublab_project/preview_admin/__init__.py new file mode 100644 index 0000000..3d18e8d --- /dev/null +++ b/sublab_project/preview_admin/__init__.py @@ -0,0 +1 @@ +from preview_admin.admin import PreviewAdmin diff --git a/sublab_project/preview_admin/admin.py b/sublab_project/preview_admin/admin.py new file mode 100644 index 0000000..6f6f0e2 --- /dev/null +++ b/sublab_project/preview_admin/admin.py @@ -0,0 +1,47 @@ +from django.contrib.admin import ModelAdmin +from django.conf.urls.defaults import patterns, url +from django.core.urlresolvers import reverse, NoReverseMatch +from django.shortcuts import render_to_response +from django.utils.safestring import mark_safe + +import logging +logger = logging.getLogger('preview_admin') + +class PreviewAdmin(ModelAdmin): + preview_fields = {} + def get_urls(self): + info = self.model._meta.app_label, self.model._meta.module_name + + urls = super(PreviewAdmin, self).get_urls() + myurls = patterns('', + url(r'^preview/(.+)/$', self.admin_site.admin_view(self.preview), + name='%s_%s_preview' % info), + ) + return myurls + urls + + def add_preview(self, formfield): + render = formfield.widget.render + + def preview_render(name, *args, **kwargs): + rendered = render(name, *args, **kwargs) + + info = self.model._meta.app_label, self.model._meta.module_name + preview_url = reverse('admin:%s_%s_preview' % info, + args=[name], current_app=self.admin_site.name) + + rendered += '<p><a href="%s">Preview</a></p>' % preview_url + return mark_safe(rendered) + + formfield.widget.render = preview_render + + def formfield_for_dbfield(self, db_field, **kwargs): + formfield = super(PreviewAdmin, self).formfield_for_dbfield(db_field, **kwargs) + + if db_field.name in self.preview_fields: + self.add_preview(formfield) + + return formfield + + def preview(self, request, field): + return render_to_response('404.html') + |