Skip to content
Snippets Groups Projects
Commit 7d8f3031 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

possibility to upload files that are served by django

parent a67f809c
No related branches found
No related tags found
1 merge request!115Resolve "PDP study patient data"
...@@ -4,6 +4,9 @@ env/* ...@@ -4,6 +4,9 @@ env/*
# Folder with db statics (dev mode) # Folder with db statics (dev mode)
smash/~/ smash/~/
# files uploaded and hosted by django
smash/uploads/
# Disable python bytecode # Disable python bytecode
*.pyc *.pyc
#vim swap files #vim swap files
......
# coding=utf-8 # coding=utf-8
import locale import locale
from django.core.files.storage import FileSystemStorage
BOOL_CHOICES = ((True, 'Yes'), (False, 'No')) BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))
SEX_CHOICES_MALE = 'M' SEX_CHOICES_MALE = 'M'
SEX_CHOICES_FEMALE = 'F' SEX_CHOICES_FEMALE = 'F'
...@@ -90,3 +92,5 @@ VOUCHER_STATUS_CHOICES = ( ...@@ -90,3 +92,5 @@ VOUCHER_STATUS_CHOICES = (
(VOUCHER_STATUS_USED, 'Used'), (VOUCHER_STATUS_USED, 'Used'),
(VOUCHER_STATUS_EXPIRED, 'Expired'), (VOUCHER_STATUS_EXPIRED, 'Expired'),
) )
FILE_STORAGE = FileSystemStorage(location='uploads')
...@@ -206,6 +206,12 @@ urlpatterns = [ ...@@ -206,6 +206,12 @@ urlpatterns = [
url(r'^configuration$', views.configuration_item.configuration_items, name='web.views.configuration'), url(r'^configuration$', views.configuration_item.configuration_items, name='web.views.configuration'),
####################
# FILES #
####################
url(r'^files/', views.uploaded_files.download, name='web.views.uploaded_files'),
#################### ####################
# AUTH # # AUTH #
#################### ####################
......
...@@ -81,3 +81,4 @@ import voucher ...@@ -81,3 +81,4 @@ import voucher
import voucher_type import voucher_type
import voucher_type_price import voucher_type_price
import redcap import redcap
import uploaded_files
# coding=utf-8
import logging
import ntpath
from wsgiref.util import FileWrapper
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from web.models.constants import FILE_STORAGE
logger = logging.getLogger(__name__)
def path_to_filename(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
@login_required
def download(request):
if request.GET and request.GET.get('file'):
path = FILE_STORAGE.location + "/" + request.GET.get('file')
response = HttpResponse(FileWrapper(open(path, 'r')), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % path_to_filename(path)
return response
import logging
from django import forms
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
logger = logging.getLogger(__name__)
class SecuredFileWidget(forms.FileInput):
"""A FileField Widget that shows secure file link"""
def __init__(self, attrs=None):
if attrs is None:
attrs = {}
super(SecuredFileWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
if value and hasattr(value, "url"):
url = reverse('web.views.uploaded_files') + '?file=' + unicode(value)
out = u'<a href="{}">{}</a><br />{} '
output.append(out.format(url, _(u'Download'), _(u'Change:')))
output.append(super(SecuredFileWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment