From 9735201c2add2f32760c7e3820a3803170d31ef6 Mon Sep 17 00:00:00 2001 From: Danyal-Faheem Date: Wed, 15 Jan 2025 20:19:27 +0500 Subject: [PATCH] refactor: move utility methods to jobs_utils --- tutor/commands/jobs_utils.py | 119 +++++++++++++++-------------------- 1 file changed, 52 insertions(+), 67 deletions(-) diff --git a/tutor/commands/jobs_utils.py b/tutor/commands/jobs_utils.py index fbeca4e350..c1d0371f4c 100644 --- a/tutor/commands/jobs_utils.py +++ b/tutor/commands/jobs_utils.py @@ -2,14 +2,30 @@ This module provides utility methods for tutor `do` commands Methods: +- `create_user_template`: Generates the necessary commands to create a user in openedx. - `get_mysql_change_charset_query`: Generates MySQL queries to upgrade the charset and collation of columns, tables, and databases. -- `get_mysql_change_authentication_plugin_query`: Generates MySQL queries to update the authentication plugin for MySQL users. +- `set_theme_template`: Generates the necessary commands to set a theme on a specific domain in openedx. """ -from typing import Sequence +from __future__ import annotations -from tutor import fmt -from tutor.types import Config, ConfigValue + +def create_user_template( + superuser: str, staff: bool, username: str, email: str, password: str +) -> str: + opts = "" + if superuser: + opts += " --superuser" + if staff: + opts += " --staff" + return f""" +./manage.py lms manage_user {opts} {username} {email} +./manage.py lms shell -c " +from django.contrib.auth import get_user_model +u = get_user_model().objects.get(username='{username}') +u.set_password('{password}') +u.save()" +""" def get_mysql_change_charset_query( @@ -138,69 +154,38 @@ def get_mysql_change_charset_query( """ -def get_mysql_change_authentication_plugin_query( - config: Config, users: Sequence[str], all_users: bool -) -> str: +def set_theme_template(theme_name: str, domain_names: list[str]) -> str: """ - Generates MySQL queries to update the authentication plugin for MySQL users. - - This method constructs queries to change the authentication plugin to - `caching_sha2_password`. User credentials must be provided in the tutor - configuration under the keys `_MYSQL_USERNAME` and `_MYSQL_PASSWORD`. - - Args: - config (Config): Tutor configuration object - users (List[str]): List of specific MySQL users to update. - all_users (bool): Flag indicating whether to include ROOT and OPENEDX users - in addition to those specified in the `users` list. - - Returns: - str: A string containing the SQL queries to execute. - - Raises: - TutorError: If any user in the `users` list does not have corresponding - username or password entries in the configuration. + For each domain, get or create a Site object and assign the selected theme. """ - - host = "%" - query = "" - - def generate_mysql_authentication_plugin_update_query( - username: ConfigValue, password: ConfigValue, host: str - ) -> str: - fmt.echo_info( - f"Authentication plugin of user {username} will be updated to caching_sha2_password" - ) - return f"ALTER USER IF EXISTS '{username}'@'{host}' IDENTIFIED with caching_sha2_password BY '{password}';" - - def generate_user_queries(users: Sequence[str]) -> str: - query = "" - for user in users: - user_uppercase = user.upper() - if not ( - f"{user_uppercase}_MYSQL_USERNAME" in config - and f"{user_uppercase}_MYSQL_PASSWORD" in config - ): - fmt.echo_alert( - f"Username or Password for User {user} not found in config. Skipping update process for User {user}." - ) - continue - - query += generate_mysql_authentication_plugin_update_query( - config[f"{user_uppercase}_MYSQL_USERNAME"], - config[f"{user_uppercase}_MYSQL_PASSWORD"], - host, + # Note that there are no double quotes " in this piece of code + python_command = """ +import sys +from django.contrib.sites.models import Site +def assign_theme(name, domain): + print('Assigning theme', name, 'to', domain) + if len(domain) > 50: + sys.stderr.write( + 'Assigning a theme to a site with a long (> 50 characters) domain name.' + ' The displayed site name will be truncated to 50 characters.\\n' ) - return query - - if not all_users: - return generate_user_queries(users) - - query += generate_mysql_authentication_plugin_update_query( - config["MYSQL_ROOT_USERNAME"], config["MYSQL_ROOT_PASSWORD"], host - ) - query += generate_mysql_authentication_plugin_update_query( - config["OPENEDX_MYSQL_USERNAME"], config["OPENEDX_MYSQL_PASSWORD"], host - ) - - return query + generate_user_queries(users) + site, _ = Site.objects.get_or_create(domain=domain) + if not site.name: + name_max_length = Site._meta.get_field('name').max_length + site.name = domain[:name_max_length] + site.save() + site.themes.all().delete() + if name != 'default': + site.themes.create(theme_dir_name=name) +""" + domain_names = domain_names or [ + "{{ LMS_HOST }}", + "{{ LMS_HOST }}:8000", + "{{ CMS_HOST }}", + "{{ CMS_HOST }}:8001", + "{{ PREVIEW_LMS_HOST }}", + "{{ PREVIEW_LMS_HOST }}:8000", + ] + for domain_name in domain_names: + python_command += f"assign_theme('{theme_name}', '{domain_name}')\n" + return f'./manage.py lms shell -c "{python_command}"'