PageRenderTime 21ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/file_uploads/views.py

https://code.google.com/p/mango-py/
Python | 115 lines | 108 code | 1 blank | 6 comment | 3 complexity | 287c50e1687262ae634423ff94b8be34 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. 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 = sha_constructor(value.read()).hexdigest()
  38. else:
  39. new_hash = sha_constructor(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_quota(request):
  76. """
  77. Dynamically add in an upload handler.
  78. """
  79. request.upload_handlers.insert(0, QuotaUploadHandler())
  80. return file_upload_echo(request)
  81. def file_upload_quota_broken(request):
  82. """
  83. You can't change handlers after reading FILES; this view shouldn't work.
  84. """
  85. response = file_upload_echo(request)
  86. request.upload_handlers.insert(0, QuotaUploadHandler())
  87. return response
  88. def file_upload_getlist_count(request):
  89. """
  90. Check the .getlist() function to ensure we receive the correct number of files.
  91. """
  92. file_counts = {}
  93. for key in request.FILES.keys():
  94. file_counts[key] = len(request.FILES.getlist(key))
  95. return HttpResponse(simplejson.dumps(file_counts))
  96. def file_upload_errors(request):
  97. request.upload_handlers.insert(0, ErroringUploadHandler())
  98. return file_upload_echo(request)