Skip to content

Commit

Permalink
add subject and all_courses route
Browse files Browse the repository at this point in the history
  • Loading branch information
Highfire1 committed Jun 20, 2024
1 parent 8c3f725 commit 0977687
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
69 changes: 69 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<department>', 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/<department>/<course_number>')
Expand Down
93 changes: 93 additions & 0 deletions templates/all_courses.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{subject}}</title>
<meta content="All courses for {{subject}}." property="og:title" />
<meta content="Information about courses in {{subject}}." property="og:description" />
<style>
body {
background: white;
margin-left:20px;
margin-right:20px;
font-size: 1.2em;
}
h2 {margin-bottom: 2px;}
.grid {max-width:90vw; display: grid; grid: auto auto/ fit-content(100%) fit-content(100%);}
.sched {max-width:90vw; display: grid; grid: auto / repeat(7, fit-content(100%));}
div > * {border: 1px solid #000; padding-right: 5px; padding-left: 5px; padding-top:2px; padding-bottom:2px; margin: 0;}
iframe {width:90vw; height: 300px; padding: 10px;}

.red {background-color: rgb(253, 167, 167);}
.yellow {background-color: rgb(241, 241, 162);}
.green {background-color: rgb(126, 198, 126);}
table {
border-collapse: collapse;
}
table > td, tbody > * {
vertical-align: top;
text-align: left;
}
th, td {
border: 1px solid #000;
text-align: left;
padding: 5px;
}

#transferTable {
width: fit-content;
table-layout: fixed;
}
.tablePriority {
min-width: max-content;
}

.offeredTable {
width: fit-content;
}

.hidden {
display: None
}

</style>
</head>
<body>

<h1>All Courses</h1>
<p>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).</p>

{% for subject in subjects %}
<h2>{{subject}}</h2>

{% if subjects[subject][0] %}
<h3>Active courses:</h3>
{% else%}
<p>No active courses.</p>
{% endif %}

{% for course in subjects[subject][0] %}
<p><a href="/course/{{course.subject}}/{{course.course_code}}">{{course.subject}} {{course.course_code}}: {{course.title}}</a></p>
{% endfor %}

{% if subjects[subject][1] %}
<h3>Discontinued courses:</h3>
{% else%}
<p>No discontinued courses.</p>
{% endif %}

{% for course in subjects[subject][1] %}
<p><a href="/course/{{course.subject}}/{{course.course_code}}">{{course.subject}} {{course.course_code}}
{% if course.title %}
: {{course.title}}
{% endif %}
</a></p>
{% endfor %}

{% endfor %}

</body>
</html>
75 changes: 75 additions & 0 deletions templates/subject.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{subject}}</title>
<meta content="All courses for {{subject}}." property="og:title" />
<meta content="Information about courses in {{subject}}." property="og:description" />
<style>
body {
background: white;
margin-left:20px;
margin-right:20px;
font-size: 1.2em;
}
h2 {margin-bottom: 2px;}
.grid {max-width:90vw; display: grid; grid: auto auto/ fit-content(100%) fit-content(100%);}
.sched {max-width:90vw; display: grid; grid: auto / repeat(7, fit-content(100%));}
div > * {border: 1px solid #000; padding-right: 5px; padding-left: 5px; padding-top:2px; padding-bottom:2px; margin: 0;}
iframe {width:90vw; height: 300px; padding: 10px;}

.red {background-color: rgb(253, 167, 167);}
.yellow {background-color: rgb(241, 241, 162);}
.green {background-color: rgb(126, 198, 126);}
table {
border-collapse: collapse;
}
table > td, tbody > * {
vertical-align: top;
text-align: left;
}
th, td {
border: 1px solid #000;
text-align: left;
padding: 5px;
}

#transferTable {
width: fit-content;
table-layout: fixed;
}
.tablePriority {
min-width: max-content;
}

.offeredTable {
width: fit-content;
}

.hidden {
display: None
}

</style>
</head>
<body>

<h1>{{subject}}</h1>

<h2>Courses:</h2>
{% for course in active_courses %}
<p><a href="/course/{{course.subject}}/{{course.course_code}}">{{course.subject}} {{course.course_code}}: {{course.title}}</a></p>
{% endfor %}

<h2>Discontinued courses:</h2>
{% for course in inactive_courses %}
<p><a href="/course/{{course.subject}}/{{course.course_code}}">{{course.subject}} {{course.course_code}}
{% if course.title %}
: {{course.title}}
{% endif %}
</a></p>
{% endfor %}

</body>
</html>

0 comments on commit 0977687

Please sign in to comment.