2
from django.core.files.uploadedfile import UploadedFile
3
from django.http import HttpResponse, HttpResponseServerError
4
from django.utils import simplejson
5
from models import FileModel
6
from uploadhandler import QuotaUploadHandler, ErroringUploadHandler
7
from django.utils.hashcompat import sha_constructor
9
def file_upload_view(request):
11
Check that a file upload can be updated into the POST dictionary without
14
form_data = request.POST.copy()
15
form_data.update(request.FILES)
16
if isinstance(form_data.get('file_field'), UploadedFile) and isinstance(form_data['name'], unicode):
17
# If a file is posted, the dummy client should only post the file name,
19
if os.path.dirname(form_data['file_field'].name) != '':
20
return HttpResponseServerError()
21
return HttpResponse('')
23
return HttpResponseServerError()
25
def file_upload_view_verify(request):
27
Use the sha digest hash to verify the uploaded contents.
29
form_data = request.POST.copy()
30
form_data.update(request.FILES)
32
# Check to see if unicode names worked out.
33
if not request.FILES['file_unicode'].name.endswith(u'test_\u4e2d\u6587_Orl\xe9ans.jpg'):
34
return HttpResponseServerError()
36
for key, value in form_data.items():
37
if key.endswith('_hash'):
39
if key + '_hash' not in form_data:
41
submitted_hash = form_data[key + '_hash']
42
if isinstance(value, UploadedFile):
43
new_hash = sha_constructor(value.read()).hexdigest()
45
new_hash = sha_constructor(value).hexdigest()
46
if new_hash != submitted_hash:
47
return HttpResponseServerError()
49
# Adding large file to the database should succeed
50
largefile = request.FILES['file_field2']
52
obj.testfile.save(largefile.name, largefile)
54
return HttpResponse('')
56
def file_upload_echo(request):
58
Simple view to echo back info about uploaded files for tests.
60
r = dict([(k, f.name) for k, f in request.FILES.items()])
61
return HttpResponse(simplejson.dumps(r))
63
def file_upload_quota(request):
65
Dynamically add in an upload handler.
67
request.upload_handlers.insert(0, QuotaUploadHandler())
68
return file_upload_echo(request)
70
def file_upload_quota_broken(request):
72
You can't change handlers after reading FILES; this view shouldn't work.
74
response = file_upload_echo(request)
75
request.upload_handlers.insert(0, QuotaUploadHandler())
78
def file_upload_getlist_count(request):
80
Check the .getlist() function to ensure we receive the correct number of files.
84
for key in request.FILES.keys():
85
file_counts[key] = len(request.FILES.getlist(key))
86
return HttpResponse(simplejson.dumps(file_counts))
88
def file_upload_errors(request):
89
request.upload_handlers.insert(0, ErroringUploadHandler())
90
return file_upload_echo(request)