#!/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 item changelogs.
"""
__author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import flask
from flask_babel import lazy_gettext
import hawat.const
import hawat.acl
import hawat.menu
from hawat.base import HawatBlueprint
from hawat.view import BaseSearchView, ItemShowView
from hawat.view.mixin import HTMLMixin, SQLAlchemyMixin
from hawat.blueprints.changelogs.forms import ItemChangeLogSearchForm
BLUEPRINT_NAME = 'changelogs'
"""Name of the blueprint as module global constant."""
[docs]class SearchView(HTMLMixin, SQLAlchemyMixin, BaseSearchView): # pylint: disable=locally-disabled,too-many-ancestors
"""
General item changelog record listing.
"""
methods = ['GET']
authentication = True
authorization = [hawat.acl.PERMISSION_POWER]
[docs] @classmethod
def get_view_title(cls, **kwargs):
return lazy_gettext('Search item changelogs')
# ---------------------------------------------------------------------------
@property
def dbmodel(self):
return self.get_model(hawat.const.MODEL_ITEM_CHANGELOG)
[docs] @staticmethod
def build_query(query, model, form_args):
# Adjust query based on lower time boundary selection.
if 'dt_from' in form_args and form_args['dt_from']:
query = query.filter(model.createtime >= form_args['dt_from'])
# Adjust query based on upper time boundary selection.
if 'dt_to' in form_args and form_args['dt_to']:
query = query.filter(model.createtime <= form_args['dt_to'])
# Adjust query based on changelog author selection.
if 'authors' in form_args and form_args['authors']:
query = query.filter(model.author_id.in_([x.id for x in form_args['authors']]))
# Adjust query based on changelog operation selection.
if 'operations' in form_args and form_args['operations']:
query = query.filter(model.operation.in_(form_args['operations']))
# Adjust query based on changelog model selection.
if 'imodel' in form_args and form_args['imodel']:
# Adjust query based on changelog model ID selection.
if 'imodel_id' in form_args and form_args['imodel_id']:
query = query.filter(model.model == form_args['imodel'], model.imodel_id == form_args['imodel_id'])
else:
query = query.filter(model.model == form_args['imodel'])
# Return the result sorted by creation time in descending order.
return query.order_by(model.createtime.desc())
[docs]class ShowView(HTMLMixin, SQLAlchemyMixin, ItemShowView):
"""
Detailed network record view.
"""
methods = ['GET']
authentication = True
authorization = [hawat.acl.PERMISSION_POWER]
[docs] @classmethod
def get_view_title(cls, **kwargs):
return lazy_gettext('Show item changelog record details')
# ---------------------------------------------------------------------------
@property
def dbmodel(self):
return self.get_model(hawat.const.MODEL_ITEM_CHANGELOG)
# -------------------------------------------------------------------------------
[docs]class ItemChangeLogsBlueprint(HawatBlueprint):
"""Pluggable module - item changelog record management (*changelogs*)."""
[docs] @classmethod
def get_module_title(cls):
return lazy_gettext('Item changelog record management')
[docs] def register_app(self, app):
app.menu_main.add_entry(
'view',
'admin.{}'.format(BLUEPRINT_NAME),
position=80,
view=SearchView
)
# -------------------------------------------------------------------------------
[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 = ItemChangeLogsBlueprint(
BLUEPRINT_NAME,
__name__,
template_folder='templates',
url_prefix='/{}'.format(BLUEPRINT_NAME)
)
hbp.register_view_class(SearchView, '/search')
hbp.register_view_class(ShowView, '/<int:item_id>/show')
return hbp