lms.cli.courses.groups.memberships.add

Add users to a group.

usage: python3 -m lms.cli.courses.groups.memberships.add [-h] [--version]
                                                         [--server SERVER]
                                                         [--server-type {blackboard,canvas,moodle}]
                                                         [--auth-user AUTH_USER]
                                                         [--auth-password AUTH_PASSWORD]
                                                         [--auth-token AUTH_TOKEN]
                                                         [--course COURSE]
                                                         [--groupset GROUPSET]
                                                         [--group GROUP]
                                                         [--format {json,table,text}]
                                                         [--include-extra-fields]
                                                         [--pretty-headers]
                                                         [--skip-headers]
                                                         [--strict]
                                                         USER_QUERY
                                                         [USER_QUERY ...]

Add users to a group.

positional arguments:
    USER_QUERY          A query for a user to add to the group.

options:
    -h, --help          show this help message and exit
    --version           show program's version number and exit
    --course COURSE     The course to target for this operation.
    --groupset GROUPSET
                        The group set to target for this operation.
    --group GROUP       The group to target for this operation.
    --strict            Enable strict mode, which is stricter about what
                        counts as an error (default: False).

server options:
    --server SERVER     The address of the LMS server to connect to.
    --server-type {blackboard,canvas,moodle}
                        The type of LMS being connected to (this can normally
                        be guessed from the server address).

authentication options:
    --auth-user AUTH_USER
                        The user to authenticate with.
    --auth-password AUTH_PASSWORD
                        The password to authenticate with.
    --auth-token AUTH_TOKEN
                        The token to authenticate with.

output formatting options:
    --format {json,table,text}
                        The format to display the output as (default: text).
    --include-extra-fields
                        Include non-common (usually LMS-specific) fields in
                        results (default: False).
    --pretty-headers    When displaying headers, try to make them look
                        "pretty" (default: False).
    --skip-headers      Skip headers when outputting results, will not apply
                        to all formats (default: False).
 1"""
 2Add users to a group.
 3"""
 4
 5import argparse
 6import sys
 7
 8import lms.backend.instance
 9import lms.cli.common
10import lms.cli.parser
11import lms.model.base
12
13def run_cli(args: argparse.Namespace) -> int:
14    """ Run the CLI. """
15
16    config = args._config_info.application_config
17    backend = lms.backend.instance.get_backend(config)
18
19    course_query = lms.cli.common.check_required_course(backend, config)
20    if (course_query is None):
21        return 1
22
23    groupset_query = lms.cli.common.check_required_groupset(backend, config)
24    if (groupset_query is None):
25        return 2
26
27    group_query = lms.cli.common.check_required_group(backend, config)
28    if (group_query is None):
29        return 3
30
31    user_queries = backend.parse_user_queries(args.users)
32
33    count = backend.courses_groups_memberships_resolve_and_add(course_query, groupset_query, group_query, user_queries)
34
35    print(f"Added {count} users to group {group_query}.")
36
37    return lms.cli.common.strict_check(config, (count != len(user_queries)),
38        f"Expected to add {len(user_queries)} memberships to group, but added {count}.", 4)
39
40def main() -> int:
41    """ Get a parser, parse the args, and call run. """
42    return run_cli(_get_parser().parse_args())
43
44def _get_parser() -> argparse.ArgumentParser:
45    """ Get the parser. """
46
47    parser = lms.cli.parser.get_parser(__doc__.strip(),
48            include_output_format = True,
49            include_course = True,
50            include_groupset = True,
51            include_group = True,
52            include_strict = True,
53    )
54
55    parser.add_argument('users', metavar = 'USER_QUERY',
56        type = str, nargs = '+',
57        help = 'A query for a user to add to the group.')
58
59    return parser
60
61if (__name__ == '__main__'):
62    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
14def run_cli(args: argparse.Namespace) -> int:
15    """ Run the CLI. """
16
17    config = args._config_info.application_config
18    backend = lms.backend.instance.get_backend(config)
19
20    course_query = lms.cli.common.check_required_course(backend, config)
21    if (course_query is None):
22        return 1
23
24    groupset_query = lms.cli.common.check_required_groupset(backend, config)
25    if (groupset_query is None):
26        return 2
27
28    group_query = lms.cli.common.check_required_group(backend, config)
29    if (group_query is None):
30        return 3
31
32    user_queries = backend.parse_user_queries(args.users)
33
34    count = backend.courses_groups_memberships_resolve_and_add(course_query, groupset_query, group_query, user_queries)
35
36    print(f"Added {count} users to group {group_query}.")
37
38    return lms.cli.common.strict_check(config, (count != len(user_queries)),
39        f"Expected to add {len(user_queries)} memberships to group, but added {count}.", 4)

Run the CLI.

def main() -> int:
41def main() -> int:
42    """ Get a parser, parse the args, and call run. """
43    return run_cli(_get_parser().parse_args())

Get a parser, parse the args, and call run.