-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
92 additions
and
13 deletions.
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
Empty file.
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class DownloadsConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'downloads' |
Empty file.
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,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
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,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
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 @@ | ||
from django.conf.urls import url | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from competition.models import Solution | ||
|
||
from .views import download_protected_file | ||
|
||
urlpatterns = [ | ||
# Include non-translated versions only since Admin ignores lang prefix | ||
url(r'solutions/(?P<path>.*)$', download_protected_file, | ||
{'path_prefix': 'solutions/', 'model_class': Solution}, | ||
name='download_solution'), | ||
url(r'corrected_solutions/(?P<path>.*)$', download_protected_file, | ||
{'path_prefix': 'corrected_solutions/', | ||
'model_class': Solution}, | ||
name='download_corrected_solution'), | ||
|
||
] |
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,29 @@ | ||
import os | ||
|
||
from django.conf import settings | ||
from django.contrib.auth.decorators import login_required | ||
from django.core.exceptions import PermissionDenied | ||
from django_sendfile import sendfile | ||
from rest_framework.request import Request | ||
|
||
|
||
@login_required | ||
def download_protected_file(request: Request, model_class, path_prefix, path): | ||
""" | ||
This view allows download of the file at the specified path, if the user | ||
is allowed to. This is checked by calling the model's can_access_files | ||
method. | ||
""" | ||
filepath = os.path.join(settings.PRIVATE_STORAGE_ROOT, path_prefix, path) | ||
filepath_mediapath = path_prefix + path | ||
|
||
if request.user.is_authenticated: | ||
# Superusers can access all files | ||
if request.user.is_superuser: | ||
return sendfile(request, filepath) | ||
obj = model_class.get_by_filepath(filepath_mediapath) | ||
|
||
if obj is not None and obj.can_access(request.user): | ||
return sendfile(request, filepath) | ||
|
||
raise PermissionDenied |
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