from django.shortcuts import render_to_response
from django import template
from django import forms
try:
    from django.utils.functional import update_wrapper
except ImportError:
    from functools import update_wrapper
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.contrib.admin.util import unquote
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
from campaign.models import MailTemplate, Campaign, BlacklistEntry, \
SubscriberList, Newsletter


class CampaignAdmin(admin.ModelAdmin):
    filter_horizontal=('recipients',)
    list_display=('name', 'newsletter', 'sent', 'sent_at', 'online')
    send_template = None

    def has_send_permission(self, request, obj):
        """
        Subclasses may override this and implement more granular permissions.
        TODO: integrate with django's permisson system
        """
        return request.user.is_superuser


    def send_view(self, request, object_id, extra_context=None):
        """
        Allows the admin to send out the mails for this campaign.
        """
        model = self.model
        opts = model._meta

        try:
            obj = model._default_manager.get(pk=unquote(object_id))
        except model.DoesNotExist:
            # Don't raise Http404 just yet, because we haven't checked
            # permissions yet. We don't want an unauthenticated user to be able
            # to determine whether a given object exists.
            obj = None

        if not self.has_send_permission(request, obj):
            raise PermissionDenied

        if obj is None:
            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})

        if request.method == 'POST':
            if not request.POST.get('send', None) == '1':
                raise PermissionDenied

            num_sent = obj.send()
            messages.success(request, _(u'The %(name)s "%(obj)s" was successfully sent. %(num_sent)s messages delivered.' %  {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj), 'num_sent': num_sent,}))
            return HttpResponseRedirect('../')


        def form_media():
            from django.conf import settings
            css = ['css/forms.css',]
            return forms.Media(css={'screen': ['%sadmin/%s' % (settings.STATIC_URL, url) for url in css]})

        media = self.media + form_media()

        context = {
            'title': _('Send %s') % force_unicode(opts.verbose_name),
            'object_id': object_id,
            'object': obj,
            'is_popup': request.REQUEST.has_key('_popup'),
            'media': mark_safe(media),
            'app_label': opts.app_label,
            'opts': opts,
        }
        context.update(extra_context or {})

        return render_to_response(self.send_template or
            ['admin/%s/%s/send_object.html' % (opts.app_label, opts.object_name.lower()),
            'admin/%s/send_object.html' % opts.app_label,
            'admin/send_object.html'], context, context_instance=template.RequestContext(request))


    def get_urls(self):
        from django.conf.urls import patterns, url

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        info = self.admin_site.name, self.model._meta.app_label, self.model._meta.module_name

        super_urlpatterns = super(CampaignAdmin, self).get_urls()
        urlpatterns = patterns('',
            url(r'^(.+)/send/$',
                wrap(self.send_view),
                name='%sadmin_%s_%s_send' % info),
        )
        urlpatterns += super_urlpatterns

        return urlpatterns


admin.site.register(Campaign, CampaignAdmin)
admin.site.register(MailTemplate)
admin.site.register(BlacklistEntry, list_display=('email', 'added'))
admin.site.register(SubscriberList)
admin.site.register(Newsletter)

@@ -234,25 +234,173 @@
 .con
-trib import admin
+f.urls import patterns, url%0Afrom django.conf import settings%0Afrom django.contrib import admin%0Afrom django.contrib.admin.views.decorators import staff_member_required
 %0Afro
@@ -575,16 +575,64 @@
 nDenied%0A
+from django.core.management import call_command%0A
 from dja
@@ -2591,53 +2591,8 @@
 ():%0A
-            from django.conf import settings%0A
     
@@ -3481,60 +3481,8 @@
 f):%0A
-        from django.conf.urls import patterns, url%0A%0A
     
@@ -4065,16 +4065,725 @@
 terns%0A%0A%0A
+class BlacklistEntryAdmin(admin.ModelAdmin):%0A    list_display=('email', 'added')%0A%0A    def fetch_mandrill_rejects(self, request):%0A        call_command('fetch_mandrill_rejects')%0A        return HttpResponseRedirect(request.META%5B%22HTTP_REFERER%22%5D)%0A%0A%0A    def get_urls(self):%0A        info = self.admin_site.name, self.model._meta.app_label, self.model._meta.module_name%0A%0A        super_urlpatterns = super(BlacklistEntryAdmin, self).get_urls()%0A        urlpatterns = patterns('',%0A            url(r'%5Efetch_mandrill_rejects/$',%0A                self.fetch_mandrill_rejects,%0A                name='%25sadmin_%25s_%25s_fetchmandrillrejects' %25 info),%0A        )%0A        urlpatterns += super_urlpatterns%0A%0A        return urlpatterns%0A%0A%0A
 admin.si
@@ -4893,39 +4893,27 @@
 ry, 
-list_display=('email', 'added')
+BlacklistEntryAdmin
 )%0Aad

Update admin.py