views.py 4.2 KB

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