Exemplo n.º 1
0
 def handle(self, program_uuid, user_items, *args, **options):
     try:
         parsed_program_uuid = UUID(program_uuid)
     except ValueError:
         raise CommandError(
             "supplied program_uuid '{}' is not a valid UUID")
     ext_keys_to_usernames = self.parse_user_items(user_items)
     try:
         link_program_enrollments(parsed_program_uuid,
                                  ext_keys_to_usernames)
     except Exception as e:
         raise CommandError(str(e))
Exemplo n.º 2
0
def validate_and_link_program_enrollments(program_uuid_string, linkage_text):
    """
    Validate arguments, and if valid, call `link_program_enrollments`.

    Returns: (successes, errors)
        where successes and errors are both list[str]
    """
    if not (program_uuid_string and linkage_text):
        error = (
            "You must provide both a program uuid "
            "and a series of lines with the format "
            "'external_user_key,lms_username'."
        )
        return [], [error]
    try:
        program_uuid = UUID(program_uuid_string)
    except ValueError:
        return [], [
            f"Supplied program UUID '{program_uuid_string}' is not a valid UUID."
        ]
    reader = csv.DictReader(
        linkage_text.splitlines(), fieldnames=('external_key', 'username')
    )
    ext_key_to_username = {
        (item.get('external_key') or '').strip(): (item['username'] or '').strip()
        for item in reader
    }
    if not (all(ext_key_to_username.keys()) and all(ext_key_to_username.values())):  # lint-amnesty, pylint: disable=consider-iterating-dictionary
        return [], [
            "All linking lines must be in the format 'external_user_key,lms_username'"
        ]
    link_errors = link_program_enrollments(
        program_uuid, ext_key_to_username
    )
    successes = [
        str(item)
        for item in ext_key_to_username.items()
        if item[0] not in link_errors.keys()  # lint-amnesty, pylint: disable=consider-iterating-dictionary
    ]
    errors = [message for message in link_errors.values()]  # lint-amnesty, pylint: disable=unnecessary-comprehension
    return successes, errors