Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
restructured google sheet; assignments now visible.
  • Loading branch information
chrissdelaney committed Mar 22, 2024
1 parent de12641 commit bdcf741
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 42 deletions.
105 changes: 92 additions & 13 deletions app/spreadsheet_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,107 @@ def get_course_assignments(self, student_email:str, course_id:str) -> list:
self.set_active_document(course_document_id_list[0])

# now, get the assignments from the sheet
assignments_sheet = self.get_sheet("ASSIGNMENT_MASTER")
assignments_records = assignments_sheet.get_all_records()
assignment_sheet = self.get_sheet("ASSIGNMENT_MASTER")
assignments = assignment_sheet.get_all_records()


#get student_grade
gradebook_sheet = self.get_sheet("GRADEBOOK")
all_grades = gradebook_sheet.get_all_records()
student_grades = [g for g in all_grades if g.get("Email") == student_email]

if len(student_grades) == 0:
raise Exception("Student Grades not found in gradebook!")

student_grades = student_grades[0]

#merge student grades in with
for a in assignments:
assignment_name = a.get("NAME")
a['GRADE'] = self.to_pct(student_grades[assignment_name]/a['POINTS'])

return assignments

def get_assignment_details(self, student_email:str, course_id:str, assignment_id:str) -> dict:
if self.doc.id == GOOGLE_SHEETS_MASTER_DOCUMENT_ID:
courses_sheet = self.get_sheet("courses")
courses_records = courses_sheet.get_all_records()

#assign the student email to the sheet that calculates scores...
scores_sheet = self.get_sheet("STUDENT_SCORES")
scores_sheet.update_acell("K2", student_email)
course_document_id_list = [c["SHEETS_DOCUMENT_ID"] for c in courses_records if c["COURSE_ID"] == int(course_id)]

if len(course_document_id_list) == 0:
raise Exception("course not found...")
#TODO: handle within the route
if len(course_document_id_list) > 1:
raise Exception("course duplicate found...error")
#TODO: handle within the route

self.set_active_document(course_document_id_list[0])

#find the assignment scores...
for i in range(len(assignments_records)):
del assignments_records[i]["SHEET_NAME"]
assignments_records[i]["GRADE"] = self.to_pct(scores_sheet.cell(i + 2, 9).value)
assignments_sheet = self.get_sheet("ASSIGNMENT_MASTER")
assignments = assignments_sheet.get_all_records()
assignment_details = [a for a in assignments if a.get('SHEET_NAME') == assignment_id]

#clear the email
scores_sheet.update_acell("K2", "")
if len(assignment_details) == 0:
raise Exception("assignment sheet with id not found!")

return assignments_records
assignment_details = assignment_details[0]


assignment_sheet = self.get_sheet(assignment_id)
assignment_values = assignment_sheet.get_all_values()

#fetch the student data
row = 0
header_row = []
student_row = []
for r in assignment_values:
if "Email Address" in r:
header_row = r
elif student_email in r:
student_row = r
row += 1

if header_row != [] and student_row != []:
break


if header_row == [] or student_row == []:
raise Exception("Error! Header row or student row not found.")

on_time_index = -1
raw_score_index = -1
for i in range(len(header_row)):
if header_row[i] == "ON TIME":
on_time_index = i
elif header_row[i] == "RAW SCORE":
raw_score_index = i

if on_time_index > 0 and raw_score_index > 0:
break

assignments_list = []
i = on_time_index
while i <= raw_score_index:
if i == raw_score_index:
assignment_details['RAW_SCORE'] = self.to_pct(float(student_row[i]))
break
assignments_list.append({
"metric": header_row[i],
"score": student_row[i],
"comments": student_row[i+1],
})
i += 2

assignment_details["DETAILS"] = assignments_list

return assignment_details




if __name__ == "__main__":

ss = SpreadsheetService()

ss.get_course_assignments("[email protected]", "12345")
ss.get_assignment_details("[email protected]", "12345", "groceries-mjr")
2 changes: 1 addition & 1 deletion web_app/routes/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def google_oauth_callback():
#> 'iat': __________,
#> 'exp': __________
#> }
print("USER INFO:", user_info["email"], user_info["name"], user_info["locale"])
print("USER INFO:", user_info["email"], user_info["name"], user_info.get("locale"))

# add user info to the session
session["current_user"] = user_info
Expand Down
23 changes: 20 additions & 3 deletions web_app/routes/courses_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@

courses_routes = Blueprint("courses_routes", __name__)

from web_app.routes.wrappers import student_authenticated_route
from web_app.routes.wrappers import authenticated_route

@courses_routes.route("/courses/<course_id>")
@student_authenticated_route
@authenticated_route
def course(course_id):
print(f"COURSE {course_id}")
ss = current_app.config["SPREADSHEET_SERVICE"]
current_user = session.get("current_user")
email = current_user["email"]
#TODO: change hard coded email to "email" fetched from session
assignments_list = ss.get_course_assignments("[email protected]", course_id)
return render_template("assignments.html", assignments=assignments_list)
return render_template("assignments.html", assignments=assignments_list, course_id=course_id)

