From 386f8342a645e9f6c8aabbe5a78776761680f297 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 11 Aug 2015 22:53:21 +0800 Subject: [PATCH] Add update action support Partially implements blueprint mistral-dashboard-crud-operations Change-Id: I11241c2c5b8dd3e0f1115cf8bd7df160b7437f82 --- mistraldashboard/actions/forms.py | 58 +++++++++++++++++++ mistraldashboard/actions/tables.py | 10 +++- .../actions/templates/actions/_update.html | 7 +++ .../actions/templates/actions/update.html | 7 +++ mistraldashboard/actions/urls.py | 1 + mistraldashboard/actions/views.py | 11 ++++ mistraldashboard/api.py | 9 +++ 7 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 mistraldashboard/actions/templates/actions/_update.html create mode 100644 mistraldashboard/actions/templates/actions/update.html diff --git a/mistraldashboard/actions/forms.py b/mistraldashboard/actions/forms.py index e75833e..410394a 100644 --- a/mistraldashboard/actions/forms.py +++ b/mistraldashboard/actions/forms.py @@ -78,3 +78,61 @@ class CreateForm(forms.SelfHandlingForm): msg = _('Failed to create action.') redirect = reverse('horizon:mistral:actions:index') exceptions.handle(request, msg, redirect=redirect) + + +class UpdateForm(forms.SelfHandlingForm): + definition_source = forms.ChoiceField( + label=_('Definition Source'), + choices=[('file', _('File')), + ('raw', _('Direct Input'))], + widget=forms.Select( + attrs={'class': 'switchable', + 'data-slug': 'definitionsource'}) + ) + definition_upload = forms.FileField( + label=_('Definition File'), + help_text=_('A local definition to upload.'), + widget=forms.FileInput( + attrs={'class': 'switched', + 'data-switch-on': 'definitionsource', + 'data-definitionsource-file': _('Definition File')} + ), + required=False + ) + definition_data = forms.CharField( + label=_('Definition Data'), + help_text=_('The raw contents of the definition.'), + widget=forms.widgets.Textarea( + attrs={'class': 'switched', + 'data-switch-on': 'definitionsource', + 'data-definitionsource-raw': _('Definition Data'), + 'rows': 4} + ), + required=False + ) + + def clean(self): + cleaned_data = super(UpdateForm, self).clean() + + if cleaned_data.get('definition_upload'): + files = self.request.FILES + cleaned_data['definition'] = files['definition_upload'].read() + elif cleaned_data.get('definition_data'): + cleaned_data['definition'] = cleaned_data['definition_data'] + else: + raise forms.ValidationError( + _('You must specify the definition source.')) + + return cleaned_data + + def handle(self, request, data): + try: + api.action_update(request, data['definition']) + msg = _('Successfully updated action.') + messages.success(request, msg) + + return True + except Exception: + msg = _('Failed to update action.') + redirect = reverse('horizon:mistral:actions:index') + exceptions.handle(request, msg, redirect=redirect) diff --git a/mistraldashboard/actions/tables.py b/mistraldashboard/actions/tables.py index 1726dbb..738e009 100644 --- a/mistraldashboard/actions/tables.py +++ b/mistraldashboard/actions/tables.py @@ -26,6 +26,14 @@ class CreateAction(tables.LinkAction): icon = "plus" +class UpdateAction(tables.LinkAction): + name = "update" + verbose_name = _("Update Action") + url = "horizon:mistral:actions:update" + classes = ("ajax-modal",) + icon = "pencil" + + def tags_to_string(action): return ', '.join(action.tags) if action.tags else None @@ -72,4 +80,4 @@ class ActionsTable(tables.DataTable): class Meta(object): name = "actions" verbose_name = _("Actions") - table_actions = (CreateAction,) + table_actions = (CreateAction, UpdateAction) diff --git a/mistraldashboard/actions/templates/actions/_update.html b/mistraldashboard/actions/templates/actions/_update.html new file mode 100644 index 0000000..fc4a7d4 --- /dev/null +++ b/mistraldashboard/actions/templates/actions/_update.html @@ -0,0 +1,7 @@ +{% extends "horizon/common/_modal_form.html" %} +{% load i18n %} +{% block form_attrs %}enctype="multipart/form-data"{% endblock %} +{% block modal-body-right %} +

{% trans "Description:" %}

+

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

+{% endblock %} diff --git a/mistraldashboard/actions/templates/actions/update.html b/mistraldashboard/actions/templates/actions/update.html new file mode 100644 index 0000000..b64e27d --- /dev/null +++ b/mistraldashboard/actions/templates/actions/update.html @@ -0,0 +1,7 @@ +{% extends 'base.html' %} +{% load i18n %} +{% block title %}{% trans "Update Action" %}{% endblock %} + +{% block main %} + {% include 'mistral/actions/_update.html' %} +{% endblock %} diff --git a/mistraldashboard/actions/urls.py b/mistraldashboard/actions/urls.py index 951dbd7..f869796 100644 --- a/mistraldashboard/actions/urls.py +++ b/mistraldashboard/actions/urls.py @@ -24,4 +24,5 @@ urlpatterns = patterns( url(r'^$', views.IndexView.as_view(), name='index'), url(ACTIONS % 'detail', views.DetailView.as_view(), name='detail'), url(r'^create$', views.CreateView.as_view(), name='create'), + url(r'^update$', views.UpdateView.as_view(), name='update'), ) diff --git a/mistraldashboard/actions/views.py b/mistraldashboard/actions/views.py index 2ce64d0..edd3822 100644 --- a/mistraldashboard/actions/views.py +++ b/mistraldashboard/actions/views.py @@ -37,6 +37,17 @@ class CreateView(forms.ModalFormView): page_title = _("Create Action") +class UpdateView(forms.ModalFormView): + template_name = 'mistral/actions/update.html' + modal_header = _("Update Action") + form_id = "update_action" + form_class = mistral_forms.UpdateForm + submit_label = _("Update") + submit_url = reverse_lazy("horizon:mistral:actions:update") + success_url = reverse_lazy('horizon:mistral:actions:index') + page_title = _("Update Action") + + class IndexView(tables.DataTableView): table_class = ActionsTable template_name = 'mistral/actions/index.html' diff --git a/mistraldashboard/api.py b/mistraldashboard/api.py index 6c83186..c317020 100644 --- a/mistraldashboard/api.py +++ b/mistraldashboard/api.py @@ -212,3 +212,12 @@ def action_create(request, action_definition): """ return mistralclient(request).actions.create(action_definition) + + +def action_update(request, action_definition): + """Update action. + + :param action_definition: Action definition + """ + + return mistralclient(request).actions.update(action_definition)