wcs/wcs/backoffice/studio.py

145 lines
6.1 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2019 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/>.
from quixote import get_publisher, get_request
from quixote.directory import Directory
from wcs.admin.logged_errors import LoggedErrorsDirectory
from wcs.backoffice.deprecations import DeprecationsDirectory
from wcs.blocks import BlockDef
from wcs.carddef import CardDef
from wcs.data_sources import NamedDataSource
from wcs.formdef import FormDef
from wcs.mail_templates import MailTemplate
from wcs.qommon import _, misc, pgettext, template
from wcs.qommon.backoffice.listing import pagination_links
from wcs.qommon.backoffice.menu import html_top
from wcs.qommon.form import get_response
from wcs.workflows import Workflow
from wcs.wscalls import NamedWsCall
class ChangesDirectory(Directory):
_q_exports = ['']
def _q_index(self):
get_response().breadcrumb.append(('all-changes/', pgettext('studio', 'All changes')))
html_top(pgettext('studio', 'All Changes'))
limit = misc.get_int_or_400(
get_request().form.get('limit', get_publisher().get_site_option('default-page-size')) or 20
)
offset = misc.get_int_or_400(get_request().form.get('offset', 0))
backoffice_root = get_publisher().get_backoffice_root()
object_types = []
if backoffice_root.is_accessible('workflows'):
object_types += [Workflow, MailTemplate]
if backoffice_root.is_accessible('forms'):
object_types += [NamedDataSource, BlockDef, FormDef]
if backoffice_root.is_accessible('workflows'):
object_types += [NamedDataSource]
if backoffice_root.is_accessible('settings'):
object_types += [NamedDataSource, NamedWsCall]
if backoffice_root.is_accessible('cards'):
object_types += [CardDef]
object_types = [ot.xml_root_node for ot in object_types]
objects = []
links = ''
if get_publisher().snapshot_class:
objects = get_publisher().snapshot_class.get_recent_changes(
object_types=object_types, limit=limit, offset=offset
)
total_count = get_publisher().snapshot_class.count_recent_changes(object_types=object_types)
links = pagination_links(offset, limit, total_count, load_js=False)
return template.QommonTemplateResponse(
templates=['wcs/backoffice/changes.html'],
context={
'objects': objects,
'pagination_links': links,
},
)
def is_accessible(self, user):
return user.is_admin
class StudioDirectory(Directory):
_q_exports = ['', 'deprecations', ('logged-errors', 'logged_errors_dir'), ('all-changes', 'changes_dir')]
deprecations = DeprecationsDirectory()
changes_dir = ChangesDirectory()
def __init__(self):
self.logged_errors_dir = LoggedErrorsDirectory(parent_dir=self)
def html_top(self, title):
return html_top('studio', title)
def _q_traverse(self, path):
get_response().breadcrumb.append(('studio/', _('Studio')))
return super()._q_traverse(path)
def _q_index(self):
self.html_top(_('Studio'))
extra_links = []
backoffice_root = get_publisher().get_backoffice_root()
object_types = []
if backoffice_root.is_accessible('forms'):
extra_links.append(('../forms/blocks/', pgettext('studio', 'Field blocks')))
if backoffice_root.is_accessible('workflows'):
extra_links.append(('../workflows/mail-templates/', pgettext('studio', 'Mail templates')))
object_types += [Workflow, MailTemplate]
if backoffice_root.is_accessible('forms'):
extra_links.append(('../forms/data-sources/', pgettext('studio', 'Data sources')))
object_types += [NamedDataSource, BlockDef, FormDef]
elif backoffice_root.is_accessible('workflows'):
extra_links.append(('../workflows/data-sources/', pgettext('studio', 'Data sources')))
object_types += [NamedDataSource]
elif backoffice_root.is_accessible('settings'):
extra_links.append(('../settings/data-sources/', pgettext('studio', 'Data sources')))
object_types += [NamedDataSource]
if backoffice_root.is_accessible('settings'):
extra_links.append(('../settings/wscalls/', pgettext('studio', 'Webservice calls')))
object_types += [NamedWsCall]
if backoffice_root.is_accessible('cards'):
object_types += [CardDef]
user = get_request().user
context = {
'has_sidebar': False,
'extra_links': extra_links,
'recent_errors': LoggedErrorsDirectory.get_errors(offset=0, limit=5)[0],
'show_all_changes': get_publisher().snapshot_class and user and user.is_admin,
}
if get_publisher().snapshot_class:
context['recent_objects'] = get_publisher().snapshot_class.get_recent_changes(
object_types=[ot.xml_root_node for ot in object_types],
user=get_request().user,
)
return template.QommonTemplateResponse(
templates=['wcs/backoffice/studio.html'], context=context, is_django_native=True
)
def is_accessible(self, user):
backoffice_root = get_publisher().get_backoffice_root()
return (
backoffice_root.is_accessible('forms')
or backoffice_root.is_accessible('workflows')
or backoffice_root.is_accessible('cards')
)