Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Notify user when updating topology from admin fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Dec 29, 2015
1 parent 365509c commit 7fa86db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
20 changes: 16 additions & 4 deletions django_netjsongraph/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _

from .models import Topology, Node, Link
Expand All @@ -24,19 +25,30 @@ def get_actions(self, request):
actions['delete_selected'] = delete
return actions

def _message(self, request, rows, suffix):
def _message(self, request, rows, suffix, level=messages.SUCCESS):
if rows == 1:
prefix = _('1 {0} was'.format(self.model._meta.verbose_name))
else: # pragma: nocover
prefix = _('{0} {1} were'.format(rows, self.model._meta.verbose_name_plural))
self.message_user(request, '{0} {1}'.format(prefix, suffix))
self.message_user(request, '{0} {1}'.format(prefix, suffix), level=level)

def update_selected(self, request, queryset):
items = list(queryset)
failed = []
for item in items:
with log_on_fail('update topology admin action', item):
try:
item.update()
self._message(request, len(items), _('successfully updated'))
except Exception as e:
failed.append('{0}: {1}'.format(item.label, str(e)))
with log_on_fail('update topology admin action', item):
raise e
failures = len(failed)
successes = len(items) - failures
if successes > 0:
self._message(request, successes, _('successfully updated'))
if failures > 0:
message = _('not updated. %s') % '; '.join(failed)
self._message(request, failures, message, level=messages.ERROR)
update_selected.short_description = _('Update selected topologies')

def publish_selected(self, request, queryset):
Expand Down
21 changes: 21 additions & 0 deletions django_netjsongraph/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,24 @@ def test_update_selected(self):
})
self.assertEqual(Node.objects.count(), 2)
self.assertEqual(Link.objects.count(), 1)

@responses.activate
def test_update_selected_failed(self):
t = Topology.objects.first()
t.parser = 'netdiff.NetJsonParser'
t.save()
responses.add(responses.GET,
'http://127.0.0.1:9090',
body='{"error": "not found"}',
status=404,
content_type='application/json')
Node.objects.all().delete()
response = self.client.post(self.changelist_path, {
'action': 'update_selected',
'_selected_action': str(t.pk)
}, follow=True)
self.assertEqual(Node.objects.count(), 0)
self.assertEqual(Link.objects.count(), 0)
message = list(response.context['messages'])[0]
self.assertEqual(message.tags, 'error')
self.assertIn('not updated', message.message)

0 comments on commit 7fa86db

Please sign in to comment.