PageRenderTime 92ms CodeModel.GetById 79ms app.highlight 10ms 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
  1import os
  2from django.core.files.uploadedfile import UploadedFile
  3from django.http import HttpResponse, HttpResponseServerError
  4from django.utils import simplejson
  5from models import FileModel, UPLOAD_TO
  6from uploadhandler import QuotaUploadHandler, ErroringUploadHandler
  7from django.utils.hashcompat import sha_constructor
  8from tests import UNICODE_FILENAME
  9
 10def 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
 26def 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
 53def 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    os.unlink(full_name)
 75
 76    if response:
 77        return response
 78    else:
 79        return HttpResponse('')
 80
 81def file_upload_echo(request):
 82    """
 83    Simple view to echo back info about uploaded files for tests.
 84    """
 85    r = dict([(k, f.name) for k, f in request.FILES.items()])
 86    return HttpResponse(simplejson.dumps(r))
 87
 88def file_upload_quota(request):
 89    """
 90    Dynamically add in an upload handler.
 91    """
 92    request.upload_handlers.insert(0, QuotaUploadHandler())
 93    return file_upload_echo(request)
 94
 95def file_upload_quota_broken(request):
 96    """
 97    You can't change handlers after reading FILES; this view shouldn't work.
 98    """
 99    response = file_upload_echo(request)
100    request.upload_handlers.insert(0, QuotaUploadHandler())
101    return response
102
103def file_upload_getlist_count(request):
104    """
105    Check the .getlist() function to ensure we receive the correct number of files.
106    """
107    file_counts = {}
108
109    for key in request.FILES.keys():
110        file_counts[key] = len(request.FILES.getlist(key))
111    return HttpResponse(simplejson.dumps(file_counts))
112
113def file_upload_errors(request):
114    request.upload_handlers.insert(0, ErroringUploadHandler())
115    return file_upload_echo(request)