2016-03-07 05:48:24 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2023-01-02 00:23:55 -06:00
|
|
|
# Copyright (C) 2013 - 2023, The pgAdmin Development Team
|
2016-03-07 05:48:24 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
Utility classes to register, getter, setter functions for the preferences of a
|
|
|
|
module within the system.
|
|
|
|
"""
|
|
|
|
|
2016-06-21 08:12:14 -05:00
|
|
|
import decimal
|
2023-02-14 23:40:12 -06:00
|
|
|
import json
|
2016-06-21 08:12:14 -05:00
|
|
|
|
|
|
|
import dateutil.parser as dateutil_parser
|
2016-03-07 05:48:24 -06:00
|
|
|
from flask import current_app
|
2021-11-24 05:52:57 -06:00
|
|
|
from flask_babel import gettext
|
2016-07-22 10:25:23 -05:00
|
|
|
from flask_security import current_user
|
2016-06-21 08:12:14 -05:00
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
from pgadmin.model import db, Preferences as PrefTable, \
|
|
|
|
ModulePreference as ModulePrefTable, UserPreference as UserPrefTable, \
|
|
|
|
PreferenceCategory as PrefCategoryTbl
|
|
|
|
|
|
|
|
|
2022-11-18 22:43:41 -06:00
|
|
|
class _Preference():
|
2016-03-07 05:48:24 -06:00
|
|
|
"""
|
|
|
|
Internal class representing module, and categoy bound preference.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
2020-07-14 08:53:50 -05:00
|
|
|
self, cid, name, label, _type, default, **kwargs
|
2016-06-21 08:21:06 -05:00
|
|
|
):
|
2016-03-07 05:48:24 -06:00
|
|
|
"""
|
|
|
|
__init__
|
|
|
|
Constructor/Initializer for the internal _Preference object.
|
|
|
|
|
|
|
|
It creates a new entry for this preference in configuration table based
|
|
|
|
on the name (if not exists), and keep the id of it for on demand value
|
|
|
|
fetching from the configuration table in later stage. Also, keeps track
|
|
|
|
of type of the preference/option, and other supporting parameters like
|
|
|
|
min, max, options, etc.
|
|
|
|
|
|
|
|
:param cid: configuration id
|
|
|
|
:param name: Name of the preference (must be unique for each
|
|
|
|
configuration)
|
|
|
|
:param label: Display name of the options/preference
|
|
|
|
:param _type: Type for proper validation on value
|
|
|
|
:param default: Default value
|
|
|
|
:param help_str: Help string to be shown in preferences dialog.
|
|
|
|
:param min_val: minimum value
|
|
|
|
:param max_val: maximum value
|
|
|
|
:param options: options (Array of list objects)
|
2017-11-20 07:50:47 -06:00
|
|
|
:param select2: select2 options (object)
|
2018-01-25 06:49:06 -06:00
|
|
|
:param fields: field schema (if preference has more than one field to
|
|
|
|
take input from user e.g. keyboardshortcut preference)
|
2019-01-01 03:44:32 -06:00
|
|
|
:param allow_blanks: Flag specify whether to allow blank value.
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
:returns: nothing
|
|
|
|
"""
|
|
|
|
self.cid = cid
|
|
|
|
self.name = name
|
|
|
|
self.default = default
|
|
|
|
self.label = label
|
|
|
|
self._type = _type
|
2020-07-14 08:53:50 -05:00
|
|
|
self.help_str = kwargs.get('help_str', None)
|
2022-03-21 02:59:26 -05:00
|
|
|
self.control_props = kwargs.get('control_props', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
self.min_val = kwargs.get('min_val', None)
|
|
|
|
self.max_val = kwargs.get('max_val', None)
|
|
|
|
self.options = kwargs.get('options', None)
|
2022-03-21 02:59:26 -05:00
|
|
|
self.select = kwargs.get('select', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
self.fields = kwargs.get('fields', None)
|
2023-03-06 05:33:47 -06:00
|
|
|
self.hidden = kwargs.get('hidden', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
self.allow_blanks = kwargs.get('allow_blanks', None)
|
2021-05-25 09:42:57 -05:00
|
|
|
self.disabled = kwargs.get('disabled', False)
|
2021-07-27 04:47:06 -05:00
|
|
|
self.dependents = kwargs.get('dependents', None)
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
# Look into the configuration table to find out the id of the specific
|
|
|
|
# preference.
|
|
|
|
res = PrefTable.query.filter_by(
|
2019-06-10 05:10:49 -05:00
|
|
|
name=name, cid=cid
|
2016-06-21 08:21:06 -05:00
|
|
|
).first()
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
if res is None:
|
2017-03-07 04:29:54 -06:00
|
|
|
# Could not find in the configuration table, we will create new
|
2016-03-07 05:48:24 -06:00
|
|
|
# entry for it.
|
|
|
|
res = PrefTable(name=self.name, cid=cid)
|
|
|
|
db.session.add(res)
|
|
|
|
db.session.commit()
|
|
|
|
res = PrefTable.query.filter_by(
|
|
|
|
name=name
|
2016-06-21 08:21:06 -05:00
|
|
|
).first()
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
# Save this id for letter use.
|
|
|
|
self.pid = res.id
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
"""
|
|
|
|
get
|
|
|
|
Fetch the value from the server for the current user from the
|
|
|
|
configuration table (if available), otherwise returns the default value
|
|
|
|
for it.
|
|
|
|
|
|
|
|
:returns: value for this preference.
|
|
|
|
"""
|
|
|
|
res = UserPrefTable.query.filter_by(
|
|
|
|
pid=self.pid
|
2016-06-21 08:21:06 -05:00
|
|
|
).filter_by(uid=current_user.id).first()
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2017-03-07 04:29:54 -06:00
|
|
|
# Could not find any preference for this user, return default value.
|
2016-03-07 05:48:24 -06:00
|
|
|
if res is None:
|
|
|
|
return self.default
|
|
|
|
|
|
|
|
# The data stored in the configuration will be in string format, we
|
|
|
|
# need to convert them in proper format.
|
2021-01-18 01:32:19 -06:00
|
|
|
is_format_data, data = self._get_format_data(res)
|
|
|
|
if is_format_data:
|
|
|
|
return data
|
|
|
|
|
2020-08-11 04:43:35 -05:00
|
|
|
if self._type == 'text' and res.value == '' and not self.allow_blanks:
|
2020-06-15 05:29:37 -05:00
|
|
|
return self.default
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2020-08-11 04:43:35 -05:00
|
|
|
parser_map = {
|
|
|
|
'integer': int,
|
|
|
|
'numeric': decimal.Decimal,
|
|
|
|
'date': dateutil_parser.parse,
|
|
|
|
'datetime': dateutil_parser.parse,
|
|
|
|
'keyboardshortcut': json.loads
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
return parser_map.get(self._type, lambda v: v)(res.value)
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return self.default
|
2016-03-07 05:48:24 -06:00
|
|
|
return res.value
|
|
|
|
|
2021-01-18 01:32:19 -06:00
|
|
|
def _get_format_data(self, res):
|
|
|
|
"""
|
|
|
|
Configuration data get stored in string format, convert it in to
|
|
|
|
required format.
|
|
|
|
:param res: type value.
|
|
|
|
"""
|
|
|
|
if self._type in ('boolean', 'switch', 'node'):
|
|
|
|
return True, res.value == 'True'
|
|
|
|
if self._type == 'options':
|
|
|
|
for opt in self.options:
|
|
|
|
if 'value' in opt and opt['value'] == res.value:
|
|
|
|
return True, res.value
|
2022-10-20 05:58:42 -05:00
|
|
|
|
|
|
|
if self.control_props and self.control_props['creatable']:
|
|
|
|
return True, res.value
|
|
|
|
|
2022-03-21 02:59:26 -05:00
|
|
|
if self.select and self.select['tags']:
|
2021-01-18 01:32:19 -06:00
|
|
|
return True, res.value
|
|
|
|
return True, self.default
|
2022-03-21 02:59:26 -05:00
|
|
|
if self._type == 'select':
|
2021-01-18 01:32:19 -06:00
|
|
|
if res.value:
|
|
|
|
res.value = res.value.replace('[', '')
|
|
|
|
res.value = res.value.replace(']', '')
|
|
|
|
res.value = res.value.replace('\'', '')
|
|
|
|
return True, [val.strip() for val in res.value.split(',')]
|
|
|
|
return True, None
|
|
|
|
|
|
|
|
return False, None
|
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
def set(self, value):
|
|
|
|
"""
|
|
|
|
set
|
|
|
|
Set the value into the configuration table for this current user.
|
|
|
|
|
|
|
|
:param value: Value to be set
|
|
|
|
|
|
|
|
:returns: nothing.
|
|
|
|
"""
|
|
|
|
# We can't store the values in the given format, we need to convert
|
|
|
|
# them in string first. We also need to validate the value type.
|
2020-08-11 04:43:35 -05:00
|
|
|
|
|
|
|
parser_map = {
|
|
|
|
'integer': int,
|
|
|
|
'numeric': float,
|
|
|
|
'date': dateutil_parser.parse,
|
|
|
|
'datetime': dateutil_parser.parse,
|
|
|
|
'keyboardshortcut': json.dumps
|
|
|
|
}
|
|
|
|
|
|
|
|
error_map = {
|
|
|
|
'keyboardshortcut': 'keyboard shortcut'
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self._type in ('boolean', 'switch', 'node'):
|
2020-08-31 06:15:31 -05:00
|
|
|
assert isinstance(value, bool)
|
2020-08-11 04:43:35 -05:00
|
|
|
elif self._type == 'options':
|
|
|
|
has_value = next((True for opt in self.options
|
|
|
|
if 'value' in opt and opt['value'] == value),
|
|
|
|
False)
|
2022-10-20 05:58:42 -05:00
|
|
|
assert (has_value or (self.control_props and
|
|
|
|
(self.control_props['tags'] or
|
|
|
|
self.control_props['creatable'])))
|
2020-08-11 04:43:35 -05:00
|
|
|
elif self._type == 'date':
|
|
|
|
value = parser_map[self._type](value).date()
|
|
|
|
else:
|
|
|
|
value = parser_map.get(self._type, lambda v: v)(value)
|
2020-08-12 07:12:53 -05:00
|
|
|
if self._type == 'integer':
|
2020-08-11 04:43:35 -05:00
|
|
|
value = self.normalize_range(value)
|
2020-08-31 06:15:31 -05:00
|
|
|
assert isinstance(value, int)
|
2020-08-11 04:43:35 -05:00
|
|
|
if self._type == 'numeric':
|
2020-08-12 07:12:53 -05:00
|
|
|
value = self.normalize_range(value)
|
2020-08-31 06:15:31 -05:00
|
|
|
assert (
|
|
|
|
isinstance(value, int) or isinstance(value, float) or
|
|
|
|
isinstance(value, decimal.Decimal))
|
2020-08-11 04:43:35 -05:00
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return False, gettext(
|
|
|
|
"Invalid value for {0} option.".format(
|
|
|
|
error_map.get(self._type, self._type)))
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
pref = UserPrefTable.query.filter_by(
|
|
|
|
pid=self.pid
|
2016-06-21 08:21:06 -05:00
|
|
|
).filter_by(uid=current_user.id).first()
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2020-08-31 06:15:31 -05:00
|
|
|
value = "{}".format(value)
|
2016-03-07 05:48:24 -06:00
|
|
|
if pref is None:
|
|
|
|
pref = UserPrefTable(
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
uid=current_user.id, pid=self.pid, value=value
|
2016-06-21 08:21:06 -05:00
|
|
|
)
|
2016-03-07 05:48:24 -06:00
|
|
|
db.session.add(pref)
|
|
|
|
else:
|
2017-03-27 11:19:28 -05:00
|
|
|
pref.value = value
|
2016-03-07 05:48:24 -06:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return True, None
|
|
|
|
|
2020-08-11 04:43:35 -05:00
|
|
|
def normalize_range(self, value):
|
|
|
|
ret_val = value
|
|
|
|
if self.min_val is not None and value < self.min_val:
|
|
|
|
ret_val = self.min_val
|
|
|
|
if self.max_val is not None and value > self.max_val:
|
|
|
|
ret_val = self.max_val
|
|
|
|
|
|
|
|
return ret_val
|
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
def to_json(self):
|
|
|
|
"""
|
|
|
|
to_json
|
|
|
|
Returns the JSON object representing this preferences.
|
|
|
|
|
|
|
|
:returns: the JSON representation for this preferences
|
|
|
|
"""
|
|
|
|
res = {
|
|
|
|
'id': self.pid,
|
|
|
|
'cid': self.cid,
|
|
|
|
'name': self.name,
|
|
|
|
'label': self.label or self.name,
|
|
|
|
'type': self._type,
|
|
|
|
'help_str': self.help_str,
|
2022-03-21 02:59:26 -05:00
|
|
|
'control_props': self.control_props,
|
2016-03-07 05:48:24 -06:00
|
|
|
'min_val': self.min_val,
|
|
|
|
'max_val': self.max_val,
|
|
|
|
'options': self.options,
|
2022-03-21 02:59:26 -05:00
|
|
|
'select': self.select,
|
2018-01-25 06:49:06 -06:00
|
|
|
'value': self.get(),
|
|
|
|
'fields': self.fields,
|
2023-03-06 05:33:47 -06:00
|
|
|
'hidden': self.hidden,
|
2021-05-25 09:42:57 -05:00
|
|
|
'disabled': self.disabled,
|
2021-07-27 04:47:06 -05:00
|
|
|
'dependents': self.dependents
|
2016-06-21 08:21:06 -05:00
|
|
|
}
|
2016-03-07 05:48:24 -06:00
|
|
|
return res
|
|
|
|
|
|
|
|
|
2022-11-18 22:43:41 -06:00
|
|
|
class Preferences():
|
2016-03-07 05:48:24 -06:00
|
|
|
"""
|
|
|
|
class Preferences
|
|
|
|
|
|
|
|
It helps to manage all the preferences/options related to a specific
|
|
|
|
module.
|
|
|
|
|
|
|
|
It keeps track of all the preferences registered with it using this class
|
|
|
|
in the group of categories.
|
|
|
|
|
|
|
|
Also, create the required entries for each module, and categories in the
|
|
|
|
preferences tables (if required). If it is already present, it will refer
|
|
|
|
to the existing data from those tables.
|
|
|
|
|
|
|
|
class variables:
|
|
|
|
---------------
|
|
|
|
modules:
|
|
|
|
Dictionary of all the modules, can be refered by its name.
|
|
|
|
Keeps track of all the modules in it, so that - only one object per module
|
|
|
|
gets created. If the same module refered by different object, the
|
|
|
|
categories dictionary within it will be shared between them to keep the
|
|
|
|
consistent data among all the object.
|
|
|
|
|
|
|
|
Instance Definitions:
|
|
|
|
-------- -----------
|
|
|
|
"""
|
|
|
|
modules = dict()
|
|
|
|
|
|
|
|
def __init__(self, name, label=None):
|
|
|
|
"""
|
|
|
|
__init__
|
|
|
|
Constructor/Initializer for the Preferences class.
|
|
|
|
|
|
|
|
:param name: Name of the module
|
|
|
|
:param label: Display name of the module, it will be displayed in the
|
|
|
|
preferences dialog.
|
|
|
|
|
|
|
|
:returns nothing
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.label = label
|
|
|
|
self.categories = dict()
|
|
|
|
|
|
|
|
# Find the entry for this module in the configuration database.
|
|
|
|
module = ModulePrefTable.query.filter_by(name=name).first()
|
|
|
|
|
|
|
|
# Can't find the reference for it in the configuration database,
|
|
|
|
# create on for it.
|
|
|
|
if module is None:
|
|
|
|
module = ModulePrefTable(name=name)
|
|
|
|
db.session.add(module)
|
|
|
|
db.session.commit()
|
|
|
|
module = ModulePrefTable.query.filter_by(name=name).first()
|
|
|
|
|
|
|
|
self.mid = module.id
|
|
|
|
|
|
|
|
if name in Preferences.modules:
|
2016-05-15 05:29:32 -05:00
|
|
|
m = Preferences.modules[name]
|
2016-03-07 05:48:24 -06:00
|
|
|
self.categories = m.categories
|
|
|
|
else:
|
|
|
|
Preferences.modules[name] = self
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
"""
|
|
|
|
to_json
|
|
|
|
Converts the preference object to the JSON Format.
|
|
|
|
|
|
|
|
:returns: a JSON object contains information.
|
|
|
|
"""
|
|
|
|
res = {
|
|
|
|
'id': self.mid,
|
|
|
|
'label': self.label or self.name,
|
2017-06-22 06:18:56 -05:00
|
|
|
'name': self.name,
|
2016-03-07 05:48:24 -06:00
|
|
|
'categories': []
|
|
|
|
}
|
|
|
|
for c in self.categories:
|
|
|
|
cat = self.categories[c]
|
|
|
|
interm = {
|
|
|
|
'id': cat['id'],
|
2023-03-24 05:14:43 -05:00
|
|
|
'name': cat['name'],
|
2016-03-07 05:48:24 -06:00
|
|
|
'label': cat['label'] or cat['name'],
|
|
|
|
'preferences': []
|
2016-06-21 08:21:06 -05:00
|
|
|
}
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
res['categories'].append(interm)
|
|
|
|
|
|
|
|
for p in cat['preferences']:
|
|
|
|
pref = (cat['preferences'][p]).to_json().copy()
|
|
|
|
pref.update({'mid': self.mid, 'cid': cat['id']})
|
|
|
|
interm['preferences'].append(pref)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def __category(self, name, label):
|
|
|
|
"""
|
|
|
|
__category
|
|
|
|
|
|
|
|
A private method to create/refer category for/of this module.
|
|
|
|
|
|
|
|
:param name: Name of the category
|
|
|
|
:param label: Display name of the category, it will be send to
|
|
|
|
client/front end to list down in the preferences/options
|
|
|
|
dialog.
|
|
|
|
:returns: A dictionary object reprenting this category.
|
|
|
|
"""
|
|
|
|
if name in self.categories:
|
|
|
|
res = self.categories[name]
|
|
|
|
# Update the category label (if not yet defined)
|
|
|
|
res['label'] = res['label'] or label
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
cat = PrefCategoryTbl.query.filter_by(
|
|
|
|
mid=self.mid
|
2016-06-21 08:21:06 -05:00
|
|
|
).filter_by(name=name).first()
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
if cat is None:
|
|
|
|
cat = PrefCategoryTbl(name=name, mid=self.mid)
|
|
|
|
db.session.add(cat)
|
|
|
|
db.session.commit()
|
|
|
|
cat = PrefCategoryTbl.query.filter_by(
|
|
|
|
mid=self.mid
|
2016-06-21 08:21:06 -05:00
|
|
|
).filter_by(name=name).first()
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
self.categories[name] = res = {
|
|
|
|
'id': cat.id,
|
|
|
|
'name': name,
|
|
|
|
'label': label,
|
|
|
|
'preferences': dict()
|
2016-06-21 08:21:06 -05:00
|
|
|
}
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def register(
|
2022-03-21 02:59:26 -05:00
|
|
|
self, category, name, label, _type, default, **kwargs
|
2016-03-07 05:48:24 -06:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
register
|
|
|
|
Register/Refer the particular preference in this module.
|
|
|
|
|
|
|
|
:param category: name of the category, in which this preference/option
|
|
|
|
will be displayed.
|
|
|
|
:param name: name of the preference/option
|
|
|
|
:param label: Display name of the preference
|
|
|
|
:param _type: [optional] Type of the options.
|
|
|
|
It is an optional argument, only if this
|
|
|
|
option/preference is registered earlier.
|
|
|
|
:param default: [optional] Default value of the options
|
|
|
|
It is an optional argument, only if this
|
|
|
|
option/preference is registered earlier.
|
|
|
|
:param min_val:
|
|
|
|
:param max_val:
|
|
|
|
:param options:
|
|
|
|
:param help_str:
|
|
|
|
:param category_label:
|
2022-03-21 02:59:26 -05:00
|
|
|
:param select: select control extra options
|
2018-01-25 06:49:06 -06:00
|
|
|
:param fields: field schema (if preference has more than one field to
|
|
|
|
take input from user e.g. keyboardshortcut preference)
|
2019-01-01 03:44:32 -06:00
|
|
|
:param allow_blanks: Flag specify whether to allow blank value.
|
2021-05-25 09:42:57 -05:00
|
|
|
:param disabled: Flag specify whether to disable the setting or not.
|
2016-03-07 05:48:24 -06:00
|
|
|
"""
|
2020-07-14 08:53:50 -05:00
|
|
|
min_val = kwargs.get('min_val', None)
|
|
|
|
max_val = kwargs.get('max_val', None)
|
|
|
|
options = kwargs.get('options', None)
|
|
|
|
help_str = kwargs.get('help_str', None)
|
2022-03-21 02:59:26 -05:00
|
|
|
control_props = kwargs.get('control_props', {})
|
2020-07-14 08:53:50 -05:00
|
|
|
category_label = kwargs.get('category_label', None)
|
2022-03-21 02:59:26 -05:00
|
|
|
select = kwargs.get('select', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
fields = kwargs.get('fields', None)
|
2023-03-06 05:33:47 -06:00
|
|
|
hidden = kwargs.get('hidden', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
allow_blanks = kwargs.get('allow_blanks', None)
|
2021-05-25 09:42:57 -05:00
|
|
|
disabled = kwargs.get('disabled', False)
|
2021-07-27 04:47:06 -05:00
|
|
|
dependents = kwargs.get('dependents', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
cat = self.__category(category, category_label)
|
|
|
|
if name in cat['preferences']:
|
|
|
|
return (cat['preferences'])[name]
|
|
|
|
|
2017-04-05 07:38:14 -05:00
|
|
|
assert label is not None, "Label for a preference cannot be none!"
|
|
|
|
assert _type is not None, "Type for a preference cannot be none!"
|
2016-03-07 05:48:24 -06:00
|
|
|
assert _type in (
|
|
|
|
'boolean', 'integer', 'numeric', 'date', 'datetime',
|
2019-05-31 10:51:30 -05:00
|
|
|
'options', 'multiline', 'switch', 'node', 'text', 'radioModern',
|
2022-03-21 02:59:26 -05:00
|
|
|
'keyboardshortcut', 'select', 'selectFile', 'threshold'
|
2017-04-05 07:38:14 -05:00
|
|
|
), "Type cannot be found in the defined list!"
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
(cat['preferences'])[name] = res = _Preference(
|
2020-07-14 08:53:50 -05:00
|
|
|
cat['id'], name, label, _type, default, help_str=help_str,
|
|
|
|
min_val=min_val, max_val=max_val, options=options,
|
2022-03-21 02:59:26 -05:00
|
|
|
select=select, fields=fields, allow_blanks=allow_blanks,
|
|
|
|
disabled=disabled, dependents=dependents,
|
2023-03-06 05:33:47 -06:00
|
|
|
control_props=control_props, hidden=hidden
|
2016-06-21 08:21:06 -05:00
|
|
|
)
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def preference(self, name):
|
|
|
|
"""
|
|
|
|
preference
|
|
|
|
Refer the particular preference in this module.
|
|
|
|
|
|
|
|
:param name: name of the preference/option
|
|
|
|
"""
|
|
|
|
for key in self.categories:
|
|
|
|
cat = self.categories[key]
|
|
|
|
if name in cat['preferences']:
|
|
|
|
return (cat['preferences'])[name]
|
|
|
|
|
2016-08-08 05:59:37 -05:00
|
|
|
return None
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def preferences(cls):
|
|
|
|
"""
|
|
|
|
preferences
|
|
|
|
Convert all the module preferences in the JSON format.
|
|
|
|
|
|
|
|
:returns: a list of the preferences for each of the modules.
|
|
|
|
"""
|
|
|
|
res = []
|
|
|
|
|
|
|
|
for m in Preferences.modules:
|
|
|
|
res.append(Preferences.modules[m].to_json())
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def register_preference(
|
2022-03-21 02:59:26 -05:00
|
|
|
cls, module, category, name, label, _type, **kwargs
|
2016-06-21 08:21:06 -05:00
|
|
|
):
|
2016-03-07 05:48:24 -06:00
|
|
|
"""
|
|
|
|
register
|
|
|
|
Register/Refer a preference in the system for any module.
|
|
|
|
|
|
|
|
:param module: Name of the module
|
|
|
|
:param category: Name of category
|
|
|
|
:param name: Name of the option
|
|
|
|
:param label: Label of the option, shown in the preferences dialog.
|
|
|
|
:param _type: Type of the option.
|
|
|
|
Allowed type of options are as below:
|
|
|
|
boolean, integer, numeric, date, datetime,
|
|
|
|
options, multiline, switch, node
|
|
|
|
"""
|
2021-03-02 03:23:05 -06:00
|
|
|
default = kwargs.get('default')
|
2020-07-14 08:53:50 -05:00
|
|
|
min_val = kwargs.get('min_val', None)
|
|
|
|
max_val = kwargs.get('max_val', None)
|
|
|
|
options = kwargs.get('options', None)
|
|
|
|
help_str = kwargs.get('help_str', None)
|
2022-03-21 02:59:26 -05:00
|
|
|
control_props = kwargs.get('control_props', None)
|
2020-07-14 08:53:50 -05:00
|
|
|
module_label = kwargs.get('module_label', None)
|
|
|
|
category_label = kwargs.get('category_label', None)
|
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
if module in Preferences.modules:
|
|
|
|
m = Preferences.modules[module]
|
|
|
|
# Update the label (if not defined yet)
|
|
|
|
m.label = m.label or module_label
|
|
|
|
else:
|
|
|
|
m = Preferences(module, module_label)
|
|
|
|
|
|
|
|
return m.register(
|
2020-07-14 08:53:50 -05:00
|
|
|
category, name, label, _type, default, min_val=min_val,
|
|
|
|
max_val=max_val, options=options, help_str=help_str,
|
2022-03-21 02:59:26 -05:00
|
|
|
control_props=control_props,
|
2020-07-14 08:53:50 -05:00
|
|
|
category_label=category_label
|
2016-06-21 08:21:06 -05:00
|
|
|
)
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2017-10-30 07:50:25 -05:00
|
|
|
@staticmethod
|
|
|
|
def raw_value(_module, _preference, _category=None, _user_id=None):
|
|
|
|
# Find the entry for this module in the configuration database.
|
|
|
|
module = ModulePrefTable.query.filter_by(name=_module).first()
|
|
|
|
|
|
|
|
if module is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if _category is None:
|
|
|
|
_category = _module
|
|
|
|
|
|
|
|
if _user_id is None:
|
|
|
|
_user_id = getattr(current_user, 'id', None)
|
|
|
|
if _user_id is None:
|
|
|
|
return None
|
|
|
|
|
2018-01-31 07:58:55 -06:00
|
|
|
cat = PrefCategoryTbl.query.filter_by(
|
|
|
|
mid=module.id).filter_by(name=_category).first()
|
2017-10-30 07:50:25 -05:00
|
|
|
|
|
|
|
if cat is None:
|
|
|
|
return None
|
|
|
|
|
2018-01-31 07:58:55 -06:00
|
|
|
pref = PrefTable.query.filter_by(
|
|
|
|
name=_preference).filter_by(cid=cat.id).first()
|
2017-10-30 07:50:25 -05:00
|
|
|
|
|
|
|
if pref is None:
|
|
|
|
return None
|
|
|
|
|
2018-01-31 07:58:55 -06:00
|
|
|
user_pref = UserPrefTable.query.filter_by(
|
2017-10-30 07:50:25 -05:00
|
|
|
pid=pref.id
|
|
|
|
).filter_by(uid=_user_id).first()
|
|
|
|
|
|
|
|
if user_pref is not None:
|
|
|
|
return user_pref.value
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
@classmethod
|
2016-08-08 05:59:37 -05:00
|
|
|
def module(cls, name, create=True):
|
2016-03-07 05:48:24 -06:00
|
|
|
"""
|
|
|
|
module (classmethod)
|
|
|
|
Get the module preferences object
|
|
|
|
|
|
|
|
:param name: Name of the module
|
2016-08-08 05:59:37 -05:00
|
|
|
:param create: Flag to create Preferences object
|
2016-03-07 05:48:24 -06:00
|
|
|
:returns: a Preferences object representing for the module.
|
|
|
|
"""
|
|
|
|
if name in Preferences.modules:
|
|
|
|
m = Preferences.modules[name]
|
|
|
|
# Update the label (if not defined yet)
|
|
|
|
if m.label is None:
|
|
|
|
m.label = name
|
|
|
|
return m
|
|
|
|
|
2016-08-08 05:59:37 -05:00
|
|
|
if create:
|
|
|
|
return Preferences(name, None)
|
|
|
|
|
|
|
|
return None
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def save(cls, mid, cid, pid, value):
|
|
|
|
"""
|
|
|
|
save
|
|
|
|
Update the value for the preference in the configuration database.
|
|
|
|
|
|
|
|
:param mid: Module ID
|
|
|
|
:param cid: Category ID
|
|
|
|
:param pid: Preference ID
|
|
|
|
:param value: Value for the options
|
|
|
|
"""
|
|
|
|
# Find the entry for this module in the configuration database.
|
|
|
|
module = ModulePrefTable.query.filter_by(id=mid).first()
|
|
|
|
|
|
|
|
# Can't find the reference for it in the configuration database,
|
|
|
|
# create on for it.
|
|
|
|
if module is None:
|
2017-11-01 07:58:19 -05:00
|
|
|
return False, gettext("Could not find the specified module.")
|
2016-03-07 05:48:24 -06:00
|
|
|
|
|
|
|
m = cls.modules[module.name]
|
|
|
|
|
|
|
|
if m is None:
|
|
|
|
return False, gettext(
|
2016-05-06 07:53:48 -05:00
|
|
|
"Module '{0}' is no longer in use."
|
2016-03-07 05:48:24 -06:00
|
|
|
).format(module.name)
|
|
|
|
|
|
|
|
category = None
|
|
|
|
|
|
|
|
for c in m.categories:
|
|
|
|
cat = m.categories[c]
|
|
|
|
if cid == cat['id']:
|
|
|
|
category = cat
|
|
|
|
break
|
|
|
|
|
|
|
|
if category is None:
|
|
|
|
return False, gettext(
|
2016-06-21 08:21:06 -05:00
|
|
|
"Module '{0}' does not have category with id '{1}'"
|
2016-03-07 05:48:24 -06:00
|
|
|
).format(module.name, cid)
|
|
|
|
|
|
|
|
preference = None
|
|
|
|
|
|
|
|
for p in category['preferences']:
|
|
|
|
pref = (category['preferences'])[p]
|
|
|
|
|
|
|
|
if pref.pid == pid:
|
|
|
|
preference = pref
|
|
|
|
break
|
|
|
|
|
|
|
|
if preference is None:
|
|
|
|
return False, gettext(
|
2016-06-21 08:21:06 -05:00
|
|
|
"Could not find the specified preference."
|
2016-03-07 05:48:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
pref.set(value)
|
|
|
|
except Exception as e:
|
2018-07-09 08:08:41 -05:00
|
|
|
current_app.logger.exception(e)
|
2016-03-07 05:48:24 -06:00
|
|
|
return False, str(e)
|
|
|
|
|
|
|
|
return True, None
|
2021-06-10 12:19:05 -05:00
|
|
|
|
|
|
|
def migrate_user_preferences(self, pid, converter_func):
|
|
|
|
"""
|
|
|
|
This function is used to migrate user preferences.
|
|
|
|
"""
|
|
|
|
user_prefs = UserPrefTable.query.filter_by(
|
|
|
|
pid=pid
|
|
|
|
)
|
|
|
|
for pref in user_prefs:
|
|
|
|
pref.value = converter_func(pref.value)
|
|
|
|
|
|
|
|
db.session.commit()
|