Add create action support
Partially implements blueprint mistral-dashboard-crud-operations Change-Id: Ibb45f55e72ab37c3942a165b7f9a51522ce75d26
This commit is contained in:
parent
c85c3a6bbe
commit
9f5ea7c56c
80
mistraldashboard/actions/forms.py
Normal file
80
mistraldashboard/actions/forms.py
Normal file
@ -0,0 +1,80 @@
|
||||
# Copyright 2015 Huawei Technologies Co., Ltd.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from horizon import exceptions
|
||||
from horizon import forms
|
||||
from horizon import messages
|
||||
|
||||
from mistraldashboard import api
|
||||
|
||||
|
||||
class CreateForm(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(CreateForm, 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_create(request, data['definition'])
|
||||
msg = _('Successfully created action.')
|
||||
messages.success(request, msg)
|
||||
|
||||
return True
|
||||
except Exception:
|
||||
msg = _('Failed to create action.')
|
||||
redirect = reverse('horizon:mistral:actions:index')
|
||||
exceptions.handle(request, msg, redirect=redirect)
|
@ -18,6 +18,14 @@ from horizon import tables
|
||||
from horizon.utils import filters
|
||||
|
||||
|
||||
class CreateAction(tables.LinkAction):
|
||||
name = "create"
|
||||
verbose_name = _("Create Action")
|
||||
url = "horizon:mistral:actions:create"
|
||||
classes = ("ajax-modal",)
|
||||
icon = "plus"
|
||||
|
||||
|
||||
def tags_to_string(action):
|
||||
return ', '.join(action.tags) if action.tags else None
|
||||
|
||||
@ -64,3 +72,4 @@ class ActionsTable(tables.DataTable):
|
||||
class Meta(object):
|
||||
name = "actions"
|
||||
verbose_name = _("Actions")
|
||||
table_actions = (CreateAction,)
|
||||
|
7
mistraldashboard/actions/templates/actions/_create.html
Normal file
7
mistraldashboard/actions/templates/actions/_create.html
Normal file
@ -0,0 +1,7 @@
|
||||
{% extends "horizon/common/_modal_form.html" %}
|
||||
{% load i18n %}
|
||||
{% block form_attrs %}enctype="multipart/form-data"{% endblock %}
|
||||
{% block modal-body-right %}
|
||||
<h3>{% trans "Description:" %}</h3>
|
||||
<p>{% trans "Create a new action with the provided definition." %}</p>
|
||||
{% endblock %}
|
7
mistraldashboard/actions/templates/actions/create.html
Normal file
7
mistraldashboard/actions/templates/actions/create.html
Normal file
@ -0,0 +1,7 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load i18n %}
|
||||
{% block title %}{% trans "Create Action" %}{% endblock %}
|
||||
|
||||
{% block main %}
|
||||
{% include 'mistral/actions/_create.html' %}
|
||||
{% endblock %}
|
@ -23,4 +23,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'),
|
||||
)
|
||||
|
@ -13,16 +13,30 @@
|
||||
# limitations under the License.
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views import generic
|
||||
|
||||
from horizon import exceptions
|
||||
from horizon import forms
|
||||
from horizon import tables
|
||||
|
||||
from mistraldashboard.actions import forms as mistral_forms
|
||||
from mistraldashboard.actions.tables import ActionsTable
|
||||
from mistraldashboard import api
|
||||
|
||||
|
||||
class CreateView(forms.ModalFormView):
|
||||
template_name = 'mistral/actions/create.html'
|
||||
modal_header = _("Create Action")
|
||||
form_id = "create_action"
|
||||
form_class = mistral_forms.CreateForm
|
||||
submit_label = _("Create")
|
||||
submit_url = reverse_lazy("horizon:mistral:actions:create")
|
||||
success_url = reverse_lazy('horizon:mistral:actions:index')
|
||||
page_title = _("Create Action")
|
||||
|
||||
|
||||
class IndexView(tables.DataTableView):
|
||||
table_class = ActionsTable
|
||||
template_name = 'mistral/actions/index.html'
|
||||
|
@ -203,3 +203,12 @@ def action_get(request, action_name):
|
||||
"""
|
||||
|
||||
return mistralclient(request).actions.get(action_name)
|
||||
|
||||
|
||||
def action_create(request, action_definition):
|
||||
"""Create action.
|
||||
|
||||
:param action_definition: Action definition
|
||||
"""
|
||||
|
||||
return mistralclient(request).actions.create(action_definition)
|
||||
|
Loading…
x
Reference in New Issue
Block a user