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-23 15:38:46 +00:00
|
|
|
from django.shortcuts import render, redirect
|
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")
|
2014-10-23 15:38:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clone(modeladmin, request, queryset):
|
|
|
|
service = queryset.get()
|
2014-10-27 13:29:02 +00:00
|
|
|
fields = modeladmin.get_fields(request)
|
2014-10-23 15:38:46 +00:00
|
|
|
query = []
|
|
|
|
for field in fields:
|
2015-03-26 16:00:30 +00:00
|
|
|
model_field = type(service)._meta.get_field_by_name(field)[0]
|
|
|
|
if model_field.rel:
|
2014-10-27 13:29:02 +00:00
|
|
|
value = getattr(service, field + '_id')
|
2015-03-26 16:00:30 +00:00
|
|
|
elif 'Boolean' in model_field.__class__.__name__:
|
|
|
|
value = 'True' if getattr(service, field) else ''
|
2014-10-27 13:29:02 +00:00
|
|
|
else:
|
|
|
|
value = getattr(service, field)
|
2014-10-23 15:38:46 +00:00
|
|
|
query.append('%s=%s' % (field, value))
|
|
|
|
opts = service._meta
|
|
|
|
url = reverse('admin:%s_%s_add' % (opts.app_label, opts.model_name))
|
|
|
|
url += '?%s' % '&'.join(query)
|
|
|
|
return redirect(url)
|