django-orchestra-test/orchestra/apps/services/actions.py

65 lines
2.4 KiB
Python
Raw Normal View History

2014-10-20 19:22:18 +00:00
from django.contrib.admin import helpers
from django.core.urlresolvers import reverse
2014-09-26 10:38:50 +00:00
from django.db import transaction
2014-10-20 19:22:18 +00:00
from django.shortcuts import render
2014-09-26 15:05:20 +00:00
from django.template.response import TemplateResponse
2014-10-20 19:22:18 +00:00
from django.utils.safestring import mark_safe
2014-09-26 10:38:50 +00:00
from django.utils.translation import ugettext_lazy as _
2014-10-20 19:22:18 +00:00
from orchestra.admin.utils import get_object_from_url
2014-09-26 10:38:50 +00:00
@transaction.atomic
2014-10-20 19:22:18 +00:00
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 = _('<a href="%s">One related order</a> has benn updated') % url
else:
url = reverse('admin:orders_order_changelist')
url += '?service__in=%s' % ','.join(services)
msg = _('<a href="%s">%s related orders</a> have been updated') % (url, num)
modeladmin.message_user(request, mark_safe(msg))
return
updates = []
2014-09-26 10:38:50 +00:00
for service in queryset:
2014-10-20 19:22:18 +00:00
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)
2014-09-26 10:38:50 +00:00
update_orders.url_name = 'update-orders'
update_orders.verbose_name = _("Update orders")
2014-09-26 15:05:20 +00:00
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")