|
1 import os |
|
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, UPLOAD_TO |
|
6 from uploadhandler import QuotaUploadHandler, ErroringUploadHandler |
|
7 from django.utils.hashcompat import sha_constructor |
|
8 from tests import UNICODE_FILENAME |
|
9 |
|
10 def file_upload_view(request): |
|
11 """ |
|
12 Check that a file upload can be updated into the POST dictionary without |
|
13 going pear-shaped. |
|
14 """ |
|
15 form_data = request.POST.copy() |
|
16 form_data.update(request.FILES) |
|
17 if isinstance(form_data.get('file_field'), UploadedFile) and isinstance(form_data['name'], unicode): |
|
18 # If a file is posted, the dummy client should only post the file name, |
|
19 # not the full path. |
|
20 if os.path.dirname(form_data['file_field'].name) != '': |
|
21 return HttpResponseServerError() |
|
22 return HttpResponse('') |
|
23 else: |
|
24 return HttpResponseServerError() |
|
25 |
|
26 def file_upload_view_verify(request): |
|
27 """ |
|
28 Use the sha digest hash to verify the uploaded contents. |
|
29 """ |
|
30 form_data = request.POST.copy() |
|
31 form_data.update(request.FILES) |
|
32 |
|
33 for key, value in form_data.items(): |
|
34 if key.endswith('_hash'): |
|
35 continue |
|
36 if key + '_hash' not in form_data: |
|
37 continue |
|
38 submitted_hash = form_data[key + '_hash'] |
|
39 if isinstance(value, UploadedFile): |
|
40 new_hash = sha_constructor(value.read()).hexdigest() |
|
41 else: |
|
42 new_hash = sha_constructor(value).hexdigest() |
|
43 if new_hash != submitted_hash: |
|
44 return HttpResponseServerError() |
|
45 |
|
46 # Adding large file to the database should succeed |
|
47 largefile = request.FILES['file_field2'] |
|
48 obj = FileModel() |
|
49 obj.testfile.save(largefile.name, largefile) |
|
50 |
|
51 return HttpResponse('') |
|
52 |
|
53 def file_upload_unicode_name(request): |
|
54 |
|
55 # Check to see if unicode name came through properly. |
|
56 if not request.FILES['file_unicode'].name.endswith(UNICODE_FILENAME): |
|
57 return HttpResponseServerError() |
|
58 |
|
59 response = None |
|
60 |
|
61 # Check to make sure the exotic characters are preserved even |
|
62 # through file save. |
|
63 uni_named_file = request.FILES['file_unicode'] |
|
64 obj = FileModel.objects.create(testfile=uni_named_file) |
|
65 full_name = u'%s/%s' % (UPLOAD_TO, uni_named_file.name) |
|
66 if not os.path.exists(full_name): |
|
67 response = HttpResponseServerError() |
|
68 |
|
69 # Cleanup the object with its exotic file name immediately. |
|
70 # (shutil.rmtree used elsewhere in the tests to clean up the |
|
71 # upload directory has been seen to choke on unicode |
|
72 # filenames on Windows.) |
|
73 obj.delete() |
|
74 |
|
75 if response: |
|
76 return response |
|
77 else: |
|
78 return HttpResponse('') |
|
79 |
|
80 def file_upload_echo(request): |
|
81 """ |
|
82 Simple view to echo back info about uploaded files for tests. |
|
83 """ |
|
84 r = dict([(k, f.name) for k, f in request.FILES.items()]) |
|
85 return HttpResponse(simplejson.dumps(r)) |
|
86 |
|
87 def file_upload_quota(request): |
|
88 """ |
|
89 Dynamically add in an upload handler. |
|
90 """ |
|
91 request.upload_handlers.insert(0, QuotaUploadHandler()) |
|
92 return file_upload_echo(request) |
|
93 |
|
94 def file_upload_quota_broken(request): |
|
95 """ |
|
96 You can't change handlers after reading FILES; this view shouldn't work. |
|
97 """ |
|
98 response = file_upload_echo(request) |
|
99 request.upload_handlers.insert(0, QuotaUploadHandler()) |
|
100 return response |
|
101 |
|
102 def file_upload_getlist_count(request): |
|
103 """ |
|
104 Check the .getlist() function to ensure we receive the correct number of files. |
|
105 """ |
|
106 file_counts = {} |
|
107 |
|
108 for key in request.FILES.keys(): |
|
109 file_counts[key] = len(request.FILES.getlist(key)) |
|
110 return HttpResponse(simplejson.dumps(file_counts)) |
|
111 |
|
112 def file_upload_errors(request): |
|
113 request.upload_handlers.insert(0, ErroringUploadHandler()) |
|
114 return file_upload_echo(request) |