#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license, see LICENSE file.
# -------------------------------------------------------------------------------
"""
This pluggable module provides access to group reporting settings management features.
These features include:
* detailed reporting settings view
* creating new reporting settings
* updating existing reporting settings
* deleting existing reporting settings
"""
__author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import markupsafe
import flask
import flask_principal
from flask_babel import gettext, lazy_gettext
import mentat.reports.utils
from mentat.datatype.sqldb import SettingsReportingModel, ItemChangeLogModel
import hawat.acl
import hawat.menu
from hawat.base import HawatBlueprint
from hawat.view import ItemShowView, ItemCreateView, ItemUpdateView
from hawat.view.mixin import HTMLMixin, SQLAlchemyMixin
from hawat.blueprints.settings_reporting.forms import CreateSettingsReportingForm, \
UpdateSettingsReportingForm
BLUEPRINT_NAME = 'settings_reporting'
"""Name of the blueprint as module global constant."""
[docs]class ShowView(HTMLMixin, SQLAlchemyMixin, ItemShowView):
"""
Detailed reporting settings view.
"""
methods = ['GET']
authentication = True
[docs] @classmethod
def get_view_icon(cls):
return 'module-settings-reporting'
[docs] @classmethod
def get_view_title(cls, **kwargs):
return lazy_gettext('Show reporting settings')
@property
def dbmodel(self):
return SettingsReportingModel
[docs] @classmethod
def authorize_item_action(cls, **kwargs):
permission_mm = flask_principal.Permission(
hawat.acl.MembershipNeed(kwargs['item'].group.id),
hawat.acl.ManagementNeed(kwargs['item'].group.id)
)
return hawat.acl.PERMISSION_POWER.can() or permission_mm.can()
[docs] def do_before_response(self, **kwargs):
item = self.response_context['item']
system_default_repsettings = mentat.reports.utils.ReportingSettings(
item.group
)
self.response_context.update(
system_default_repsettings=system_default_repsettings
)
if self.can_access_endpoint('settings_reporting.update', item=item) and self.has_endpoint('changelogs.search'):
self.response_context.update(
context_action_menu_changelogs=self.get_endpoint_class(
'changelogs.search'
).get_context_action_menu()
)
item_changelog = self.dbsession.query(ItemChangeLogModel). \
filter(ItemChangeLogModel.model == item.__class__.__name__). \
filter(ItemChangeLogModel.model_id == item.id). \
order_by(ItemChangeLogModel.createtime.desc()). \
limit(100). \
all()
self.response_context.update(item_changelog=item_changelog)
[docs]class CreateView(HTMLMixin, SQLAlchemyMixin, ItemCreateView): # pylint: disable=locally-disabled,too-many-ancestors
"""
View for creating new reporting settings.
"""
methods = ['GET', 'POST']
authentication = True
[docs] @classmethod
def get_view_title(cls, **kwargs):
return lazy_gettext('Create new reporting settings')
@property
def dbmodel(self):
return SettingsReportingModel
@property
def dbchlogmodel(self):
return ItemChangeLogModel
[docs] @classmethod
def authorize_item_action(cls, **kwargs):
permission_m = flask_principal.Permission(
hawat.acl.ManagementNeed(kwargs['item'].group.id)
)
return hawat.acl.PERMISSION_POWER.can() or permission_m.can()
[docs] @staticmethod
def get_message_success(**kwargs):
return gettext(
'Reporting settings <strong>%(item_id)s</strong> for group <strong>%(parent_id)s</strong> were successfully created.',
item_id=markupsafe.escape(str(kwargs['item'])),
parent_id=markupsafe.escape(str(kwargs['item'].group))
)
[docs] @staticmethod
def get_message_failure(**kwargs):
return gettext(
'Unable to create new reporting settings for group <strong>%(parent_id)s</strong>.',
parent_id=markupsafe.escape(str(kwargs['item'].group))
)
[docs] @staticmethod
def get_message_cancel(**kwargs):
return gettext(
'Canceled creating new reporting settings for group <strong>%(parent_id)s</strong>.',
parent_id=markupsafe.escape(str(kwargs['item'].group))
)
[docs]class UpdateView(HTMLMixin, SQLAlchemyMixin, ItemUpdateView): # pylint: disable=locally-disabled,too-many-ancestors
"""
View for updating existing reporting settings.
"""
methods = ['GET', 'POST']
authentication = True
[docs] @classmethod
def get_view_title(cls, **kwargs):
return lazy_gettext('Update reporting settings details')
@property
def dbmodel(self):
return SettingsReportingModel
@property
def dbchlogmodel(self):
return ItemChangeLogModel
[docs] @classmethod
def authorize_item_action(cls, **kwargs):
permission_m = flask_principal.Permission(
hawat.acl.ManagementNeed(kwargs['item'].group.id)
)
return hawat.acl.PERMISSION_POWER.can() or permission_m.can()
[docs] @staticmethod
def get_message_success(**kwargs):
return gettext(
'Reporting settings <strong>%(item_id)s</strong> for group <strong>%(parent_id)s</strong> were successfully updated.',
item_id=markupsafe.escape(str(kwargs['item'])),
parent_id=markupsafe.escape(str(kwargs['item'].group))
)
[docs] @staticmethod
def get_message_failure(**kwargs):
return gettext(
'Unable to update reporting settings <strong>%(item_id)s</strong> for group <strong>%(parent_id)s</strong>.',
item_id=markupsafe.escape(str(kwargs['item'])),
parent_id=markupsafe.escape(str(kwargs['item'].group))
)
[docs] @staticmethod
def get_message_cancel(**kwargs):
return gettext(
'Canceled updating reporting settings <strong>%(item_id)s</strong> for group <strong>%(parent_id)s</strong>.',
item_id=markupsafe.escape(str(kwargs['item'])),
parent_id=markupsafe.escape(str(kwargs['item'].group))
)
# -------------------------------------------------------------------------------
[docs]class SettingsReportingBlueprint(HawatBlueprint):
"""Pluggable module - reporting settings. (*settings_reporting*)"""
[docs] @classmethod
def get_module_title(cls):
return lazy_gettext('Reporting settings management')
# -------------------------------------------------------------------------------
[docs]def get_blueprint():
"""
Mandatory interface for :py:mod:`hawat.Hawat` and factory function. This function
must return a valid instance of :py:class:`hawat.app.HawatBlueprint` or
:py:class:`flask.Blueprint`.
"""
hbp = SettingsReportingBlueprint(
BLUEPRINT_NAME,
__name__,
template_folder='templates',
url_prefix='/{}'.format(BLUEPRINT_NAME)
)
hbp.register_view_class(CreateView, '/create')
hbp.register_view_class(ShowView, '/<int:item_id>/show')
hbp.register_view_class(UpdateView, '/<int:item_id>/update')
return hbp