show custom fields in contact admin
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 8 Nov 2012 11:31:44 +0000 (12:31 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 8 Nov 2012 11:31:44 +0000 (12:31 +0100)
contact/admin.py
contact/forms.py
prawokultury/contact_forms.py

index 8bd6341..5ddf831 100644 (file)
@@ -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 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
 
 
 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):
 class ContactAdmin(admin.ModelAdmin):
+    __metaclass__ = ContactAdminMeta
     date_hierarchy = 'created_at'
     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']
 
     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:
     def change_view(self, request, object_id, extra_context=None):
         if object_id:
             try:
index 82eba4b..d2df1f7 100644 (file)
@@ -10,10 +10,14 @@ from .models import Attachment, Contact
 
 
 contact_forms = {}
 
 
 contact_forms = {}
+admin_list_width = 0
 class ContactFormMeta(forms.Form.__metaclass__):
     def __new__(cls, *args, **kwargs):
 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.'
         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
 
         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')
     form_tag = None
     form_title = _('Contact form')
     submit_label = _('Submit')
+    admin_list = None
 
     required_css_class = 'required'
     contact = forms.CharField(max_length=128)
 
     required_css_class = 'required'
     contact = forms.CharField(max_length=128)
index b3019cf..e57983d 100644 (file)
@@ -9,6 +9,7 @@ from django.utils.translation import ugettext_lazy as _
 class RegistrationForm(ContactForm):
     form_tag = 'register'
     form_title = _('Take part!')
 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)
 
     name = forms.CharField(label=_('Name'), max_length=128)
     contact = forms.EmailField(label=_('E-mail'), max_length=128)