@courses_routes.route("/courses/<course_id>/assignments/<assignment_id>")
@authenticated_route
def assignment(course_id, assignment_id):
print(f"COURSE {course_id}: ASSIGNMENT {assignment_id}")
ss = current_app.config["SPREADSHEET_SERVICE"]
current_user = session.get("current_user")
email = current_user["email"]

#TODO: CHANGE THIS ####
email = "[email protected]"
#######################

assignment_details = ss.get_assignment_details(email, course_id, assignment_id)

return render_template("assignment.html", assignment_details=assignment_details)
6 changes: 3 additions & 3 deletions web_app/routes/user_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from flask import Blueprint, render_template, flash, redirect, current_app, url_for, session, request #, jsonify

from web_app.routes.wrappers import student_authenticated_route
from web_app.routes.wrappers import authenticated_route

user_routes = Blueprint("user_routes", __name__)

Expand All @@ -10,7 +10,7 @@
#

@user_routes.route("/user/courses")
@student_authenticated_route
@authenticated_route
def courses():
print("USER COURSES...")
current_user = session.get("current_user")
Expand Down Expand Up @@ -70,7 +70,7 @@ def create_order():
#

@user_routes.route("/user/profile")
@student_authenticated_route
@authenticated_route
def profile():
print("USER PROFILE...")
current_user = session.get("current_user")
Expand Down
2 changes: 1 addition & 1 deletion web_app/routes/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import functools
from flask import session, redirect, flash

def student_authenticated_route(view):
def authenticated_route(view):
"""
Wrap a route with this decorator to prevent unauthenticated access.
Expand Down
41 changes: 41 additions & 0 deletions web_app/templates/assignment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends "bootstrap_5_layout.html" %}
{% set active_page = "assignment" %}

{% block content %}

<h1>Assignment: {{assignment_details.NAME}}</h1>
<h3>Due: {{assignment_details.DUE_DATE}}</h3>
<hr>
<table class="table table-hover text-center">
<thead>
<tr>
<th scope="col">Metric</th>
<th scope="col">Score</th>
<th scope="col">Comments</th>
</tr>
<tbody>
{% for a in assignment_details.DETAILS %}
<tr>
<td>
{{ a.metric }}
</td>
<td>
{{ a.score }}
</td>
<td>
{{ a.comments }}
</td>
</tr>
{% endfor %}
</tbody>
</thead>
</table>
<hr>
<h4>
Score: {{assignment_details.RAW_SCORE}}
</h4>
<p>
Points: {{assignment_details.POINTS}}
</p>

{% endblock %}
48 changes: 29 additions & 19 deletions web_app/templates/assignments.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "bootstrap_5_layout.html" %}
{% set active_page = "courses" %}
{% set active_page = "assignments" %}

{% block content %}

Expand All @@ -13,25 +13,35 @@ <h1>My Assignments</h1>
<th scope="col">Points</th>
<th scope="col">Grade</th>
</tr>
<tbody>
{% for a in assignments %}
<tr>
<td>
{{ a.NAME }}
</td>
<td>
{{ a.DUE_DATE }}
</td>
<td>
{{ a.POINTS }}
</td>
<td>
{{ a.GRADE }}
</td>
</tr>
{% endfor %}
</tbody>
</thead>
<tbody>
{% for a in assignments %}
<tr class="clickable-row" data-href="/courses/{{ course_id }}/assignments/{{ a.SHEET_NAME }}">
<td>
{{ a.NAME }}
</td>
<td>
{{ a.DUE_DATE }}
</td>
<td>
{{ a.POINTS }}
</td>
<td>
{{ a.GRADE }}
</td>
</tr>
{% endfor %}
</tbody>
</table>

<script>
document.addEventListener("DOMContentLoaded", function(){
document.querySelectorAll('.clickable-row').forEach(row => {
row.addEventListener('click', function() {
window.location.href = this.dataset.href;
});
});
});
</script>

{% endblock %}
29 changes: 27 additions & 2 deletions web_app/templates/courses.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,33 @@

{% block content %}

<h1>Courses</h1>
<h1>My Courses</h1>

<p class="lead">This is the courses page. your courses will be appear here.</p>
<table class="table table-hover text-center">
<thead>
<tr>
<th scope="col">Course</th>
<th scope="col">Term</th>
<th scope="col">Professor</th>
</tr>
<tbody>
{% for c in courses %}
<tr>
<a href="/courses/{{c.COURSE_ID}}">
<td>
{{ c.DEPARTMENT }}-{{ c.NUMBER }}: {{ c.COURSE_NAME }}
</td>
</a>
<td>
{{ c.TERM }}
</td>
<td>
{{ c.PROFESSOR }}
</td>
</tr>
{% endfor %}
</tbody>
</thead>
</table>

{% endblock %}

0 comments on commit bdcf741

Please sign in to comment.