From 5131a0b3cde54fb6c5b77f55e9f4eb881ba16ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20A=2E=20S=C3=A1nchez=20L=C3=B3pez?= Date: Thu, 26 Aug 2010 13:43:40 +0000 Subject: [PATCH] Allow to send reminders from the django admin interface. Fixes #377 - Legacy-Id: 2510 --- ietf/liaisons/admin.py | 72 +++++++++++++++++++ .../commands/remind_update_sdo_list.py | 17 +++-- .../admin/liaisons/sdos/change_form.html | 12 ++++ .../admin/liaisons/sdos/change_list.html | 16 +++++ .../admin/liaisons/sdos/send_reminder.html | 47 ++++++++++++ 5 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 ietf/templates/admin/liaisons/sdos/change_form.html create mode 100644 ietf/templates/admin/liaisons/sdos/change_list.html create mode 100644 ietf/templates/admin/liaisons/sdos/send_reminder.html diff --git a/ietf/liaisons/admin.py b/ietf/liaisons/admin.py index befb3a98a..c670a8ea4 100644 --- a/ietf/liaisons/admin.py +++ b/ietf/liaisons/admin.py @@ -1,5 +1,16 @@ #coding: utf-8 +from django import template from django.contrib import admin +from django.contrib.admin.util import unquote +from django.core.exceptions import PermissionDenied +from django.core.management import load_command_class +from django.http import Http404 +from django.shortcuts import render_to_response +from django.utils.encoding import force_unicode +from django.utils.functional import update_wrapper +from django.utils.html import escape +from django.utils.translation import ugettext as _ + from ietf.liaisons.models import (FromBodies, LiaisonDetail, LiaisonPurpose, SDOs, LiaisonManagers, SDOAuthorizedIndividual) @@ -37,6 +48,67 @@ class SDOAuthorizedIndividualAdmin(admin.ModelAdmin): class SDOsAdmin(admin.ModelAdmin): inlines = [LiaisonManagersInline, SDOAuthorizedIndividualInline] + def get_urls(self): + from django.conf.urls.defaults 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.model._meta.app_label, self.model._meta.module_name + + urls = patterns('', + url(r'^reminder/$', + wrap(self.send_reminder), + name='%s_%s_reminder' % info), + url(r'^(.+)/reminder/$', + wrap(self.send_one_reminder), + name='%s_%s_one_reminder' % info), + ) + urls += super(SDOsAdmin, self).get_urls() + return urls + + def send_reminder(self, request, sdo=None): + opts = self.model._meta + app_label = opts.app_label + + output = None + sdo_pk = sdo and sdo.pk or None + if request.method == 'POST' and request.POST.get('send', False): + command = load_command_class('ietf.liaisons', 'remind_update_sdo_list') + output=command.handle(return_output=True, sdo_pk=sdo_pk) + output='\n'.join(output) + + context = { + 'opts': opts, + 'has_change_permission': self.has_change_permission(request), + 'app_label': app_label, + 'output': output, + 'sdo': sdo, + } + return render_to_response('admin/liaisons/sdos/send_reminder.html', + context, + context_instance = template.RequestContext(request, current_app=self.admin_site.name), + ) + + def send_one_reminder(self, request, object_id): + model = self.model + opts = model._meta + + try: + obj = self.queryset(request).get(pk=unquote(object_id)) + except model.DoesNotExist: + obj = None + + if not self.has_change_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)}) + + return self.send_reminder(request, sdo=obj) + class RelatedAdmin(admin.ModelAdmin): pass diff --git a/ietf/liaisons/management/commands/remind_update_sdo_list.py b/ietf/liaisons/management/commands/remind_update_sdo_list.py index 8bc669b04..f0f429464 100644 --- a/ietf/liaisons/management/commands/remind_update_sdo_list.py +++ b/ietf/liaisons/management/commands/remind_update_sdo_list.py @@ -32,21 +32,26 @@ class Command(BaseCommand): body = body) if not settings.DEBUG: mail.send() - print '%05s#: %s Mail Sent!' % (sdo.pk, sdo.sdo_name) + msg = '%05s#: %s Mail Sent!' % (sdo.pk, sdo.sdo_name) else: - print '%05s#: %s Mail Not Sent because in DEBUG mode!' % (sdo.pk, sdo.sdo_name) - return + msg = '%05s#: %s Mail Not Sent because in DEBUG mode!' % (sdo.pk, sdo.sdo_name) + return msg def handle(self, *args, **options): query = SDOs.objects.all().order_by('pk') sdo_pk = options.get('sdo_pk', None) + return_output = options.get('return_output', False) if sdo_pk: query = query.filter(pk=sdo_pk) + msg_list = [] for sdo in query: manager = sdo.liaisonmanager() if manager: - self.send_mail_to(manager.person, sdo) + msg = self.send_mail_to(manager.person, sdo) else: - print '%05s#: %s has no liaison manager' % (sdo.pk, sdo.sdo_name) - + msg = '%05s#: %s has no liaison manager' % (sdo.pk, sdo.sdo_name) + print msg + msg_list.append(msg) + if return_output: + return msg_list diff --git a/ietf/templates/admin/liaisons/sdos/change_form.html b/ietf/templates/admin/liaisons/sdos/change_form.html new file mode 100644 index 000000000..62dc3d52c --- /dev/null +++ b/ietf/templates/admin/liaisons/sdos/change_form.html @@ -0,0 +1,12 @@ +{% extends "admin/change_form.html" %} +{% load i18n %} + +{% block object-tools %} +{% if change %}{% if not is_popup %} + +{% endif %}{% endif %} +{% endblock %} diff --git a/ietf/templates/admin/liaisons/sdos/change_list.html b/ietf/templates/admin/liaisons/sdos/change_list.html new file mode 100644 index 000000000..253752899 --- /dev/null +++ b/ietf/templates/admin/liaisons/sdos/change_list.html @@ -0,0 +1,16 @@ +{% extends "admin/change_list.html" %} +{% load i18n %} + + +{% block object-tools %} + {% if has_add_permission %} + + {% endif %} +{% endblock %} diff --git a/ietf/templates/admin/liaisons/sdos/send_reminder.html b/ietf/templates/admin/liaisons/sdos/send_reminder.html new file mode 100644 index 000000000..8ddf7406e --- /dev/null +++ b/ietf/templates/admin/liaisons/sdos/send_reminder.html @@ -0,0 +1,47 @@ +{% extends "admin/base_site.html" %} + +{% load i18n admin_modify adminmedia %} + +{% block extrastyle %}{{ block.super }}{% endblock %} + +{% block bodyclass %}change-form{% endblock %} + +{% block breadcrumbs %}{% if not is_popup %} + +{% endif %}{% endblock %} + +{% block content %}
+
+

Send a reminder to each SDO Liaison Manager

+{% if output %} +

+Reminder sent successfully. See the output of the command: +

+
+{{ output }}
+
+{% else %} +{% if sdo %} +

+You can send a reminder to the {{ sdo }} SDO Liaison Manager to request an updated list of persons authorized to send liaison statements on behalf of {{ sdo }} +

+{% else %} +

+You can send a reminder to each SDO Liaison Manager to request an updated list of persons authorized to send liaison statements on behalf of his SDO +

+

+By clicking the 'Send' button you will send a request to all the SDO Liaison Managers. In order to send the request to one SDO Liaison Manager go to the SDO edit page. +

+{% endif %} +
+ +
+{% endif %} +
+
+{% endblock %}