ucsschool.importer.models package

Contents

ucsschool.importer.models package#

Submodules#

ucsschool.importer.models.import_user module#

Representation of a user read from a file.

class ucsschool.importer.models.import_user.FunctionSignature(name, args, kwargs)#

Bases: tuple

Create new instance of FunctionSignature(name, args, kwargs)

args#

Alias for field number 1

kwargs#

Alias for field number 2

name#

Alias for field number 0

class ucsschool.importer.models.import_user.UsernameUniquenessTuple(record_uid, source_uid, dn)#

Bases: tuple

Create new instance of UsernameUniquenessTuple(record_uid, source_uid, dn)

dn#

Alias for field number 2

record_uid#

Alias for field number 0

source_uid#

Alias for field number 1

class ucsschool.importer.models.import_user.ImportUser(name: str = None, school: str = None, **kwargs: str)[source]#

Bases: User

Representation of a user read from a file. Abstract class, please use one of its subclasses ImportStaff etc.

An import profile and a factory must have been loaded, before the class can be used. A convenience module does this:

from ucsschool.importer.utils.shell import *
user = factory.make_import_user(roles)

Create ImportUser object (neither saved nor loaded from LDAP yet). The dn attribute is calculated.

Parameters:
  • name (str) – username

  • school (str) – OU

  • kwargs – attributes to set on user object

source_uid: str = <ucsschool.lib.models.attributes.SourceUID object>#
record_uid: str = <ucsschool.lib.models.attributes.RecordUID object>#
no_overwrite_attributes: List[str] = ['mailPrimaryAddress', 'uid']#
factory = None#
ucr = {}#
prop = <univention.admin.property object>#
build_hook_line(hook_time: str, func_name: str) str[source]#

Recreate original input data for hook creation.

IMPLEMENTME if the Reader class in use does not put a list with the original input text in self.input_data. return _build_hook_line() with a list as argument.

Parameters:
  • hook_time (str) – pre or post

  • func_name (str) – create, modify, move or remove

Returns:

return code of lib hooks

Return type:

int

call_hooks(hook_time: str, func_name: str, lo: LoType) None[source]#

Runs PyHooks, then ucs-school-libs fork hooks.

Parameters:
Returns:

return code of lib hooks

call_format_hook(prop_name: str, fields: Dict[str, Any]) Dict[str, Any][source]#

Run format hooks.

Parameters:
  • prop_name (str) – the property for format

  • fields (dict) – dictionary to manipulate in hook, will be used later to format the property

Returns:

manipulated dictionary

Return type:

dict

change_school(school: str, lo: LoType) bool[source]#

Change primary school of user.

Parameters:
  • school (str) – new OU

  • lo (univention.admin.uldap.access connection) – LDAP connection object

Returns:

whether the school change succeeded

Return type:

bool

check_schools(lo: LoType, additional_schools: Iterable[str] | None = None) None[source]#

