# 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 # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import re from django import forms as django_forms from django.template.loader import get_template from django.utils import html from django.utils.translation import ugettext_lazy as _ # noqa import six from horizon import exceptions from horizon import forms from horizon import messages from monitoring.alarms import constants from monitoring import api class ExpressionWidget(forms.Widget): def __init__(self, initial, attrs): super(ExpressionWidget, self).__init__(attrs) self.initial = initial def render(self, name, value, attrs): final_attrs = self.build_attrs(attrs, {'name': name}) if value: dim = value else: if 'all' in self.initial['service']: dim = '' else: dim = next(("%s=%s" % (k, v) for k, v in self.initial.items()), '') t = get_template(constants.TEMPLATE_PREFIX + 'expression_field.html') local_attrs = {'service': dim} local_attrs.update(final_attrs) return t.render(local_attrs) class SimpleExpressionWidget(django_forms.MultiWidget): def __init__(self, initial, attrs=None): comparators = [('>', '>'), ('>=', '>='), ('<', '<'), ('<=', '<=')] func = [('min', _('min')), ('max', _('max')), ('sum', _('sum')), ('count', _('count')), ('avg', _('avg'))] _widgets = ( django_forms.widgets.Select(attrs=attrs, choices=func), ExpressionWidget(initial, attrs={}), django_forms.widgets.Select(attrs=attrs, choices=comparators), django_forms.widgets.TextInput(), ) super(SimpleExpressionWidget, self).__init__(_widgets, attrs) def decompress(self, expr): if expr: return re.search('^(\w+)\((.*)\) ([<>=]*) (.*)$', expr).groups() else: return [None, None, None, None] def format_output(self, rendered_widgets): return ''.join(rendered_widgets) def value_from_datadict(self, data, files, name): values = [ widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] try: expression = '%s(%s) %s %s' % (values[0], values[1], values[2], values[3]) except ValueError: return '' else: return expression class NotificationField(forms.MultiValueField): def __init__(self, *args, **kwargs): super(NotificationField, self).__init__(fields=(), *args, **kwargs) def _get_choices(self): return self._choices def _set_choices(self, value): # Setting choices also sets the choices on the widget. # choices can be any iterable, but we call list() on it because # it will be consumed more than once. self._choices = self.widget.choices = list(value) choices = property(_get_choices, _set_choices) def compress(self, data_list): return data_list def clean(self, value): return value class NotificationCreateWidget(forms.Select): def __init__(self, *args, **kwargs): super(NotificationCreateWidget, self).__init__(*args, **kwargs) def render(self, name, value, attrs=None, choices=()): output = '' output += '' % \ six.text_type(_("Name")) if value: idx = 1 for notification in value: output += '' output += '' idx += 1 else: output += '
%s
' output += ('' output += 'X
' output += '