Skip to content

Commit

Permalink
Merge branch 'release/0.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
scsanad committed Nov 16, 2020
2 parents 7fc197f + b62a35d commit a016959
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
14 changes: 10 additions & 4 deletions canvas_client/canvas_api.py
Expand Up @@ -4,7 +4,7 @@
import sys

import canvas_client.util
from canvas_client.submission import Submission, SubmissionAttempt, SubmissionComment
from canvas_client.submission import Submission, SubmissionAttempt, SubmissionComment, SubmissionAttachment
from canvas_client.section import Section

class Parser:
Expand Down Expand Up @@ -56,13 +56,19 @@ def submission_attempt_from_dict(attempt_dict):
try:
submitted_at = datetime.strptime( attempt_dict['submitted_at'],
'%Y-%m-%dT%H:%M:%SZ')

attachments = []

for attachment in attempt_dict['attachments']:
attachments.append(
SubmissionAttachment( attachment_name=attachment['display_name'],
attachment_url=attachment['url']))

submission_attempt = SubmissionAttempt( nr=attempt_dict['attempt'],
late=attempt_dict['late'],
submitted_at=submitted_at.strftime("%Y-%m-%d %H:%M:%S"),
seconds_late=attempt_dict['seconds_late'],
attachment_name=attempt_dict['attachments'][0]['display_name'],
attachment_url=attempt_dict['attachments'][0]['url'])
#TODO handle more than one atachment
attachments=attachments)
except KeyError as error:
raise Exception('No {} in attempts.'.format(error))
return submission_attempt
Expand Down
23 changes: 12 additions & 11 deletions canvas_client/client.py
Expand Up @@ -104,17 +104,18 @@ def _download_submissions(self, submissions):
for attempt in submission.attempts:
attempt.dir = os.path.join(user_dir, 'attempt_' + str(attempt.nr))

#TODO - could be more attachments - files in a submission
attachment_path = os.path.join(attempt.dir, attempt.attachment_name)
attempt.attachment_path = attachment_path
#if the file is not already downloaded
if not os.path.isfile(attachment_path):
try:
os.mkdir(attempt.dir)
raw_attachment = self.canvasAPI.download_submission_attachment(attempt.attachment_url)
util.save_raw_file(attachment_path, raw_attachment)
except OSError:
pass
try:
os.mkdir(attempt.dir)

for attachment in attempt.attachments:
attachment_path = os.path.join(attempt.dir, attachment.attachment_name)
attachment.attachment_path = attachment_path
#if the file is not already downloaded
if not os.path.isfile(attachment_path):
raw_attachment = self.canvasAPI.download_submission_attachment(attachment.attachment_url)
util.save_raw_file(attachment_path, raw_attachment)
except OSError:
pass

return submissions

Expand Down
14 changes: 9 additions & 5 deletions canvas_client/submission.py
Expand Up @@ -6,11 +6,10 @@ def __init__(self, user_id, user_name, user_comments, late, attempts, user_secti
self.user_comments = user_comments
self.late = late
self.attempts = attempts


class SubmissionAttempt(object):
def __init__( self, nr, late, submitted_at, seconds_late,
attachment_name, attachment_url, attachment_path=None,
def __init__( self, nr, late, submitted_at, seconds_late, attachments,
comment=None, grade=None, dir=None, delay_time=None):
self.nr = nr
self.dir = dir
Expand All @@ -20,10 +19,15 @@ def __init__( self, nr, late, submitted_at, seconds_late,
self.delay_time = delay_time
self.comment = comment
self.grade = grade
self.attachments = attachments


class SubmissionAttachment(object):
def __init__( self, attachment_name, attachment_url, attachment_path=None):
self.attachment_name = attachment_name
self.attachment_url = attachment_url
self.attachment_path = attachment_path

self.attachment_url = attachment_url


class SubmissionComment(object):
def __init__(self, comment, author_name, created_at):
Expand Down

0 comments on commit a016959

Please sign in to comment.