Verify that the “school” and “schools” attributes are correct. Check is case sensitive (Bug #42456).

Parameters:
  • lo (univention.admin.uldap.access connection) – LDAP connection object

  • additional_schools (list(str)) – list of school name to check additionally to the one in self.schools

Returns:

None

Return type:

None

Raises:

UnknownSchoolName – if a school is not known

create(lo: LoType, validate: bool | None = True) bool[source]#

Create user object.

Parameters:
  • lo (univention.admin.uldap.access connection) – LDAP connection object

  • validate (bool) – if the users attributes should be checked by UDM

Returns:

whether the object created succeeded

Return type:

bool

create_without_hooks_roles(lo: LoType) None[source]#

Run by py:meth:create_without_hooks() before py:meth:validate() (and thus before py:meth:do_create()).

classmethod get_ldap_filter_for_user_role() str[source]#
classmethod get_by_import_id(connection: LoType, source_uid: str, record_uid: str, superordinate: str | None = None, udm_properties: Iterable | None = None) ImportUser[source]#

Retrieve an ImportUser.

Parameters:
  • connection (univention.admin.uldap.access) – uldap object

  • source_uid (str) – source DB identifier

  • record_uid (str) – source record identifier

  • superordinate (str) – superordinate

  • udm_properties (iterable) – list of udm attributes to load into self.udm_properties

Returns:

object of ImportUser subclass loaded from LDAP or raises NoObject

Return type:

ImportUser

Raises:

ucsschool.lib.models.base.NoObject – if no user was found

deactivate() None[source]#

Deactivate user account. Caller must run modify().

expire(expiry: str) None[source]#

Set the account expiration date. Caller must run modify().

Parameters:

expiry (str) – expire date “%Y-%m-%d” or “”

Deprecated since version 4.4: v9 Use user.self.expiration_date = expiry instead.

classmethod from_dict(a_dict: Dict[str, Any]) ImportUser[source]#

Create user object from a dictionary created by to_dict().

Parameters:

a_dict (dict) – dictionary created by to_dict()

Returns:

ImportUser instance

Return type:

ImportUser

classmethod get_all_school_names(lo: LoType) Iterable[str][source]#
has_purge_timestamp(connection: LoType) bool[source]#

Check if the user account has a purge timestamp set (regardless if it is in the future or past).

Parameters:

connection (univention.admin.uldap.access) – uldap connection object

Returns:

whether the user account has a purge timestamp set

Return type:

bool

has_expired(connection: LoType) bool[source]#

Check if the user account has expired.

Parameters:

connection (univention.admin.uldap.access) – uldap connection object

Returns:

whether the user account has expired

Return type:

bool

has_expiry(connection: LoType) bool[source]#

Check if the user account has an expiry date set (regardless if it is in the future or past).

Parameters:

connection (univention.admin.uldap.access) – uldap connection object

Returns:

whether the user account has an expiry date set

Return type:

bool

property reader#
property lo: LoType#

LDAP connection object

Read-write cn=admin connection in a real run, read-only cn=admin connection during a dry-run.

prepare_all(new_user: bool | None = False) None[source]#

Necessary preparation to modify a user in UCS. Runs all make_* functions.

Parameters:

new_user (bool) – if a password should be created

Returns:

None

prepare_attributes(new_user: bool | None = False) None[source]#

Run make_* functions for all Attributes of ucsschool.lib.models.user.User.

Parameters:

new_user (bool) – if a password should be created

Returns:

None

prepare_udm_properties() None[source]#

Create self.udm_properties from schemes configured in config[“scheme”]. Existing entries will be overwritten unless listed in UCRV ucsschool/import/generate/user/attributes/no-overwrite-by-schema.

  • Attributes (email, record_uid, [user]name etc.) are ignored, as they are processed separately

    in make_*.

  • See /usr/share/doc/ucs-school-import/user_import_configuration_readme.txt.gz section “scheme”

    for details on the configuration.

prepare_uids() None[source]#

Necessary preparation to detect if user exists in UCS. Runs make_* functions for record_uid and source_uid Attributes of ImportUser.

make_birthday() str | None[source]#

Set User.birthday attribute.

make_expiration_date() str | None[source]#

Set User.expiration_date attribute.

parse_date(text: str) str[source]#
make_classes() Dict[str, Dict[str, List[str]]][source]#

Create school classes.

  • This should run after make_school().

  • If attribute already exists as a dict, it is not changed.

  • Attribute is only written if it is set to a string like ‘school1-cls2,school3-cls4’.

make_disabled() str[source]#

Set User.disabled attribute.

make_firstname() str[source]#

Normalize given name if set from import data or create from scheme.

make_lastname() str[source]#

Normalize family name if set from import data or create from scheme.

make_email() str[source]#

Create email from scheme (if not already set).

If any of the other attributes is used in the format scheme of the email address, its make_* function should have run before this!

make_password() str[source]#

Create random password (if not already set).

make_record_uid() str[source]#

Create ucsschoolRecordUID (record_uid) (if not already set).

make_source_uid() str[source]#

Set the ucsschoolSourceUID (source_uid) (if not already set).

make_school() str[source]#

Create ‘school’ attribute - the position of the object in LDAP (if not already set).

Order of detection:

  • already set (object creation or reading from input)

  • from configuration (file or cmdline)

  • first (alphanum-sorted) school in attribute schools

make_schools() List[str][source]#

Create list of schools this user is in. If possible, this should run after make_school()

  • If empty, it is set to self.school.

  • If it is a string like ‘school1,school2,school3’ the attribute is created from it.

make_ucsschool_roles() List[str][source]#
make_udm_property(property_name: str) str | None[source]#

Create self.udm_properties[property_name] from scheme if not already existent.

Parameters:

property_name (str) – name of UDM property

Returns:

value read from CSV or calculated from scheme or None

Return type:

str or None

make_username() str[source]#

Create username if not already set in self.name or self.udm_properties[“username”]. [ALWAYSCOUNTER] and [COUNTER2] are supported, but only one may be used per name.

modify(lo: LoType, validate: bool | None = True, move_if_necessary: bool | None = None) bool[source]#

Modifies an existing UDM instance. Calls pre-hooks. If the object does not exist, returns False. If the object exists, modifies it, returns True and calls post-hooks.

modify_without_hooks(lo: LoType, validate: bool | None = True, move_if_necessary: bool | None = None) bool[source]#
move(lo: LoType, udm_obj: UdmObjectType | None = None, force: bool | None = False) bool[source]#
move_without_hooks(lo, udm_obj, force=False)[source]#
classmethod normalize(s: str) str[source]#

Normalize string (german umlauts etc)

Parameters:

s (str) – str to normalize

Returns:

normalized s

Return type:

str

normalize_udm_properties() None[source]#

Normalize data in self.udm_properties.

reactivate() None[source]#

Reactivate a deactivated user account, reset the account expiry setting and purge timestamp. Run this only on existing users fetched from LDAP.

remove(lo: LoType) bool[source]#

Removes an existing UDM instance. Calls pre-hooks. If the object does not exist, returns False. If the object exists, removes it, returns True and calls post-hooks.

remove_without_hooks(lo: LoType) bool[source]#
validate()[source]#

Runs self-tests in the following order:

  • check existence of mandatory_attributes

  • check uniqueness of record_uid in this import job

  • check uniqueness of username in this import job

  • check uniqueness of email (mailPrimaryAddress) in this import job

  • check that username is not empty

  • check maximum username length

  • check minimum password_length

  • check email has valid format

  • check birthday has valid format

  • check school_classes is a dict

  • check schools is a list

  • check format of entries in school_classes

  • check existence of schools in school and schools

  • check that a username is not already in use by another user

Parameters:
  • lo – LDAP connection object

  • validate_unlikely_changes (bool) – whether to create messages in self.warnings for changes to certain attributes

  • check_username (bool) – if username and password checks should run

  • check_name (bool) – if name checks should run

Returns:

None

Raises:
set_purge_timestamp(ts: str) None[source]#

Set the date at which the account whould be deleted by the ucs-school-purge-expired-users script. Caller must run modify().

Parameters:

ts (str) – account deletion date “%Y-%m-%d” or “”

Returns:

None

property role_sting: str#

Mapping from self.roles to string used in configuration.

Returns:

one of staff, student, teacher, teacher_and_staff

Return type:

str

property school_classes_as_str: str#

Create a string representation of the school_classes attribute.

Returns:

string representation of school_classes attribute

Return type:

str

property unique_email_handler: UsernameHandler#
property username_handler: UsernameHandler#
property username_scheme: str#

Fetch scheme for username for role.

Returns:

scheme for the role from configuration

Return type:

str

solve_format_dependencies(prop_to_format: str, scheme: str, **kwargs: str) None[source]#

Call make_*() methods required to create values for <properties> used in scheme.

Parameters:
  • prop_to_format (str) – name of property for which the scheme is

  • scheme (str) – scheme used to format prop_to_format

  • kwargs (dict) – additional data to use for formatting

Returns:

None

format_from_scheme(prop_name: str, scheme: str, **kwargs: str) str[source]#

Format property with scheme for current import_user. * Uses the replacement code from users:templates. * This does not do the counter variable replacements for username. * Replacement <variables> are filled in the following oder (later additions overwriting previous ones): - from raw input data - from Attributes of self (ImportUser & ucsschool.lib.models.user.User) - from self.udm_properties - from kwargs

Parameters:
  • prop_name (str) – name of property to be formatted

  • scheme (str) – scheme to use

  • kwargs (dict) – additional data to use for formatting

Returns:

formatted string

Return type:

str

classmethod get_class_for_udm_obj(udm_obj: UdmObjectType, school: str) None | Type['ImportUser'][source]#

IMPLEMENTME if you subclass!

get_school_class_objs() List[School][source]#
classmethod attribute_udm_names() Dict[str, str][source]#
to_dict() Dict[str, Any][source]#

Returns a dictionary somewhat representing this instance. This dictionary is usually used when sending the instance to a browser as JSON. By default the attributes are present as well as the dn and the udm_module.

update(other: ImportUser) None[source]#

Copy attributes of other ImportUser into this one.

IMPLEMENTME if you subclass and add attributes that are not ucsschool.lib.models.attributes.

Parameters:

other (ImportUser) – data source

property username_max_length: int#
classmethod school_classes_invalid_character_replacement(school_class: str, char_replacement: str) str[source]#

Replace disallowed characters in school_class with char_replacement. Allowed chars: [string.digits, string.ascii_letters, " -._"]. If char_replacement is empty no replacement will be done.

Parameters:
  • school_class (str) – name of school class

  • char_replacement (str) – character to replace disallowed characters with

Returns:

(possibly modified) name of school class

Return type:

str

logger = <Proxy at 0x7f6d6ffe2e00 wrapping <Logger ucsschool.importer.models.import_user (INFO)> at 0x7f6d6f7a1a10 with factory <function UCSSchoolHelperMetaClass.__new__.<locals>.<lambda>>>#
class ucsschool.importer.models.import_user.ImportStaff(name: str = None, school: str = None, **kwargs: str)[source]#

Bases: ImportUser, Staff

Create ImportUser object (neither saved nor loaded from LDAP yet). The dn attribute is calculated.

Parameters:
  • name (str) – username

  • school (str) – OU

  • kwargs – attributes to set on user object

logger = <Proxy at 0x7f6d6fd92c00 wrapping <Logger ucsschool.importer.models.import_user (INFO)> at 0x7f6d6f7a1a10 with factory <function UCSSchoolHelperMetaClass.__new__.<locals>.<lambda>>>#
class ucsschool.importer.models.import_user.ImportStudent(name: str = None, school: str = None, **kwargs: str)[source]#

Bases: ImportUser, Student

Create ImportUser object (neither saved nor loaded from LDAP yet). The dn attribute is calculated.

Parameters:
  • name (str) – username

  • school (str) – OU

  • kwargs – attributes to set on user object

logger = <Proxy at 0x7f6d6fd91780 wrapping <Logger ucsschool.importer.models.import_user (INFO)> at 0x7f6d6f7a1a10 with factory <function UCSSchoolHelperMetaClass.__new__.<locals>.<lambda>>>#
class ucsschool.importer.models.import_user.ImportTeacher(name: str = None, school: str = None, **kwargs: str)[source]#

Bases: ImportUser, Teacher

Create ImportUser object (neither saved nor loaded from LDAP yet). The dn attribute is calculated.

Parameters:
  • name (str) – username

  • school (str) – OU

  • kwargs – attributes to set on user object

logger = <Proxy at 0x7f6d6fd92000 wrapping <Logger ucsschool.importer.models.import_user (INFO)> at 0x7f6d6f7a1a10 with factory <function UCSSchoolHelperMetaClass.__new__.<locals>.<lambda>>>#
class ucsschool.importer.models.import_user.ImportTeachersAndStaff(name: str = None, school: str = None, **kwargs: str)[source]#

Bases: ImportUser, TeachersAndStaff

Create ImportUser object (neither saved nor loaded from LDAP yet). The dn attribute is calculated.

Parameters:
  • name (str) – username

  • school (str) – OU

  • kwargs – attributes to set on user object

logger = <Proxy at 0x7f6d6fd91b80 wrapping <Logger ucsschool.importer.models.import_user (INFO)> at 0x7f6d6f7a1a10 with factory <function UCSSchoolHelperMetaClass.__new__.<locals>.<lambda>>>#