From 8e1ddfabc4731ab7edd9c88d9d0a7b3f3e1817df Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Mon, 20 Jul 2015 16:42:15 +0800 Subject: [PATCH] Add update workbook support Partially implements blueprint mistral-dashboard-crud-operations Change-Id: I84b44a5d33718ddef6ac64dcd4fe59f2d1ee914c --- mistraldashboard/api.py | 9 ++++++++ mistraldashboard/workbooks/forms.py | 15 ++++++++++++ mistraldashboard/workbooks/tables.py | 10 +++++++- .../templates/workbooks/_update.html | 6 +++++ .../workbooks/templates/workbooks/update.html | 7 ++++++ mistraldashboard/workbooks/urls.py | 4 ++++ mistraldashboard/workbooks/views.py | 23 +++++++++++++++++++ 7 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 mistraldashboard/workbooks/templates/workbooks/_update.html create mode 100644 mistraldashboard/workbooks/templates/workbooks/update.html diff --git a/mistraldashboard/api.py b/mistraldashboard/api.py index 4fb7ef0..4542185 100644 --- a/mistraldashboard/api.py +++ b/mistraldashboard/api.py @@ -169,3 +169,12 @@ def workbook_delete(request, workbook_definition): """ return mistralclient(request).workbooks.delete(workbook_definition) + + +def workbook_update(request, workbook_definition): + """Update workbook. + + :param workbook_definition: Workbook definition + """ + + return mistralclient(request).workbooks.update(workbook_definition) diff --git a/mistraldashboard/workbooks/forms.py b/mistraldashboard/workbooks/forms.py index ec52665..16bab06 100644 --- a/mistraldashboard/workbooks/forms.py +++ b/mistraldashboard/workbooks/forms.py @@ -109,3 +109,18 @@ class CreateForm(forms.SelfHandlingForm): msg = _('Failed to create workbook.') redirect = reverse('horizon:mistral:workbooks:index') exceptions.handle(request, msg, redirect=redirect) + + +class UpdateForm(CreateForm): + + def handle(self, request, data): + try: + api.workbook_update(request, data['definition']) + msg = _('Successfully updated workbook.') + messages.success(request, msg) + + return True + except Exception: + msg = _('Failed to update workbook.') + redirect = reverse('horizon:mistral:workbooks:index') + exceptions.handle(request, msg, redirect=redirect) diff --git a/mistraldashboard/workbooks/tables.py b/mistraldashboard/workbooks/tables.py index f559a2b..d469d27 100644 --- a/mistraldashboard/workbooks/tables.py +++ b/mistraldashboard/workbooks/tables.py @@ -31,6 +31,14 @@ class CreateWorkbook(tables.LinkAction): icon = "plus" +class UpdateWorkbook(tables.LinkAction): + name = "update" + verbose_name = _("Update Workbook") + url = "horizon:mistral:workbooks:change_definition" + classes = ("ajax-modal",) + icon = "pencil" + + class DeleteWorkbook(tables.DeleteAction): @staticmethod def action_present(count): @@ -86,5 +94,5 @@ class WorkbooksTable(tables.DataTable): class Meta(object): name = "workbooks" verbose_name = _("Workbooks") - table_actions = (CreateWorkbook, DeleteWorkbook) + table_actions = (CreateWorkbook, UpdateWorkbook, DeleteWorkbook) row_actions = (DeleteWorkbook,) diff --git a/mistraldashboard/workbooks/templates/workbooks/_update.html b/mistraldashboard/workbooks/templates/workbooks/_update.html new file mode 100644 index 0000000..44828fc --- /dev/null +++ b/mistraldashboard/workbooks/templates/workbooks/_update.html @@ -0,0 +1,6 @@ +{% extends "horizon/common/_modal_form.html" %} +{% load i18n %} +{% block modal-body-right %} +

{% trans "Description:" %}

+

{% trans "Update workbooks with the provided definition." %}

+{% endblock %} diff --git a/mistraldashboard/workbooks/templates/workbooks/update.html b/mistraldashboard/workbooks/templates/workbooks/update.html new file mode 100644 index 0000000..2c7d06e --- /dev/null +++ b/mistraldashboard/workbooks/templates/workbooks/update.html @@ -0,0 +1,7 @@ +{% extends 'base.html' %} +{% load i18n %} +{% block title %}{% trans "Update Workbook" %}{% endblock %} + +{% block main %} + {% include 'mistral/workbooks/_update.html' %} +{% endblock %} diff --git a/mistraldashboard/workbooks/urls.py b/mistraldashboard/workbooks/urls.py index 53148dd..a5ce4ec 100644 --- a/mistraldashboard/workbooks/urls.py +++ b/mistraldashboard/workbooks/urls.py @@ -27,6 +27,10 @@ urlpatterns = patterns( url(r'^select_definition$', views.SelectDefinitionView.as_view(), name='select_definition'), + url(r'^change_definition$', + views.ChangeDefinitionView.as_view(), + name='change_definition'), url(r'^create$', views.CreateView.as_view(), name='create'), + url(r'^update$', views.UpdateView.as_view(), name='update'), url(WORKBOOKS % 'detail', views.DetailView.as_view(), name='detail'), ) diff --git a/mistraldashboard/workbooks/views.py b/mistraldashboard/workbooks/views.py index aa68a8e..b24d82f 100644 --- a/mistraldashboard/workbooks/views.py +++ b/mistraldashboard/workbooks/views.py @@ -76,6 +76,19 @@ class SelectDefinitionView(forms.ModalFormView): return kwargs +class ChangeDefinitionView(SelectDefinitionView): + modal_header = _("Update Definition") + submit_url = reverse_lazy("horizon:mistral:workbooks:change_definition") + success_url = reverse_lazy('horizon:mistral:workbooks:update') + page_title = _("Update Definition") + + def get_form_kwargs(self): + kwargs = super(ChangeDefinitionView, self).get_form_kwargs() + kwargs['next_view'] = UpdateView + + return kwargs + + class CreateView(forms.ModalFormView): template_name = 'mistral/workbooks/create.html' modal_header = _("Create Workbook") @@ -93,3 +106,13 @@ class CreateView(forms.ModalFormView): initial['definition'] = self.kwargs['definition'] return initial + + +class UpdateView(CreateView): + template_name = 'mistral/workbooks/update.html' + modal_header = _("Update Workbook") + form_id = "update_workbook" + form_class = mistral_forms.UpdateForm + submit_label = _("Update") + submit_url = reverse_lazy("horizon:mistral:workbooks:update") + page_title = _("Update Workbook")