From 09776879192476ac0ac4b3940dfe907b757e09f8 Mon Sep 17 00:00:00 2001 From: Anderson T Date: Thu, 20 Jun 2024 05:25:46 -0700 Subject: [PATCH] add subject and all_courses route --- main.py | 69 ++++++++++++++++++++++++++++ templates/all_courses.html | 93 ++++++++++++++++++++++++++++++++++++++ templates/subject.html | 75 ++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 templates/all_courses.html create mode 100644 templates/subject.html diff --git a/main.py b/main.py index 0711b63..293e250 100644 --- a/main.py +++ b/main.py @@ -40,6 +40,75 @@ def format_attribute(value): return "no" return value +@app.route('/course') +def all_courses(): + api_url = f"https://coursesapi.langaracs.ca/index/courses" + response = requests.get(api_url) + if response.status_code != 200: + return jsonify({'error': 'Failed to fetch data'}), response.status_code + + data = response.json() + + subjects = {} + active_c = 0 + active_s = 0 + + for course in data['courses']: + if course['subject'] not in subjects: + subjects[course['subject']] = ([], []) + + if course["active"]: + subjects[course['subject']][0].append(course) + active_c+=1 + else: + subjects[course['subject']][1].append(course) + + for s in subjects: + if len(subjects[s][0]) > 0: + active_s+=1 + + return render_template('all_courses.html', + count_s=len(subjects), + count_s_active=active_s, + count_s_inactive=len(subjects)-active_s, + count_c=len(data['courses']), + count_c_active=active_c, + count_c_inactive=len(data['courses'])-active_c, + subject="All Courses", + subjects=subjects) + + +# route for getting all courses for a subject +@app.route('/course/', strict_slashes=False) +def subject(department:str): + api_url = f"https://coursesapi.langaracs.ca/index/courses" + response = requests.get(api_url) + if response.status_code != 200: + return jsonify({'error': 'Failed to fetch data'}), response.status_code + + data = response.json() + + subject_courses:list[dict] = [] + + department=department.upper() + for c in data["courses"]: + if c['subject'] == department: + subject_courses.append(c) + + if len(subject_courses) == 0: + return jsonify({'error': 'Nothing found for that subject.'}), 404 + + active_courses = [] + inactive_courses = [] + + for c in subject_courses: + if c["active"]: + active_courses.append(c) + else: + inactive_courses.append(c) + + return render_template('subject.html', subject=department, active_courses=active_courses, inactive_courses=inactive_courses) + # route for getting specific course @app.route('/course//') diff --git a/templates/all_courses.html b/templates/all_courses.html new file mode 100644 index 0000000..94a51c7 --- /dev/null +++ b/templates/all_courses.html @@ -0,0 +1,93 @@ + + + + + + {{subject}} + + + + + + +

All Courses

+

Found {{count_s}} subjects ({{count_s_active}} active and {{count_s_inactive}} discontinued) + and + {{count_c}} courses ({{count_c_active}} active and {{count_c_inactive}} discontinued).

+ + {% for subject in subjects %} +

{{subject}}

+ + {% if subjects[subject][0] %} +

Active courses:

+ {% else%} +

No active courses.

+ {% endif %} + + {% for course in subjects[subject][0] %} +

{{course.subject}} {{course.course_code}}: {{course.title}}

+ {% endfor %} + + {% if subjects[subject][1] %} +

Discontinued courses:

+ {% else%} +

No discontinued courses.

+ {% endif %} + + {% for course in subjects[subject][1] %} +

{{course.subject}} {{course.course_code}} + {% if course.title %} + : {{course.title}} + {% endif %} +

+ {% endfor %} + + {% endfor %} + + + diff --git a/templates/subject.html b/templates/subject.html new file mode 100644 index 0000000..6a497ac --- /dev/null +++ b/templates/subject.html @@ -0,0 +1,75 @@ + + + + + + {{subject}} + + + + + + +

{{subject}}

+ +

Courses:

+ {% for course in active_courses %} +

{{course.subject}} {{course.course_code}}: {{course.title}}

+ {% endfor %} + +

Discontinued courses:

+ {% for course in inactive_courses %} +

{{course.subject}} {{course.course_code}} + {% if course.title %} + : {{course.title}} + {% endif %} +

+ {% endfor %} + + +