From 57ddc80d05924b3c1bade125ff43cba45302f8de Mon Sep 17 00:00:00 2001 From: Artur Basiak Date: Tue, 25 Jul 2017 09:52:48 +0200 Subject: [PATCH] Fix monasca-ui forms Fix problem with unexpected keyword argument 'name' for build_attrs() from Django. 'name' was passed to the method as a kwargs. **kwargs in build_attrs() were removed. Pass 'name' in a dict. In Django 1.10+ passing Context to render is deprecated and gives an error, pass instead a dict. Remove one assertContains from alarm definition create. Tags in where generated in random order every time the gates run: Change-Id: I2d39a10f08ac60c163f80cca3256a2bb24b4e0a2 Story: 2001133 Task: 4844 --- monitoring/alarmdefs/forms.py | 43 ++++++++++++++++------------------- monitoring/alarmdefs/tests.py | 7 +++--- monitoring/alarms/forms.py | 8 +++---- requirements.txt | 2 +- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/monitoring/alarmdefs/forms.py b/monitoring/alarmdefs/forms.py index 659035bd..9114c8e5 100644 --- a/monitoring/alarmdefs/forms.py +++ b/monitoring/alarmdefs/forms.py @@ -1,4 +1,5 @@ # Copyright 2013 Hewlett-Packard Development Company, L.P. +# Copyright 2017 FUJITSU LIMITED # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -16,7 +17,6 @@ import json from itertools import chain from django.template.loader import get_template -from django.template import Context from django.utils.translation import ugettext as _ # noqa from horizon import exceptions @@ -34,11 +34,7 @@ def _get_metrics(request): def _get_notifications(request): notifications = api.monitor.notification_list(request) - return [(notification['id'], - notification['name'], - notification['type'], - notification['address']) - for notification in notifications] + return [(notification['id'], notification) for notification in notifications] class ExpressionWidget(forms.Widget): @@ -54,7 +50,7 @@ class ExpressionWidget(forms.Widget): self.initial = initial def render(self, name, value, attrs=None): - final_attrs = self.build_attrs(attrs, name=name) + final_attrs = self.build_attrs(attrs, {'name': name}) t = get_template(constants.TEMPLATE_PREFIX + 'expression_field.html') local_attrs = { @@ -65,8 +61,7 @@ class ExpressionWidget(forms.Widget): } local_attrs.update(final_attrs) - - return t.render(Context(local_attrs)) + return t.render(local_attrs) class ExpressionField(forms.CharField): @@ -86,13 +81,12 @@ class MatchByWidget(forms.Widget): self.initial = initial def render(self, name, value, attrs=None): - final_attrs = self.build_attrs(attrs, name=name) + final_attrs = self.build_attrs(attrs, {'name': name}) t = get_template(constants.TEMPLATE_PREFIX + 'match_by_field.html') local_attrs = {'service': ''} local_attrs.update(final_attrs) - context = Context(local_attrs) - return t.render(context) + return t.render(local_attrs) class NotificationField(forms.MultiValueField): @@ -122,7 +116,7 @@ class NotificationCreateWidget(forms.Select): super(NotificationCreateWidget, self).__init__(*args, **kwargs) def render(self, name, value, attrs=None, choices=()): - final_attrs = self.build_attrs(attrs, name=name) + final_attrs = self.build_attrs(attrs, {'name': name}) tpl = get_template(constants.TEMPLATE_PREFIX + 'notification_field.html') selected = {} @@ -131,17 +125,22 @@ class NotificationCreateWidget(forms.Select): 'ok': item['ok'], 'undetermined': item['undetermined']} data = [] - for id, label, type, address in chain(self.choices, choices): - if id in selected: - actions = selected[id] - data.append((id, label, type, address, actions['alarm'], + + for pk, notification in chain(self.choices, choices): + nt_label = notification['name'] + nt_address = notification['address'] + nt_type = notification['type'] + + if pk in selected: + actions = selected[pk] + data.append((pk, nt_label, nt_type, nt_address, actions['alarm'], actions['ok'], actions['undetermined'], True)) else: - data.append((id, label, type, address, True, True, True, False)) + data.append((pk, nt_label, nt_type, nt_address, True, True, True, False)) local_attrs = {'data': json.dumps(data)} local_attrs.update(final_attrs) - return tpl.render(Context(local_attrs)) + return tpl.render(local_attrs) def value_from_datadict(self, data, files, name): return [{"id": _id} for _id in data.getlist(name)] @@ -229,11 +228,7 @@ class EditAlarmForm(forms.SelfHandlingForm): notifications = [] exceptions.handle(request, _('Unable to retrieve notifications: %s') % e) - notification_choices = [(notification['id'], - notification['name'], - notification['type'], - notification['address']) - for notification in notifications] + notification_choices = [(notification['id'], notification) for notification in notifications] self.fields['notifications'].choices = notification_choices diff --git a/monitoring/alarmdefs/tests.py b/monitoring/alarmdefs/tests.py index 40bccfff..b0f60820 100644 --- a/monitoring/alarmdefs/tests.py +++ b/monitoring/alarmdefs/tests.py @@ -1,5 +1,5 @@ # Copyright 2013 Hewlett-Packard Development Company, L.P. -# Copyright 2016 FUJITSU LIMITED +# Copyright 2016-2017 FUJITSU LIMITED # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -76,9 +76,8 @@ class AlarmDefinitionsTest(helpers.TestCase): step = workflow.get_step('setalarmnotificationsaction') self.assertIsNotNone(step) - self.assertContains(res, '') self.assertContains(res, '