from django.contrib.admin import helpers from django.core.urlresolvers import reverse from django.db import transaction from django.shortcuts import render from django.template.response import TemplateResponse from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from orchestra.admin.utils import get_object_from_url @transaction.atomic def update_orders(modeladmin, request, queryset, extra_context=None): if not queryset: return if request.POST.get('post') == 'confirmation': num = 0 services = [] for service in queryset: updates = service.update_orders() num += len(updates) services.append(str(service.pk)) modeladmin.log_change(request, service, _("Orders updated")) if num == 1: url = reverse('admin:orders_order_change', args=(updates[0][0].pk,)) msg = _('One related order has benn updated') % url else: url = reverse('admin:orders_order_changelist') url += '?service__in=%s' % ','.join(services) msg = _('%s related orders have been updated') % (url, num) modeladmin.message_user(request, mark_safe(msg)) return updates = [] for service in queryset: updates += service.update_orders(commit=False) opts = modeladmin.model._meta context = { 'title': _("Update orders will cause the following."), 'action_name': 'Update orders', 'action_value': 'update_orders', 'updates': updates, 'queryset': queryset, 'opts': opts, 'app_label': opts.app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, 'obj': get_object_from_url(modeladmin, request), } return render(request, 'admin/services/service/update_orders.html', context) update_orders.url_name = 'update-orders' update_orders.verbose_name = _("Update orders") def view_help(modeladmin, request, queryset): opts = modeladmin.model._meta context = { 'title': _("Need some help?"), 'opts': opts, 'queryset': queryset, 'obj': queryset.get(), 'action_name': _("help"), 'app_label': opts.app_label, } return TemplateResponse(request, 'admin/services/service/help.html', context) view_help.url_name = 'help' view_help.verbose_name = _("Help")