Merge "Add create workbook support"

This commit is contained in:
Jenkins 2015-07-16 16:21:49 +00:00 committed by Gerrit Code Review
commit e833d49e87
9 changed files with 208 additions and 0 deletions

View File

@ -124,3 +124,21 @@ def workbook_get(request, workbook_name):
""" """
return mistralclient(request).workbooks.get(workbook_name) return mistralclient(request).workbooks.get(workbook_name)
def workbook_create(request, workbook_definition):
"""Create workbook.
:param workbook_definition: Workbook definition
"""
return mistralclient(request).workbooks.create(workbook_definition)
def workbook_validate(request, workbook_definition):
"""Validate workbook.
:param workbook_definition: Workbook definition
"""
return mistralclient(request).workbooks.validate(workbook_definition)

View File

@ -0,0 +1,111 @@
# 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 DefinitionForm(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 __init__(self, *args, **kwargs):
self.next_view = kwargs.pop('next_view')
super(DefinitionForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(DefinitionForm, 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.'))
try:
validated = api.workbook_validate(
self.request,
cleaned_data['definition']
)
except Exception as e:
raise forms.ValidationError(unicode(e))
if not validated.get('valid'):
raise forms.ValidationError(
validated.get('error', _('Validated failed')))
return cleaned_data
def handle(self, request, data):
kwargs = {'definition': data['definition']}
request.method = 'GET'
return self.next_view.as_view()(request, **kwargs)
class CreateForm(forms.SelfHandlingForm):
definition = forms.CharField(
widget=forms.widgets.Textarea(
attrs={'readonly': 'readonly',
'rows': 12}
),
required=False
)
def handle(self, request, data):
try:
api.workbook_create(request, data['definition'])
msg = _('Successfully created workbook.')
messages.success(request, msg)
return True
except Exception:
msg = _('Failed to create workbook.')
redirect = reverse('horizon:mistral:workbooks:index')
exceptions.handle(request, msg, redirect=redirect)

View File

@ -20,6 +20,14 @@ from horizon import tables
from horizon.utils import filters from horizon.utils import filters
class CreateWorkbook(tables.LinkAction):
name = "create"
verbose_name = _("Create Workbook")
url = "horizon:mistral:workbooks:select_definition"
classes = ("ajax-modal",)
icon = "plus"
def tags_to_string(workbook): def tags_to_string(workbook):
return ', '.join(workbook.tags) if workbook.tags else None return ', '.join(workbook.tags) if workbook.tags else None
@ -54,3 +62,4 @@ class WorkbooksTable(tables.DataTable):
class Meta(object): class Meta(object):
name = "workbooks" name = "workbooks"
verbose_name = _("Workbooks") verbose_name = _("Workbooks")
table_actions = (CreateWorkbook,)

View File

@ -0,0 +1,6 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% block modal-body-right %}
<h3>{% trans "Description:" %}</h3>
<p>{% trans "Create a new workbook with the provided definition." %}</p>
{% endblock %}

View 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 "Use one of the available definition source options to specify the definition to be used in creating this workbook." %}</p>
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Create Workbook" %}{% endblock %}
{% block main %}
{% include 'mistral/workbooks/_create.html' %}
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Select Definition" %}{% endblock %}
{% block main %}
{% include 'mistral/workbooks/_select_definition.html' %}
{% endblock %}

View File

@ -24,5 +24,9 @@ WORKBOOKS = r'^(?P<workbook_name>[^/]+)/%s$'
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^$', views.IndexView.as_view(), name='index'), url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^select_definition$',
views.SelectDefinitionView.as_view(),
name='select_definition'),
url(r'^create$', views.CreateView.as_view(), name='create'),
url(WORKBOOKS % 'detail', views.DetailView.as_view(), name='detail'), url(WORKBOOKS % 'detail', views.DetailView.as_view(), name='detail'),
) )

View File

@ -15,13 +15,16 @@
# limitations under the License. # limitations under the License.
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views import generic from django.views import generic
from horizon import exceptions from horizon import exceptions
from horizon import forms
from horizon import tables from horizon import tables
from mistraldashboard import api from mistraldashboard import api
from mistraldashboard.workbooks import forms as mistral_forms
from mistraldashboard.workbooks.tables import WorkbooksTable from mistraldashboard.workbooks.tables import WorkbooksTable
@ -54,3 +57,39 @@ class DetailView(generic.TemplateView):
exceptions.handle(self.request, msg, redirect=redirect) exceptions.handle(self.request, msg, redirect=redirect)
return workbook return workbook
class SelectDefinitionView(forms.ModalFormView):
template_name = 'mistral/workbooks/select_definition.html'
modal_header = _("Select Definition")
form_id = "select_definition"
form_class = mistral_forms.DefinitionForm
submit_label = _("Next")
submit_url = reverse_lazy("horizon:mistral:workbooks:select_definition")
success_url = reverse_lazy('horizon:mistral:workbooks:create')
page_title = _("Select Definition")
def get_form_kwargs(self):
kwargs = super(SelectDefinitionView, self).get_form_kwargs()
kwargs['next_view'] = CreateView
return kwargs
class CreateView(forms.ModalFormView):
template_name = 'mistral/workbooks/create.html'
modal_header = _("Create Workbook")
form_id = "create_workbook"
form_class = mistral_forms.CreateForm
submit_label = _("Create")
submit_url = reverse_lazy("horizon:mistral:workbooks:create")
success_url = reverse_lazy('horizon:mistral:workbooks:index')
page_title = _("Create Workbook")
def get_initial(self):
initial = {}
if 'definition' in self.kwargs:
initial['definition'] = self.kwargs['definition']
return initial