|
| 1 | +# Copyright 2026 UW-IT, University of Washington |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from django.core.management.base import BaseCommand |
| 5 | +from django.conf import settings |
| 6 | +from uw_canvas.reports import Reports |
| 7 | +from uw_canvas.courses import Courses, COURSES_API |
| 8 | +from restclients_core.exceptions import DataFailureException |
| 9 | +from sis_provisioner.dao.course import valid_academic_course_sis_id |
| 10 | +from sis_provisioner.exceptions import CoursePolicyException |
| 11 | +from logging import getLogger |
| 12 | +import csv |
| 13 | +import os |
| 14 | + |
| 15 | +logger = getLogger(__name__) |
| 16 | +course_client = Courses() |
| 17 | +pretext = "ARCHIVED: " |
| 18 | + |
| 19 | + |
| 20 | +class Command(BaseCommand): |
| 21 | + help = ("Updates all SIS course titles.") |
| 22 | + |
| 23 | + def add_arguments(self, parser): |
| 24 | + parser.add_argument("term_sis_id", help="Term SIS ID") |
| 25 | + |
| 26 | + def update_course_title(self, course_id, name, course_code): |
| 27 | + url = COURSES_API.format(course_id) |
| 28 | + body = {"course": {"name": name, "course_code": course_code}} |
| 29 | + return course_client._put_resource(url, body) |
| 30 | + |
| 31 | + def handle(self, *args, **options): |
| 32 | + term_sis_id = options.get("term_sis_id") |
| 33 | + |
| 34 | + report_client = Reports() |
| 35 | + |
| 36 | + term = report_client.get_term_by_sis_id(term_sis_id) |
| 37 | + |
| 38 | + course_report = report_client.create_course_provisioning_report( |
| 39 | + settings.RESTCLIENTS_CANVAS_ACCOUNT_ID, term_id=term.term_id) |
| 40 | + |
| 41 | + course_data = report_client.get_report_data(course_report) |
| 42 | + course_csv_data = csv.reader(course_data) |
| 43 | + |
| 44 | + header = next(course_csv_data) |
| 45 | + canvas_course_id_idx = header.index("canvas_course_id") |
| 46 | + sis_course_id_idx = header.index("course_id") |
| 47 | + long_name_idx = header.index("long_name") |
| 48 | + short_name_idx = header.index("short_name") |
| 49 | + |
| 50 | + course_client = Courses() |
| 51 | + for row in course_csv_data: |
| 52 | + if not len(row): |
| 53 | + continue |
| 54 | + |
| 55 | + sis_course_id = row[sis_course_id_idx] |
| 56 | + try: |
| 57 | + valid_academic_course_sis_id(sis_course_id) |
| 58 | + except CoursePolicyException: |
| 59 | + continue |
| 60 | + |
| 61 | + course_id = row[canvas_course_id_idx] |
| 62 | + long_name = row[long_name_idx].removeprefix(pretext) |
| 63 | + short_name = row[short_name_idx].removeprefix(pretext) |
| 64 | + new_long_name = f"{pretext}{long_name}" |
| 65 | + new_short_name = f"{pretext}{short_name}" |
| 66 | + |
| 67 | + try: |
| 68 | + data = self.update_course_title( |
| 69 | + course_id, new_long_name, new_short_name) |
| 70 | + print(data) |
| 71 | + except DataFailureException as ex: |
| 72 | + print(ex) |
| 73 | + |
| 74 | + report_client.delete_report(course_report) |
0 commit comments