From: Plamen Dimitrov Date: Thu, 7 Apr 2022 04:44:29 +0000 (+0300) Subject: Introduce use of defaults i.e. UI-default rather than minimal user cnfvars X-Git-Tag: v1.7.1~3^2~6^2~8 X-Git-Url: http://developer.intra2net.com/git/?p=pyi2ncommon;a=commitdiff_plain;h=188605ae81c6d06bb1a33e911b1d75c8f316de2d Introduce use of defaults i.e. UI-default rather than minimal user cnfvars --- diff --git a/src/cnfline/build_user.py b/src/cnfline/build_user.py deleted file mode 100644 index 3daf729..0000000 --- a/src/cnfline/build_user.py +++ /dev/null @@ -1,91 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from .build_cnfvar import BuildCnfVar -"""Class to create user cnfvar objects on the fly""" - - -class BuildUser(BuildCnfVar): - - def __init__(self, data='', instance=0, line_no=1): - BuildCnfVar.__init__(self, 'USER', instance, data, line_no) - - # the bare defaults the UI adds upon - # creation of new groups - defaults = { - 'USER_DISABLED': '0', - 'USER_FULLNAME': '', - 'USER_GROUPWARE_FOLDER_CALENDAR': 'INBOX/Calendar', - 'USER_GROUPWARE_FOLDER_CONTACTS': 'INBOX/Contacts', - 'USER_GROUPWARE_FOLDER_DRAFTS': 'INBOX/Drafts', - 'USER_GROUPWARE_FOLDER_NOTES': 'INBOX/Notes', - 'USER_GROUPWARE_FOLDER_OUTBOX': 'INBOX/Sent Items', - 'USER_GROUPWARE_FOLDER_TASKS': 'INBOX/Tasks', - 'USER_GROUPWARE_FOLDER_TRASH': 'INBOX/Deleted Items', - # always a member of the 'Alle' group - 'USER_GROUP_MEMBER_REF': '2', - 'USER_LOCALE': '', - 'USER_PASSWORD': 'test1234', - 'USER_TRASH_DELETEDAYS': '30', - 'USER_WEBMAIL_MESSAGES_PER_PAGE': '25', - 'USER_WEBMAIL_SIGNATURE': '', - } - - self.add_defaults(defaults) - - def disabled(self, disabled='1'): - self.update_cnf('USER_DISABLED', 0, disabled) - return self - - def fullname(self, fullname): - self.update_cnf('USER_FULLNAME', 0, fullname) - return self - - def password(self, password): - self.update_cnf('USER_PASSWORD', 0, password) - return self - - def normal_email(self): - self.update_cnf('USER_EMAIL_FORWARD_ENABLE', 0, 'NONE') - return self - - def forward_email(self, email): - self.update_cnf('USER_EMAIL_FORWARD_ENABLE', 0, 'FORWARD') - - addr = self.update_cnf('USER_EMAIL_FORWARD_ADDRESS', 0, '') - self.update_cnf('USER_EMAIL_FORWARD_ADDRESS_ADDR', 0, email, addr) - return self - - def copy_email(self, email): - self.update_cnf('USER_EMAIL_FORWARD_ENABLE', 0, 'COPY') - - addr = self.update_cnf('USER_EMAIL_FORWARD_ADDRESS', 0, '') - self.update_cnf('USER_EMAIL_FORWARD_ADDRESS_ADDR', 0, email, addr) - return self - - def add_group_member_ref(self, group_ref): - self.add_cnf('USER_GROUP_MEMBER_REF', -1, group_ref) - return self diff --git a/src/cnfvar/templates.py b/src/cnfvar/templates.py index 2567058..7e2d9da 100644 --- a/src/cnfvar/templates.py +++ b/src/cnfvar/templates.py @@ -22,11 +22,17 @@ summary ------------------------------------------------------ -Module for one-step dynamic cnfvar configuration using minimal templates. +Module for one-step dynamic cnfvar generation from default value templates. .. codeauthor:: Intra2net +contents +------------------------------------------------------- +These templates contain the bare defaults the UI adds upon +creation of each major and frequently used cnfvar. + + interface ------------------------------------------------------ @@ -43,40 +49,73 @@ log = logging.getLogger('pyi2ncommon.cnfvar.templates') ############################################################################### +# MAJOR CNF DEFAULTS +############################################################################### + + +#: UI defaults for a user instance +user_defaults = { + "USER_DISABLED": "0", + "USER_FULLNAME": "", + "USER_GROUPWARE_FOLDER_CALENDAR": "INBOX/Calendar", + "USER_GROUPWARE_FOLDER_CONTACTS": "INBOX/Contacts", + "USER_GROUPWARE_FOLDER_DRAFTS": "INBOX/Drafts", + "USER_GROUPWARE_FOLDER_NOTES": "INBOX/Notes", + "USER_GROUPWARE_FOLDER_OUTBOX": "INBOX/Sent Items", + "USER_GROUPWARE_FOLDER_TASKS": "INBOX/Tasks", + "USER_GROUPWARE_FOLDER_TRASH": "INBOX/Deleted Items", + # always a member of the 'Alle' group + "USER_GROUP_MEMBER_REF": "2", + "USER_LOCALE": "", + "USER_PASSWORD": "test1234", + "USER_TRASH_DELETEDAYS": "30", + "USER_WEBMAIL_MESSAGES_PER_PAGE": "25", + "USER_WEBMAIL_SIGNATURE": "", +} + + +############################################################################### # MINOR CONFIGURATION ############################################################################### -def simple(varname, data, instance=0): +def template(name, value, instance=-1, defaults=None, **kwargs): """ - Generate a minimal simple cnf variable in terms of required and validated attributes. + Generate a template cnf variable from provided defaults. - :param str varname: cnf variable name - :param str data: cnf variable value + :param str name: cnf variable name + :param str value: cnf variable data value :param int instance: cnf variable instance number + :param defaults: default child variables to populate the cnf variable with + :type defaults: {str, str or {}} or None :returns: generated cnf variable :rtype: :py:class:`Cnf` + + All additional keyword arguments will be used to overwrite the defaults. """ - log.info(f"Generating a simple {varname} cnfvar") - return Cnf(varname, value=data, instance=instance) + log.info(f"Generating a template {name} cnfvar") + cnf = Cnf(name, value=value, instance=instance) + defaults = {} if defaults is None else defaults + cnf.add_children(*[(key, value) for key, value in defaults.items()]) + for key in kwargs.keys(): + cnf.children.single_with_name(f"{name}_{key}").value = kwargs[key] + return cnf -def user(username, password, instance=-1): +def user(name, password, instance=-1, **kwargs): """ - Generate a minimal user cnf variable in terms of required and validated attributes. + Generate a user cnf variable. - :param str username: username for the user + :param str name: username for the user :param str password: password for the user :param int instance: instance number for the user :returns: generated cnf variable :rtype: :py:class:`Cnf` """ - log.info(f"Generating a minimal user {username} cnfvar") - user_cnf = Cnf("user", value=username, instance=instance) - user_cnf.add_children( - ("user_fullname", username.capitalize()), - ("user_group_member_ref", 2), - ("user_password", password)) + log.info(f"Generating a user {name} cnfvar") + user_cnf = template("user", name, instance=instance, defaults=user_defaults, **kwargs) + user_cnf.children.single_with_name("user_fullname").value = name.capitalize() + user_cnf.children.single_with_name("user_password").value = password return user_cnf