Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

file upload with basic submission, fixed #119

  • Loading branch information...
commit 9473895983972f6da0fc56b4a7dfa1b6f7a3a5e3 1 parent 4d4b880
@sehmaschine authored
Showing with 34 additions and 32 deletions.
  1. +9 −12 filebrowser/sites.py
  2. +25 −20 filebrowser/tests/sites.py
View
21 filebrowser/sites.py
@@ -454,22 +454,19 @@ def _upload_file(self, request):
Upload file to the server.
"""
if request.method == "POST":
+ folder = request.GET.get('folder', '')
+
if request.is_ajax(): # Advanced (AJAX) submission
- folder = request.GET.get('folder')
filedata = ContentFile(request.raw_post_data)
- try:
- filedata.name = convert_filename(request.GET['qqfile'])
- except KeyError:
- return HttpResponseBadRequest('Invalid request! No filename given.')
else: # Basic (iframe) submission
- # TODO: This needs some attention, do we use this at all?
- folder = request.POST.get('folder')
- if len(request.FILES) == 1:
- filedata = request.FILES.values()[0]
- else:
+ if len(request.FILES) != 1:
raise Http404('Invalid request! Multiple files included.')
- # filedata.name = convert_filename(upload.name)
- filedata.name = convert_filename(request.POST.get('file_name'))
+ filedata = request.FILES.values()[0]
+
+ try:
+ filedata.name = convert_filename(request.GET['qqfile'])
+ except KeyError:
+ return HttpResponseBadRequest('Invalid request! No filename given.')
fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
folder = fb_uploadurl_re.sub('', folder)
View
45 filebrowser/tests/sites.py
@@ -9,6 +9,7 @@
"""
# PYTHON IMPORTS
+from __future__ import with_statement
import os
import sys
import shutil
@@ -89,12 +90,13 @@ def test_upload(test):
def test_do_upload(test):
## Attemp an upload using AJAX SUBMISSION
- f = open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'), "rb")
- file_size = os.path.getsize(f.name)
+
url = reverse('%s:fb_do_upload' % test.site_name)
url = '?'.join([url, urlencode({'folder': test.tmpdir.path_relative_directory, 'qqfile': 'testimage.jpg'})])
- response = test.c.post(url, data=f.read(), content_type='application/octet-stream', HTTP_X_REQUESTED_WITH='XMLHttpRequest', X_File_Name='testimage.jpg')
- f.close()
+
+ with open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'), "rb") as f:
+ file_size = os.path.getsize(f.name)
+ response = test.c.post(url, data=f.read(), content_type='application/octet-stream', HTTP_X_REQUESTED_WITH='XMLHttpRequest', X_File_Name='testimage.jpg')
# Check we get OK response
test.assertTrue(response.status_code == 200)
@@ -106,22 +108,25 @@ def test_do_upload(test):
# Check the file has the correct size
test.assertTrue(file_size == test.site.storage.size(path))
+
+ ## Attemp an upload of the file using BASIC SUBMISSION
+
+ url = reverse('%s:fb_do_upload' % test.site_name)
+ url = '?'.join([url, urlencode({'folder': test.tmpdir.path_relative_directory, 'qqfile': 'testimage_basic.jpg'})])
+
+ with open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'), 'rb') as f:
+ file_size = os.path.getsize(f.name)
+ response = test.c.post(url, {'qqfile': f})
+
+ # Check we get OK response
+ test.assertTrue(response.status_code == 200)
- # ## Attemp an upload of the file using BASIC SUBMISSION
- # f = open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'))
- # url = reverse('%s:fb_do_upload' % test.site_name)
- # response = test.c.post(url, {'file':f, 'folder': test.tmpdir.path_relative_directory, 'file_name': 'testimage_basic.jpg'})
- # f.close()
-
- # # Check we get OK response
- # test.assertTrue(response.status_code == 200)
-
- # # Check the file now exists
- # abs_path = os.path.join(test.tmpdir.path, 'testimage_basic.jpg')
- # test.assertTrue(test.site.storage.exists(abs_path))
-
- # # Check the file has the correct size
- # test.assertTrue(file_size == os.path.getsize(abs_path))
+ # Check the file now exists
+ path = os.path.join(test.tmpdir.path, 'testimage_basic.jpg')
+ test.assertTrue(test.site.storage.exists(path))
+
+ # Check the file has the correct size
+ test.assertTrue(file_size == test.site.storage.size(path))
def test_detail(test):
"""
@@ -254,4 +259,4 @@ def runTest(self):
# Delete the attribute test_class, otherwise it will be
# considered as a test case by django
-delattr(this_module, 'testcase_class')
+delattr(this_module, 'testcase_class')
Please sign in to comment.
Something went wrong with that request. Please try again.