From: Radek Czajka Date: Thu, 8 Nov 2012 11:31:44 +0000 (+0100) Subject: show custom fields in contact admin X-Git-Url: https://git.mdrn.pl/prawokultury.git/commitdiff_plain/2cd938606837981b7a3914458ae0071828ef37ca?ds=sidebyside show custom fields in contact admin --- diff --git a/contact/admin.py b/contact/admin.py index 8bd6341..5ddf831 100644 --- a/contact/admin.py +++ b/contact/admin.py @@ -2,17 +2,40 @@ from django.contrib import admin from django.forms import ModelForm from .models import Contact from django.utils.translation import ugettext as _ -from .forms import contact_forms +from .forms import contact_forms, admin_list_width from django.template import Template from django.utils.safestring import mark_safe +class ContactAdminMeta(admin.ModelAdmin.__metaclass__): + def __getattr__(cls, name): + if name.startswith('admin_list_'): + return lambda self: "" + raise AttributeError, name + + class ContactAdmin(admin.ModelAdmin): + __metaclass__ = ContactAdminMeta date_hierarchy = 'created_at' - list_display = ('created_at', 'contact', 'form_tag') + list_display = ['created_at', 'contact', 'form_tag'] + \ + ["admin_list_%d" % i for i in range(admin_list_width)] fields = ['form_tag', 'created_at', 'contact', 'ip'] readonly_fields = ['form_tag', 'created_at', 'contact', 'ip'] + def admin_list(self, obj, nr): + try: + field_name = contact_forms[obj.form_tag].admin_list[nr] + except BaseException, e: + return '' + else: + return obj.body.get(field_name, 'y') + + def __getattr__(self, name): + if name.startswith('admin_list_'): + nr = int(name[len('admin_list_'):]) + return lambda obj: self.admin_list(obj, nr) + raise AttributeError, name + def change_view(self, request, object_id, extra_context=None): if object_id: try: diff --git a/contact/forms.py b/contact/forms.py index 82eba4b..d2df1f7 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -10,10 +10,14 @@ from .models import Attachment, Contact contact_forms = {} +admin_list_width = 0 class ContactFormMeta(forms.Form.__metaclass__): def __new__(cls, *args, **kwargs): + global admin_list_width model = super(ContactFormMeta, cls).__new__(cls, *args, **kwargs) assert model.form_tag not in contact_forms, 'Duplicate form_tag.' + if model.admin_list: + admin_list_width = max(admin_list_width, len(model.admin_list)) contact_forms[model.form_tag] = model return model @@ -25,6 +29,7 @@ class ContactForm(forms.Form): form_tag = None form_title = _('Contact form') submit_label = _('Submit') + admin_list = None required_css_class = 'required' contact = forms.CharField(max_length=128) diff --git a/prawokultury/contact_forms.py b/prawokultury/contact_forms.py index b3019cf..e57983d 100644 --- a/prawokultury/contact_forms.py +++ b/prawokultury/contact_forms.py @@ -9,6 +9,7 @@ from django.utils.translation import ugettext_lazy as _ class RegistrationForm(ContactForm): form_tag = 'register' form_title = _('Take part!') + admin_list = ['name', 'organization', 'title'] name = forms.CharField(label=_('Name'), max_length=128) contact = forms.EmailField(label=_('E-mail'), max_length=128)