Handle student-mode submissions and teacher-mode submissions the same

Treat submissions from the list of submissions from every student the same as submissions from a single individual user (student) so they are downloaded the same and processed. Before, it seems like it just didn't care to download submission attachments for single-student-account-only individual submissions. Now, handle both types the same. This means that there are a lot of single-element lists of submissions in the final output if the user is a student, but it makes the code simpler. Could clean up those lists later.

Also, display the different types of errors in fetching submissions.
This commit is contained in:
17acres 2021-05-06 02:19:01 -04:00
parent 2040415d1a
commit ad174fcbb5
1 changed files with 17 additions and 29 deletions

View File

@ -6,6 +6,7 @@ import string
# external # external
from canvasapi import Canvas from canvasapi import Canvas
from canvasapi.exceptions import ResourceDoesNotExist from canvasapi.exceptions import ResourceDoesNotExist
from canvasapi.exceptions import Unauthorized
import dateutil.parser import dateutil.parser
import jsonpickle import jsonpickle
import requests import requests
@ -116,11 +117,9 @@ class assignmentView():
description = "" description = ""
assigned_date = "" assigned_date = ""
due_date = "" due_date = ""
submission = None
submissions = [] submissions = []
def __init__(self): def __init__(self):
self.submission = submissionView()
self.submissions = [] self.submissions = []
@ -376,12 +375,20 @@ def findCourseAssignments(course):
else: else:
assignment_view.due_date = "" assignment_view.due_date = ""
# Download all submissions
try: try:
try: # Download all submissions for entire class
submissions = assignment.get_submissions() submissions = assignment.get_submissions()
# TODO : Figure out the exact error raised submissions[0] # Trigger Unauthorized if not allowed
except: except Unauthorized:
print("Got no submissions for this assignment") print("Not authorized to download entire class submissions for this assignment")
# Download submission for this user only
submissions = [assignment.get_submission(USER_ID)]
submissions[0] #throw error if no submissions found at all but without error
except (ResourceDoesNotExist, NameError, IndexError):
print('Got no submissions from either class or user: {}'.format(USER_ID))
except Exception as e:
print("Failed to retrieve submissions for this assignment")
print(e.__class__.__name__)
else: else:
try: try:
for submission in submissions: for submission in submissions:
@ -430,25 +437,6 @@ def findCourseAssignments(course):
print("Skipping submission that gave the following error:") print("Skipping submission that gave the following error:")
print(e) print(e)
# The following is only useful if you are a student in the class.
# Get my user"s submission object
try:
submission = assignment.get_submission(USER_ID)
except ResourceDoesNotExist:
print('No submission for user: {}'.format(USER_ID))
else:
# Create a new submission view
assignment_view.submission = submissionView()
# My grade
assignment_view.submission.grade = str(submission.grade) if hasattr(submission, "grade") else ""
# My raw score
assignment_view.submission.raw_score = str(submission.score) if hasattr(submission, "score") else ""
# Total possible score
assignment_view.submission.total_possible_points = str(assignment.points_possible) if hasattr(assignment, "points_possible") else ""
# Submission comments
assignment_view.submission.submission_comments = str(submission.submission_comments) if hasattr(submission, "submission_comments") else ""
assignment_views.append(assignment_view) assignment_views.append(assignment_view)
except Exception as e: except Exception as e:
print("Skipping course assignments that gave the following error:") print("Skipping course assignments that gave the following error:")
@ -570,11 +558,11 @@ def getCourseView(course):
# Course announcements # Course announcements
print(" Getting announcements") print(" Getting announcements")
course_view.announcements = findCourseAnnouncements(course) #course_view.announcements = findCourseAnnouncements(course)
# Course discussions # Course discussions
print(" Getting discussions") print(" Getting discussions")
course_view.discussions = findCourseDiscussions(course) #course_view.discussions = findCourseDiscussions(course)
# Course pages # Course pages
print(" Getting pages") print(" Getting pages")
@ -649,7 +637,7 @@ if __name__ == "__main__":
all_courses_views.append(course_view) all_courses_views.append(course_view)
print(" Downloading all files") print(" Downloading all files")
downloadCourseFiles(course, course_view) #downloadCourseFiles(course, course_view)
print(" Downloading submission attachments") print(" Downloading submission attachments")
download_submission_attachments(course, course_view) download_submission_attachments(course, course_view)