• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uw-it-aca / canvas-sis-provisioner / 4076774980

pending completion
4076774980

Pull #755

github

GitHub
Merge b8f23cfb7 into 91bd59a7f
Pull Request #755: add job to backfill courses

46 of 46 new or added lines in 2 files covered. (100.0%)

4284 of 7706 relevant lines covered (55.59%)

0.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sis_provisioner/management/commands/backfill_courses.py
1
# Copyright 2023 UW-IT, University of Washington
2
# SPDX-License-Identifier: Apache-2.0
3

4

5
from django.core.management.base import BaseCommand, CommandError
×
6
from django.core.files.storage import default_storage
×
7
from django.utils.timezone import utc
×
8
from sis_provisioner.dao.canvas import (
×
9
    adhoc_course_sis_id, update_course_sis_id)
10
from sis_provisioner.models.course import Course
×
11
from uw_canvas.terms import Terms
×
12
from dateutil.parser import parse
×
13
from logging import getLogger
×
14
import csv
×
15

16
logger = getLogger(__name__)
×
17

18

19
class Command(BaseCommand):
×
20
    help = "Insert courses from file."
×
21

22
    def add_arguments(self, parser):
×
23
        parser.add_argument(
×
24
            'course_file', help='Course file path')
25

26
    def get_all_terms(self):
×
27
        terms = {}
×
28
        for term in Terms().get_all_terms():
×
29
            # print('Term id: "{}", sis_id: "{}", name: "{}"'.format(
30
            #     term.term_id, term.sis_term_id, term.name))
31
            terms[term.term_id] = term.sis_term_id or 'default'
×
32
        return terms
×
33

34
    def handle(self, *args, **options):
×
35
        course_file = options.get('course_file')
×
36
        terms = self.get_all_terms()
×
37

38
        with default_storage.open(course_file, mode='r') as csvfile:
×
39
            reader = csv.reader(csvfile)
×
40
            for row in reader:
×
41
                canvas_id = row[1]
×
42
                term_id = row[4]
×
43
                created_at = parse(row[8]).replace(tzinfo=utc)
×
44
                sis_source_id = row[13]
×
45
                workflow_state = row[14]
×
46
                term_sis_id = terms.get(term_id)
×
47

48
                if not sis_source_id:
×
49
                    sis_source_id = adhoc_course_sis_id(canvas_id)
×
50
                    try:
×
51
                        update_course_sis_id(canvas_id, sis_source_id)
×
52
                    except DataFailureException as ex:
×
53
                        logger.info('Add sis_id for course {}: {}'.format(
×
54
                            canvas_id, ex))
55
                        continue
×
56

57
                try:
×
58
                    course = Course.objects.get(course_id=sis_source_id)
×
59
                    course.created_date = created_at
×
60
                except Course.DoesNotExist:
×
61
                    course = Course(
×
62
                        course_id=sis_source_id,
63
                        canvas_course_id=canvas_id,
64
                        course_type=Course.ADHOC_TYPE,
65
                        term_id=terms.get(term_id),
66
                        created_date=created_at,
67
                        priority=Course.PRIORITY_NONE)
68

69
                course.save()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc