-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds django storage backend for ORA2 file uploads. #1018
Merged
pomegranited
merged 1 commit into
openedx:master
from
open-craft:jill/django-file-upload
Jul 14, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
from .base import BaseBackend | ||
|
||
from django.core.files.storage import default_storage | ||
from django.core.files.base import ContentFile | ||
from django.core.urlresolvers import reverse | ||
|
||
|
||
class Backend(BaseBackend): | ||
""" | ||
Manage openassessment student files uploaded using the default django storage settings. | ||
""" | ||
def get_upload_url(self, key, content_type): | ||
""" | ||
Return the URL pointing to the ORA2 django storage upload endpoint. | ||
""" | ||
return reverse("openassessment-django-storage", kwargs={'key': key}) | ||
|
||
def get_download_url(self, key): | ||
""" | ||
Return the django storage download URL for the given key. | ||
|
||
Returns None if no file exists at that location. | ||
""" | ||
path = self._get_file_path(key) | ||
if default_storage.exists(path): | ||
return default_storage.url(path) | ||
return None | ||
|
||
def upload_file(self, key, content): | ||
""" | ||
Upload the given file content to the keyed location. | ||
""" | ||
path = self._get_file_path(key) | ||
saved_path = default_storage.save(path, ContentFile(content)) | ||
return saved_path | ||
|
||
def remove_file(self, key): | ||
""" | ||
Remove the file at the given keyed location. | ||
|
||
Returns True if the file exists, and was removed. | ||
Returns False if the file does not exist, and so was not removed. | ||
""" | ||
path = self._get_file_path(key) | ||
if default_storage.exists(path): | ||
default_storage.delete(path) | ||
return True | ||
return False | ||
|
||
def _get_file_name(self, key): | ||
""" | ||
Returns the name of the keyed file. | ||
|
||
Since the backend storage may be folders, or it may use pseudo-folders, | ||
make sure the filename doesn't include any path separators. | ||
""" | ||
file_name = key.replace("..", "").strip("/ ") | ||
file_name = file_name.replace(os.sep, "_") | ||
return file_name | ||
|
||
def _get_file_path(self, key): | ||
""" | ||
Returns the path to the keyed file, including the storage prefix. | ||
""" | ||
path = self._get_key_name(self._get_file_name(key)) | ||
return path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from django.conf.urls import patterns, url | ||
|
||
urlpatterns = patterns( | ||
'openassessment.fileupload.views_django_storage', | ||
url(r'^django/(?P<key>.+)/$', 'django_storage', name='openassessment-django-storage'), | ||
) | ||
|
||
urlpatterns = patterns('openassessment.fileupload.views_filesystem', | ||
urlpatterns += patterns( | ||
'openassessment.fileupload.views_filesystem', | ||
url(r'^(?P<key>.+)/$', 'filesystem_storage', name='openassessment-filesystem-storage'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Provides the upload endpoint for the django storage backend. | ||
""" | ||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import HttpResponse | ||
from django.views.decorators.http import require_http_methods | ||
|
||
from .backends.django_storage import Backend | ||
|
||
|
||
@login_required() | ||
@require_http_methods(["PUT"]) | ||
def django_storage(request, key): | ||
""" | ||
Upload files using django storage backend. | ||
""" | ||
Backend().upload_file(key, request.body) | ||
return HttpResponse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include docstrings on these tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed by bb88ed1