340 lines
13 KiB
Python
340 lines
13 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2016 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import re
|
|
|
|
from quixote import get_publisher, get_request, get_response, get_session, redirect
|
|
from quixote.directory import Directory
|
|
from quixote.html import TemplateIO, htmltext
|
|
|
|
from wcs.admin import utils
|
|
from wcs.backoffice.applications import ApplicationsDirectory
|
|
from wcs.backoffice.snapshots import SnapshotsDirectory
|
|
from wcs.qommon import _, errors, misc, template
|
|
from wcs.qommon.form import CheckboxWidget, FileWidget, Form, HtmlWidget, SlugWidget, StringWidget, TextWidget
|
|
from wcs.utils import grep_strings
|
|
from wcs.wscalls import NamedWsCall, WsCallRequestWidget
|
|
|
|
|
|
class NamedWsCallUI:
|
|
def __init__(self, wscall):
|
|
self.wscall = wscall
|
|
if self.wscall is None:
|
|
self.wscall = NamedWsCall()
|
|
|
|
def get_form(self):
|
|
form = Form(enctype='multipart/form-data', use_tabs=True)
|
|
form.add(StringWidget, 'name', title=_('Name'), required=True, size=30, value=self.wscall.name)
|
|
form.add(
|
|
TextWidget, 'description', title=_('Description'), cols=40, rows=5, value=self.wscall.description
|
|
)
|
|
if self.wscall.slug:
|
|
form.add(
|
|
SlugWidget,
|
|
'slug',
|
|
value=self.wscall.slug,
|
|
hint=_('Beware it is risky to change it'),
|
|
advanced=True,
|
|
)
|
|
form.add(WsCallRequestWidget, 'request', value=self.wscall.request, title=_('Request'), required=True)
|
|
|
|
form.widgets.append(
|
|
HtmlWidget(
|
|
'<div class="infonotice"><p>%s</p></div>'
|
|
% _(
|
|
'This tab is about connection, payload, and HTTP errors. '
|
|
'Application errors ("err" property in response different than zero) '
|
|
'are always silent.'
|
|
),
|
|
tab=('error', _('Error Handling')),
|
|
)
|
|
)
|
|
form.add(
|
|
CheckboxWidget,
|
|
'notify_on_errors',
|
|
title=_('Notify on errors'),
|
|
value=self.wscall.notify_on_errors,
|
|
tab=('error', _('Error Handling')),
|
|
)
|
|
form.add(
|
|
CheckboxWidget,
|
|
'record_on_errors',
|
|
title=_('Record on errors'),
|
|
value=self.wscall.record_on_errors if self.wscall.slug else True,
|
|
default_value=True,
|
|
tab=('error', _('Error Handling')),
|
|
)
|
|
if not self.wscall.is_readonly():
|
|
form.add_submit('submit', _('Submit'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|
return form
|
|
|
|
def submit_form(self, form):
|
|
name = form.get_widget('name').parse()
|
|
if self.wscall.slug:
|
|
slug = form.get_widget('slug').parse()
|
|
else:
|
|
slug = None
|
|
|
|
for wscall in NamedWsCall.select():
|
|
if wscall.id == self.wscall.id:
|
|
continue
|
|
if name == wscall.name:
|
|
form.get_widget('name').set_error(_('This name is already used.'))
|
|
if slug == wscall.slug:
|
|
form.get_widget('slug').set_error(_('This value is already used.'))
|
|
if form.has_errors():
|
|
raise ValueError()
|
|
|
|
self.wscall.name = name
|
|
self.wscall.description = form.get_widget('description').parse()
|
|
self.wscall.notify_on_errors = form.get_widget('notify_on_errors').parse()
|
|
self.wscall.record_on_errors = form.get_widget('record_on_errors').parse()
|
|
self.wscall.request = form.get_widget('request').parse()
|
|
if self.wscall.slug:
|
|
self.wscall.slug = slug
|
|
self.wscall.store()
|
|
|
|
|
|
class NamedWsCallPage(Directory):
|
|
do_not_call_in_templates = True
|
|
_q_exports = [
|
|
'',
|
|
'edit',
|
|
'delete',
|
|
'export',
|
|
('history', 'snapshots_dir'),
|
|
'usage',
|
|
]
|
|
|
|
def __init__(self, component, instance=None):
|
|
try:
|
|
self.wscall = instance or NamedWsCall.get(component)
|
|
except KeyError:
|
|
raise errors.TraversalError()
|
|
self.wscall_ui = NamedWsCallUI(self.wscall)
|
|
get_response().breadcrumb.append((component + '/', self.wscall.name))
|
|
self.snapshots_dir = SnapshotsDirectory(self.wscall)
|
|
|
|
def get_sidebar(self):
|
|
r = TemplateIO(html=True)
|
|
if self.wscall.is_readonly():
|
|
r += htmltext('<div class="infonotice"><p>%s</p></div>') % _('This webservice call is readonly.')
|
|
r += utils.snapshot_info_block(snapshot=self.wscall.snapshot_object)
|
|
r += htmltext('<ul id="sidebar-actions">')
|
|
if not self.wscall.is_readonly():
|
|
r += htmltext('<li><a href="export">%s</a></li>') % _('Export')
|
|
r += htmltext('<li><a href="delete" rel="popup">%s</a></li>') % _('Delete')
|
|
if get_publisher().snapshot_class:
|
|
r += htmltext('<li><a rel="popup" href="history/save">%s</a></li>') % _('Save snapshot')
|
|
r += htmltext('<li><a href="history/">%s</a></li>') % _('History')
|
|
r += htmltext('</ul>')
|
|
if self.wscall.applications:
|
|
r += htmltext('<h3>%s</h3>') % _('Applications')
|
|
for application in self.wscall.applications:
|
|
r += (
|
|
htmltext('<a class="button button-paragraph" href="../application/%s/">')
|
|
% application.slug
|
|
)
|
|
if application.icon:
|
|
r += (
|
|
htmltext(
|
|
'<img src="../application/%s/icon" alt="" class="application-icon" width="16" /> '
|
|
)
|
|
% application.slug
|
|
)
|
|
r += htmltext('%s</a>') % application.name
|
|
return r.getvalue()
|
|
|
|
def _q_index(self):
|
|
from wcs.applications import Application
|
|
|
|
get_response().set_title(self.wscall.name)
|
|
get_response().filter['sidebar'] = self.get_sidebar()
|
|
if not self.wscall.is_readonly():
|
|
Application.load_for_object(self.wscall)
|
|
return template.QommonTemplateResponse(
|
|
templates=['wcs/backoffice/wscall.html'], context={'view': self, 'wscall': self.wscall}
|
|
)
|
|
|
|
def usage(self):
|
|
get_request().disable_error_notifications = True
|
|
get_request().ignore_session = True
|
|
get_response().raw = True
|
|
|
|
usage = {}
|
|
|
|
def accumulate(source_url, value, source_name):
|
|
usage[source_url] = source_name
|
|
|
|
grep_strings(re.compile(r'\bwebservice\.%s\b' % self.wscall.slug), hit_function=accumulate)
|
|
r = TemplateIO(html=True)
|
|
if usage:
|
|
for source_url, source_name in usage.items():
|
|
r += htmltext(f'<li><a href="{source_url}">{source_name}</a></li>\n')
|
|
else:
|
|
r += htmltext('<li class="list-item-no-usage"><p>%s</p></li>') % _('No usage detected.')
|
|
return r.getvalue()
|
|
|
|
def edit(self):
|
|
form = self.wscall_ui.get_form()
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('.')
|
|
|
|
if form.get_submit() == 'submit' and not form.has_errors():
|
|
try:
|
|
self.wscall_ui.submit_form(form)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
return redirect('../%s/' % self.wscall.id)
|
|
|
|
get_response().breadcrumb.append(('edit', _('Edit')))
|
|
get_response().set_title(_('Edit webservice call'))
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<h2>%s</h2>') % _('Edit webservice call')
|
|
r += form.render()
|
|
return r.getvalue()
|
|
|
|
def delete(self):
|
|
form = Form(enctype='multipart/form-data')
|
|
form.widgets.append(
|
|
HtmlWidget('<p>%s</p>' % _('You are about to irrevocably delete this webservice call.'))
|
|
)
|
|
form.add_submit('delete', _('Delete'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|
if form.get_widget('cancel').parse():
|
|
return redirect('..')
|
|
if not form.is_submitted() or form.has_errors():
|
|
get_response().breadcrumb.append(('delete', _('Delete')))
|
|
get_response().set_title(_('Delete webservice call'))
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<h2>%s %s</h2>') % (_('Deleting webservice call:'), self.wscall.name)
|
|
r += form.render()
|
|
return r.getvalue()
|
|
else:
|
|
self.wscall.remove_self()
|
|
return redirect('..')
|
|
|
|
def export(self):
|
|
return misc.xml_response(
|
|
self.wscall, filename='wscall-%s.wcs' % self.wscall.slug, content_type='application/x-wcs-wscall'
|
|
)
|
|
|
|
|
|
class NamedWsCallsDirectory(Directory):
|
|
_q_exports = ['', 'new', ('import', 'p_import'), ('application', 'applications_dir')]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.applications_dir = ApplicationsDirectory(NamedWsCall)
|
|
|
|
def _q_traverse(self, path):
|
|
get_response().breadcrumb.append(('wscalls/', _('Webservice Calls')))
|
|
return super()._q_traverse(path)
|
|
|
|
def _q_index(self):
|
|
from wcs.applications import Application
|
|
|
|
get_response().set_title(_('Webservice Calls'))
|
|
get_response().add_javascript(['popup.js'])
|
|
wscalls = NamedWsCall.select(order_by='name')
|
|
Application.populate_objects(wscalls)
|
|
return template.QommonTemplateResponse(
|
|
templates=['wcs/backoffice/wscalls.html'],
|
|
context={
|
|
'view': self,
|
|
'wscalls': wscalls,
|
|
'applications': Application.select_for_object_type(NamedWsCall.xml_root_node),
|
|
'elements_label': NamedWsCall.verbose_name_plural,
|
|
'has_sidebar': True,
|
|
},
|
|
is_django_native=True,
|
|
)
|
|
|
|
def new(self):
|
|
get_response().breadcrumb.append(('new', _('New')))
|
|
wscall_ui = NamedWsCallUI(None)
|
|
form = wscall_ui.get_form()
|
|
if form.get_widget('cancel').parse():
|
|
return redirect('.')
|
|
|
|
if form.get_submit() == 'submit' and not form.has_errors():
|
|
try:
|
|
wscall_ui.submit_form(form)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
return redirect('.')
|
|
|
|
get_response().set_title(_('New webservice call'))
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<h2>%s</h2>') % _('New webservice call')
|
|
r += form.render()
|
|
return r.getvalue()
|
|
|
|
def _q_lookup(self, component):
|
|
return NamedWsCallPage(component)
|
|
|
|
def p_import(self):
|
|
form = Form(enctype='multipart/form-data')
|
|
import_title = _('Import webservice call')
|
|
|
|
form.add(FileWidget, 'file', title=_('File'), required=True)
|
|
form.add_submit('submit', import_title)
|
|
form.add_submit('cancel', _('Cancel'))
|
|
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('.')
|
|
|
|
if form.is_submitted() and not form.has_errors():
|
|
try:
|
|
return self.import_submit(form)
|
|
except ValueError:
|
|
pass
|
|
|
|
get_response().breadcrumb.append(('import', _('Import')))
|
|
get_response().set_title(import_title)
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<h2>%s</h2>') % import_title
|
|
r += htmltext('<p>%s</p>') % _('You can install a new webservice call by uploading a file.')
|
|
r += form.render()
|
|
return r.getvalue()
|
|
|
|
def import_submit(self, form):
|
|
fp = form.get_widget('file').parse().fp
|
|
|
|
error = False
|
|
try:
|
|
wscall = NamedWsCall.import_from_xml(fp)
|
|
get_session().message = ('info', _('This webservice call has been successfully imported.'))
|
|
except ValueError:
|
|
error = True
|
|
|
|
if error:
|
|
form.set_error('file', _('Invalid File'))
|
|
raise ValueError()
|
|
|
|
try:
|
|
# check slug unicity
|
|
NamedWsCall.get(wscall.slug, ignore_migration=True)
|
|
except KeyError:
|
|
pass
|
|
else:
|
|
wscall.slug = None # a new one will be set in .store()
|
|
wscall.store()
|
|
return redirect('%s/' % wscall.id)
|