diff options
Diffstat (limited to 'sublab_project/preview_admin')
-rw-r--r-- | sublab_project/preview_admin/__init__.py | 1 | ||||
-rw-r--r-- | sublab_project/preview_admin/admin.py | 47 |
2 files changed, 48 insertions, 0 deletions
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') + |