Source code for hawat.blueprints.changelogs.forms

#!/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 module contains custom item changelog search form for Hawat.
"""

__author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"

import wtforms
from wtforms_sqlalchemy.fields import QuerySelectMultipleField
import flask
import flask_wtf
from flask_babel import lazy_gettext

import hawat.const
import hawat.forms
import hawat.db


[docs]def get_item_operation_choices(): """ Return select choices for item changelog operations. """ item_changelog_model = flask.current_app.get_model(hawat.const.MODEL_ITEM_CHANGELOG) operations_list = hawat.db.db_query(item_changelog_model). \ distinct(item_changelog_model.operation). \ all() return list( zip( [x.operation for x in operations_list], [lazy_gettext(x.operation) for x in operations_list] ) )
[docs]def get_item_model_choices(): """ Return select choices for item changelog item models. """ item_changelog_model = flask.current_app.get_model(hawat.const.MODEL_ITEM_CHANGELOG) models_list = hawat.db.db_query(item_changelog_model). \ distinct(item_changelog_model.model). \ all() return list( zip( [x.model for x in models_list], [lazy_gettext(x.model) for x in models_list] ) )
[docs]class ItemChangeLogSearchForm(hawat.forms.BaseSearchForm): """ Class representing item changelog search form. """ authors = QuerySelectMultipleField( lazy_gettext('Authors:'), query_factory=hawat.forms.get_available_users, get_label=lambda x: '{} ({})'.format(x.fullname, x.login) ) operations = wtforms.SelectMultipleField( lazy_gettext('Operations:'), validators=[ wtforms.validators.Optional(), ], filters=[lambda x: x or []] ) imodel = wtforms.SelectField( lazy_gettext('Item model:'), validators=[ wtforms.validators.Optional(), ], choices=[('', lazy_gettext('Nothing selected'))], filters=[lambda x: x or None], default='' ) imodel_id = wtforms.IntegerField( lazy_gettext('Model ID:'), validators=[ wtforms.validators.Optional(), ] ) dt_from = hawat.forms.SmartDateTimeField( lazy_gettext('From:'), validators=[ wtforms.validators.Optional() ], default=lambda: hawat.forms.default_dt_with_delta(hawat.const.DEFAULT_RESULT_TIMEDELTA) ) dt_to = hawat.forms.SmartDateTimeField( lazy_gettext('To:'), validators=[ wtforms.validators.Optional() ] ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.operations.choices = get_item_operation_choices() self.imodel.choices[1:] = get_item_model_choices()
[docs] @staticmethod def is_multivalue(field_name): """ Check, if given form field is a multi-value field. :param str field_name: Name of the form field :return: ``True``, if the field can contain multiple values, ``False`` otherwise :rtype: bool """ if field_name in ('authors', 'operations'): return True return False
[docs]class ItemChangeLogDashboardForm(flask_wtf.FlaskForm): """ Class representing item changelog dashboard search form. """ dt_from = hawat.forms.SmartDateTimeField( lazy_gettext('From:'), validators=[ wtforms.validators.Optional() ], default=lambda: hawat.forms.default_dt_with_delta(hawat.const.DEFAULT_RESULT_TIMEDELTA) ) dt_to = hawat.forms.SmartDateTimeField( lazy_gettext('To:'), validators=[ wtforms.validators.Optional() ] ) submit = wtforms.SubmitField( lazy_gettext('Calculate') )