Merge pull request #2 from alex-bellon/master

Added error handling when getting submissions
This commit is contained in:
David Katsandres 2021-03-18 16:57:03 -07:00 committed by GitHub
commit 577327f31a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 94 additions and 87 deletions

181
export.py
View File

@ -330,102 +330,109 @@ def findCourseAssignments(course):
# Get all assignments # Get all assignments
assignments = course.get_assignments() assignments = course.get_assignments()
for assignment in assignments: try:
# Create a new assignment view for assignment in assignments:
assignment_view = assignmentView() # Create a new assignment view
assignment_view = assignmentView()
# Title # Title
if hasattr(assignment, "name"): if hasattr(assignment, "name"):
assignment_view.title = str(assignment.name) assignment_view.title = str(assignment.name)
else: else:
assignment_view.title = "" assignment_view.title = ""
# Description # Description
if hasattr(assignment, "description"): if hasattr(assignment, "description"):
assignment_view.description = str(assignment.description) assignment_view.description = str(assignment.description)
else: else:
assignment_view.description = "" assignment_view.description = ""
# Assigned date # Assigned date
if hasattr(assignment, "created_at_date"): if hasattr(assignment, "created_at_date"):
assignment_view.assigned_date = assignment.created_at_date.strftime(DATE_TEMPLATE) assignment_view.assigned_date = assignment.created_at_date.strftime(DATE_TEMPLATE)
else: else:
assignment_view.assigned_date = "" assignment_view.assigned_date = ""
# Due date # Due date
if hasattr(assignment, "due_at_date"): if hasattr(assignment, "due_at_date"):
assignment_view.due_date = assignment.due_at_date.strftime(DATE_TEMPLATE) assignment_view.due_date = assignment.due_at_date.strftime(DATE_TEMPLATE)
else: else:
assignment_view.due_date = "" assignment_view.due_date = ""
# Download all submissions # Download all submissions
try: try:
submissions = assignment.get_submissions() submissions = assignment.get_submissions()
# TODO : Figure out the exact error raised # TODO : Figure out the exact error raised
except: except:
print("Got no submissions for this assignment") print("Got no submissions for this assignment")
else: else:
for submission in submissions: try:
for submission in submissions:
sub_view = submissionView() sub_view = submissionView()
# My grade
if hasattr(submission, "grade"):
sub_view.grade = str(submission.grade)
else:
sub_view.grade = ""
# My raw score
if hasattr(submission, "score"):
sub_view.raw_score = str(submission.score)
else:
sub_view.raw_score = ""
# Total possible score
if hasattr(assignment, "points_possible"):
sub_view.total_possible_points = str(assignment.points_possible)
else:
sub_view.total_possible_points = ""
# Submission comments
if hasattr(submission, "submission_comments"):
sub_view.submission_comments = str(submission.submission_comments)
else:
sub_view.submission_comments = ""
if hasattr(submission, "user_id"):
sub_view.user_id = str(submission.user_id)
else:
sub_view.user_id = "no-id"
try:
submission.attachments
except AttributeError:
print('No attachments')
else:
for attachment in submission.attachments:
attach_view = attachmentView()
attach_view.url = attachment["url"]
attach_view.id = attachment["id"]
attach_view.filename = attachment["filename"]
sub_view.attachments.append(attach_view)
assignment_view.submissions.append(sub_view)
except Exception as e:
print("Skipping submission that gave the following error:")
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 # My grade
if hasattr(submission, "grade"): assignment_view.submission.grade = str(submission.grade) if hasattr(submission, "grade") else ""
sub_view.grade = str(submission.grade)
else:
sub_view.grade = ""
# My raw score # My raw score
if hasattr(submission, "score"): assignment_view.submission.raw_score = str(submission.score) if hasattr(submission, "score") else ""
sub_view.raw_score = str(submission.score)
else:
sub_view.raw_score = ""
# Total possible score # Total possible score
if hasattr(assignment, "points_possible"): assignment_view.submission.total_possible_points = str(assignment.points_possible) if hasattr(assignment, "points_possible") else ""
sub_view.total_possible_points = str(assignment.points_possible)
else:
sub_view.total_possible_points = ""
# Submission comments # Submission comments
if hasattr(submission, "submission_comments"): assignment_view.submission.submission_comments = str(submission.submission_comments) if hasattr(submission, "submission_comments") else ""
sub_view.submission_comments = str(submission.submission_comments)
else:
sub_view.submission_comments = ""
if hasattr(submission, "user_id"):
sub_view.user_id = str(submission.user_id)
else:
sub_view.user_id = "no-id"
try:
submission.attachments
except AttributeError:
print('No attachments')
else:
for attachment in submission.attachments:
attach_view = attachmentView()
attach_view.url = attachment["url"]
attach_view.id = attachment["id"]
attach_view.filename = attachment["filename"]
sub_view.attachments.append(attach_view)
assignment_view.submissions.append(sub_view)
# 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:
print("Skipping course assignments that gave the following error:")
print(e)
return assignment_views return assignment_views