-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_courses.py
More file actions
63 lines (46 loc) · 1.74 KB
/
Copy pathdiff_courses.py
File metadata and controls
63 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Takes:
- a top-level category id
- a file preprocessed by preprocess_teachers_and_courses.py
compares the list of courses found in moodle vs the list of courses in the file
Uses the Moodle API
"""
import argparse
import polars as pl
import structlog
from lib.config import get_moodle_client
from lib.moodle_api import MoodleClient
from preprocess_teachers_and_courses import COURSE_SHORTNAME
log = structlog.get_logger()
def diff_courses(moodle: MoodleClient, course_category_id: str, src: pl.DataFrame):
categories = moodle(
"core_course_get_categories",
criteria=[{"key": "id", "value": course_category_id}],
)
categories.sort(key=lambda x: x.id)
existing_courses = []
for category in categories:
log.debug(
"collecting courses",
category=category.name,
course_count=category.coursecount,
)
courses_in_category = moodle(
"core_course_get_courses_by_field", field="category", value=category.id
)
existing_courses.extend(courses_in_category.courses)
wanted = set(src[COURSE_SHORTNAME])
existing = {c.shortname for c in existing_courses}
# We just display these, in case the user wants to remove them
extra = sorted(existing - wanted)
log.info("in moodle but not in file", courses=extra)
missing = sorted(wanted - existing)
log.info("in file but not in moodle", courses=missing)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("course_category_id")
parser.add_argument("preprocessed")
args = parser.parse_args()
preprocessed = pl.read_csv(args.preprocessed)
moodle = get_moodle_client()
diff_courses(moodle, args.course_category_id, preprocessed)