From 3cb953c1b06a6f865e96998f27cd24ee5da3d0c3 Mon Sep 17 00:00:00 2001 From: cindy oneill Date: Wed, 11 Jun 2014 13:17:07 -0600 Subject: [PATCH] Added Notification update --- monitoring/api/monitor.py | 3 +- monitoring/notifications/forms.py | 25 +++++++++++ monitoring/notifications/tables.py | 4 +- .../templates/notifications/_edit.html | 24 +++++++++++ .../templates/notifications/edit.html | 11 +++++ monitoring/notifications/urls.py | 6 +-- monitoring/notifications/views.py | 43 ++++++++++++++++++- 7 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 monitoring/notifications/templates/notifications/_edit.html create mode 100644 monitoring/notifications/templates/notifications/edit.html diff --git a/monitoring/api/monitor.py b/monitoring/api/monitor.py index a57e8648..445e037f 100644 --- a/monitoring/api/monitor.py +++ b/monitoring/api/monitor.py @@ -106,7 +106,8 @@ def notification_create(request, **kwargs): def notification_update(request, notification_id, **kwargs): - return monclient(request).notifications.update(notification_id, **kwargs) + return monclient(request).notifications. \ + update(notification_id=notification_id, **kwargs) def metrics_list(request, marker=None, paginate=False): diff --git a/monitoring/notifications/forms.py b/monitoring/notifications/forms.py index 59f63607..02dce512 100644 --- a/monitoring/notifications/forms.py +++ b/monitoring/notifications/forms.py @@ -101,3 +101,28 @@ class DetailMethodForm(BaseNotificationMethodForm): def handle(self, request, data): return True + + +class EditMethodForm(BaseNotificationMethodForm): + def __init__(self, request, *args, **kwargs): + super(EditMethodForm, self).__init__(request, *args, **kwargs) + super(EditMethodForm, self)._init_fields(readOnly=False) + + def handle(self, request, data): + try: + kwargs = {} + kwargs['notification_id'] = self.initial['id'] + kwargs['name'] = data['name'] + kwargs['type'] = data['type'] + kwargs['address'] = data['address'] + api.monitor.notification_update( + request, + **kwargs + ) + messages.success(request, + _('Notification has been edited successfully.')) + except Exception as e: + exceptions.handle(request, + _('Unable to edit the notification: %s') % e) + return False + return True diff --git a/monitoring/notifications/tables.py b/monitoring/notifications/tables.py index 54101008..fa35ca07 100644 --- a/monitoring/notifications/tables.py +++ b/monitoring/notifications/tables.py @@ -67,7 +67,7 @@ class EditNotification(tables.LinkAction): def get_link_url(self, datum): return reverse(constants.URL_PREFIX + 'notification_edit', - datum['id'], ) + args=(datum['id'], )) def allowed(self, request, datum=None): return True @@ -87,5 +87,5 @@ class NotificationsTable(tables.DataTable): class Meta: name = "notifications" verbose_name = _("Notifications") - row_actions = (DeleteNotification, ) + row_actions = (EditNotification, DeleteNotification, ) table_actions = (CreateNotification, ) diff --git a/monitoring/notifications/templates/notifications/_edit.html b/monitoring/notifications/templates/notifications/_edit.html new file mode 100644 index 00000000..1898c1d4 --- /dev/null +++ b/monitoring/notifications/templates/notifications/_edit.html @@ -0,0 +1,24 @@ +{% extends "horizon/common/_modal_form.html" %} +{% load i18n %} +{% load url from future %} + +{% block form_id %}edit_method_form{% endblock %} +{% block form_action %}{{ action_url }}{% endblock %} + +{% block modal-header %}{% trans "Edit Alarm" %}{% endblock %} + +{% block modal-body %} +

{% trans "Description" %}:

+

{% blocktrans %} + The Name field is used to identify the notification method. + {% endblocktrans %}

+
+ {% include "horizon/common/_form_fields.html" %} +
+ +{% endblock %} + +{% block modal-footer %} + + {% trans "Cancel" %} +{% endblock %} diff --git a/monitoring/notifications/templates/notifications/edit.html b/monitoring/notifications/templates/notifications/edit.html new file mode 100644 index 00000000..a14e4343 --- /dev/null +++ b/monitoring/notifications/templates/notifications/edit.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} +{% load i18n %} +{% block title %}{% trans 'Create Notification' %}{% endblock %} + +{% block page_header %} + {% include "horizon/common/_page_header.html" with title="Create Notification" %} +{% endblock page_header %} + +{% block main %} + {% include 'overcloud/notifications/_edit.html' %} +{% endblock %} diff --git a/monitoring/notifications/urls.py b/monitoring/notifications/urls.py index 910adc19..a4b41aca 100644 --- a/monitoring/notifications/urls.py +++ b/monitoring/notifications/urls.py @@ -24,7 +24,7 @@ urlpatterns = patterns( url(r'^notification_create$', views.NotificationCreateView.as_view(), name='notification_create'), - # url(r'^notification_edit/(?P[^/]+)$', - # views.NotificationEditView.as_view(), - # name='notification_edit'), + url(r'^notification_edit/(?P[^/]+)$', + views.NotificationEditView.as_view(), + name='notification_edit'), ) diff --git a/monitoring/notifications/views.py b/monitoring/notifications/views.py index 546992fe..28ec17ac 100644 --- a/monitoring/notifications/views.py +++ b/monitoring/notifications/views.py @@ -20,10 +20,11 @@ from django.contrib import messages from django.core.urlresolvers import reverse_lazy, reverse # noqa from django.views.generic import TemplateView +from horizon import exceptions from horizon import forms from horizon import tables -from . import forms as alarm_forms +from . import forms as notification_forms from . import constants from monitoring import api @@ -53,7 +54,7 @@ class IndexView(tables.DataTableView): class NotificationCreateView(forms.ModalFormView): - form_class = alarm_forms.CreateMethodForm + form_class = notification_forms.CreateMethodForm template_name = constants.TEMPLATE_PREFIX + 'create.html' success_url = reverse_lazy(constants.URL_PREFIX + 'index') @@ -64,3 +65,41 @@ class NotificationCreateView(forms.ModalFormView): action = constants.URL_PREFIX + 'notification_create' context["action_url"] = reverse(action) return context + + +class NotificationEditView(forms.ModalFormView): + form_class = notification_forms.EditMethodForm + template_name = constants.TEMPLATE_PREFIX + 'edit.html' + + def dispatch(self, *args, **kwargs): + return super(NotificationEditView, self).dispatch(*args, **kwargs) + + def get_object(self): + id = self.kwargs['id'] + try: + if hasattr(self, "_object"): + return self._object + self._object = None + self._object = api.monitor.notification_get(self.request, id) + return self._object + except Exception: + redirect = reverse(constants.URL_PREFIX + 'index') + exceptions.handle(self.request, + _('Unable to retrieve notification details.'), + redirect=redirect) + return None + + def get_initial(self): + self.notification = self.get_object() + return self.notification + + def get_context_data(self, **kwargs): + context = super(NotificationEditView, self).get_context_data(**kwargs) + id = self.kwargs['id'] + context["cancel_url"] = self.get_success_url() + context["action_url"] = reverse(constants.URL_PREFIX + + 'notification_edit', args=(id,)) + return context + + def get_success_url(self): + return reverse_lazy(constants.URL_PREFIX + 'index',)