Skip to content

Commit

Permalink
Merge pull request #54 from mitodl/feature/split_upload
Browse files Browse the repository at this point in the history
Feature/split upload
  • Loading branch information
pdpinch committed Feb 12, 2015
2 parents e59cb84 + 68e5926 commit 1ca44ac
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions edx_sga/sga.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def upload_assignment(self, request, suffix=''):
}
student_id = self.student_submission_id()
submissions_api.create_submission(student_id, answer)
path = _file_storage_path(str(self.location), sha1, upload.file.name)
path = self._file_storage_path(sha1, upload.file.name)
if not default_storage.exists(path):
default_storage.save(path, File(upload.file))
return Response(json_body=self.student_state())
Expand All @@ -359,7 +359,7 @@ def staff_upload_annotated(self, request, suffix=''):
state['annotated_timestamp'] = _now().strftime(
DateTime.DATETIME_FORMAT
)
path = _file_storage_path(str(self.location), sha1, filename)
path = self._file_storage_path(sha1, filename)
if not default_storage.exists(path):
default_storage.save(path, File(upload.file))
module.state = json.dumps(state)
Expand All @@ -369,14 +369,15 @@ def staff_upload_annotated(self, request, suffix=''):
@XBlock.handler
def download_assignment(self, request, suffix=''):
answer = self.get_submission()['answer']
path = _file_storage_path(
str(self.location), answer['sha1'], answer['filename'])
path = self._file_storage_path(answer['sha1'], answer['filename'])
return self.download(path, answer['mimetype'], answer['filename'])

@XBlock.handler
def download_annotated(self, request, suffix=''):
path = _file_storage_path(
str(self.location), self.annotated_sha1, self.annotated_filename)
path = self._file_storage_path(
self.annotated_sha1,
self.annotated_filename,
)
return self.download(
path,
self.annotated_mimetype,
Expand All @@ -388,17 +389,15 @@ def staff_download(self, request, suffix=''):
require(self.is_course_staff())
submission = self.get_submission(request.params['student_id'])
answer = submission['answer']
path = _file_storage_path(
str(self.location), answer['sha1'], answer['filename'])
path = self._file_storage_path(answer['sha1'], answer['filename'])
return self.download(path, answer['mimetype'], answer['filename'])

@XBlock.handler
def staff_download_annotated(self, request, suffix=''):
require(self.is_course_staff())
module = StudentModule.objects.get(pk=request.params['module_id'])
state = json.loads(module.state)
path = _file_storage_path(
str(self.location),
path = self._file_storage_path(
state['annotated_sha1'],
state['annotated_filename']
)
Expand Down Expand Up @@ -475,11 +474,16 @@ def past_due(self):
def upload_allowed(self):
return not self.past_due() and self.score is None


def _file_storage_path(url, sha1, filename):
path = url[6:] + '/' + sha1
path += os.path.splitext(filename)[1]
return path
def _file_storage_path(self, sha1, filename):
path = (
'{loc.org}/{loc.course}/{loc.block_type}/{loc.block_id}'
'/{sha1}{ext}'.format(
loc=self.location,
sha1=sha1,
ext=os.path.splitext(filename)[1]
)
)
return path


def _get_sha1(file):
Expand All @@ -501,15 +505,15 @@ def _now():
return datetime.datetime.utcnow().replace(tzinfo=pytz.utc)


def load_resource(resource_path):
def load_resource(resource_path): # pragma: NO COVER
"""
Gets the content of a resource
"""
resource_content = pkg_resources.resource_string(__name__, resource_path)
return unicode(resource_content)


def render_template(template_path, context={}):
def render_template(template_path, context={}): # pragma: NO COVER
"""
Evaluate a template by resource path, applying the provided context
"""
Expand Down

0 comments on commit 1ca44ac

Please sign in to comment.