django-orchestra-test/orchestra/contrib/resources/actions.py

48 lines
2 KiB
Python
Raw Normal View History

from django.urls import reverse
2015-07-16 13:07:15 +00:00
from django.shortcuts import redirect, render
from django.utils.safestring import mark_safe
from django.utils.translation import ngettext, gettext_lazy as _
2014-10-27 13:29:02 +00:00
def run_monitor(modeladmin, request, queryset):
""" Resource and ResourceData run monitors """
referer = request.META.get('HTTP_REFERER')
run_async = modeladmin.model.monitor.__defaults__[0]
2014-11-27 19:17:26 +00:00
logs = set()
2014-10-27 13:29:02 +00:00
for resource in queryset:
rlogs = resource.monitor()
if not run_async:
logs = logs.union(set([str(log.pk) for log in rlogs]))
2014-10-27 13:29:02 +00:00
modeladmin.log_change(request, resource, _("Run monitors"))
if run_async:
2014-11-27 19:17:26 +00:00
num = len(queryset)
# TODO listfilter by uuid: task.request.id + ?task_id__in=ids
link = reverse('admin:djcelery_taskstate_changelist')
msg = ngettext(
_("One selected resource has been <a href='%s'>scheduled for monitoring</a>.") % link,
_("%s selected resource have been <a href='%s'>scheduled for monitoring</a>.") % (num, link),
num)
else:
2014-11-27 19:17:26 +00:00
num = len(logs)
if num == 1:
log_pk = int(logs.pop())
link = reverse('admin:orchestration_backendlog_change', args=(log_pk,))
2014-11-27 19:17:26 +00:00
msg = _("One related monitor has <a href='%s'>been executed</a>.") % link
elif num >= 1:
link = reverse('admin:orchestration_backendlog_changelist')
link += '?id__in=%s' % ','.join(logs)
2014-11-27 19:17:26 +00:00
msg = _("%s related monitors have <a href='%s'>been executed</a>.") % (num, link)
else:
msg = _("No related monitors have been executed.")
modeladmin.message_user(request, mark_safe(msg))
2014-10-27 13:29:02 +00:00
if referer:
return redirect(referer)
run_monitor.url_name = 'monitor'
2015-07-16 13:07:15 +00:00
2015-07-28 10:49:20 +00:00
def show_history(modeladmin, request, queryset):
2015-07-16 13:07:15 +00:00
context = {
2015-07-27 12:55:35 +00:00
'ids': ','.join(map(str, queryset.values_list('id', flat=True))),
2015-07-16 13:07:15 +00:00
}
return render(request, 'admin/resources/resourcedata/history.html', context)