2014-10-07 13:50:59 +00:00
|
|
|
from django.contrib import messages
|
2014-11-21 15:39:41 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2014-10-07 13:50:59 +00:00
|
|
|
from django.db import transaction
|
2014-11-21 17:18:59 +00:00
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
from django.utils import timezone
|
2014-10-07 13:50:59 +00:00
|
|
|
from django.utils.translation import ungettext, ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.admin.decorators import action_with_confirmation
|
2014-11-21 17:18:59 +00:00
|
|
|
from orchestra.core import services
|
2014-10-07 13:50:59 +00:00
|
|
|
|
2014-11-27 19:17:26 +00:00
|
|
|
from . import settings
|
|
|
|
|
2014-10-07 13:50:59 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
@action_with_confirmation()
|
|
|
|
def disable(modeladmin, request, queryset):
|
|
|
|
num = 0
|
|
|
|
for account in queryset:
|
|
|
|
account.disable()
|
|
|
|
modeladmin.log_change(request, account, _("Disabled"))
|
|
|
|
num += 1
|
|
|
|
msg = ungettext(
|
|
|
|
_("Selected account and related services has been disabled."),
|
|
|
|
_("%s selected accounts and related services have been disabled.") % num,
|
|
|
|
num)
|
|
|
|
modeladmin.message_user(request, msg)
|
|
|
|
disable.url_name = 'disable'
|
|
|
|
disable.verbose_name = _("Disable")
|
2014-11-21 15:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def list_contacts(modeladmin, request, queryset):
|
|
|
|
ids = queryset.values_list('id', flat=True)
|
|
|
|
if not ids:
|
|
|
|
message.warning(request, "Select at least one account.")
|
|
|
|
return
|
|
|
|
url = reverse('admin:contacts_contact_changelist')
|
|
|
|
url += '?account__in=%s' % ','.join(map(str, ids))
|
|
|
|
return redirect(url)
|
|
|
|
list_contacts.verbose_name = _("List contacts")
|
2014-11-21 17:18:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def service_report(modeladmin, request, queryset):
|
2014-11-27 19:17:26 +00:00
|
|
|
# TODO resources
|
2014-11-21 17:18:59 +00:00
|
|
|
accounts = []
|
2014-11-24 14:39:41 +00:00
|
|
|
fields = []
|
|
|
|
# First we get related manager names to fire a prefetch related
|
|
|
|
for name, field in queryset.model._meta._name_map.iteritems():
|
|
|
|
model = field[0].model
|
|
|
|
if model in services.get() and model != queryset.model:
|
|
|
|
fields.append((model, name))
|
|
|
|
sorted(fields, key=lambda i: i[0]._meta.verbose_name_plural.lower())
|
|
|
|
fields = [field for model, field in fields]
|
|
|
|
|
|
|
|
for account in queryset.prefetch_related(*fields):
|
2014-11-21 17:18:59 +00:00
|
|
|
items = []
|
2014-11-24 14:39:41 +00:00
|
|
|
for field in fields:
|
|
|
|
related_manager = getattr(account, field)
|
|
|
|
items.append((related_manager.model._meta, related_manager.all()))
|
2014-11-21 17:18:59 +00:00
|
|
|
accounts.append((account, items))
|
2014-11-24 14:39:41 +00:00
|
|
|
|
2014-11-21 17:18:59 +00:00
|
|
|
context = {
|
|
|
|
'accounts': accounts,
|
|
|
|
'date': timezone.now().today()
|
|
|
|
}
|
2014-11-27 19:17:26 +00:00
|
|
|
return render(request, settings.ACCOUNTS_SERVICE_REPORT_TEMPLATE, context)
|