~ubuntu-branches/debian/squeeze/python-django/squeeze

« back to all changes in this revision

Viewing changes to tests/regressiontests/file_uploads/views.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb, Chris Lamb, David Spreen, Sandro Tosi
  • Date: 2008-11-19 21:31:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081119213100-gp0lqhxl1qxa6dgl
Tags: 1.0.2-1
[ Chris Lamb ]
* New upstream bugfix release. Closes: #505783
* Add myself to Uploaders with ACK from Brett.

[ David Spreen ]
* Remove python-pysqlite2 from Recommends because Python 2.5 includes
  sqlite library used by Django. Closes: 497886

[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
6
 
from uploadhandler import QuotaUploadHandler, ErroringUploadHandler
7
 
from django.utils.hashcompat import sha_constructor
8
 
 
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
 
 
25
 
def file_upload_view_verify(request):
26
 
    """
27
 
    Use the sha digest hash to verify the uploaded contents.
28
 
    """
29
 
    form_data = request.POST.copy()
30
 
    form_data.update(request.FILES)
31
 
 
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()
35
 
 
36
 
    for key, value in form_data.items():
37
 
        if key.endswith('_hash'):
38
 
            continue
39
 
        if key + '_hash' not in form_data:
40
 
            continue
41
 
        submitted_hash = form_data[key + '_hash']
42
 
        if isinstance(value, UploadedFile):
43
 
            new_hash = sha_constructor(value.read()).hexdigest()
44
 
        else:
45
 
            new_hash = sha_constructor(value).hexdigest()
46
 
        if new_hash != submitted_hash:
47
 
            return HttpResponseServerError()
48
 
 
49
 
    # Adding large file to the database should succeed
50
 
    largefile = request.FILES['file_field2']
51
 
    obj = FileModel()
52
 
    obj.testfile.save(largefile.name, largefile)
53
 
 
54
 
    return HttpResponse('')
55
 
 
56
 
def file_upload_echo(request):
57
 
    """
58
 
    Simple view to echo back info about uploaded files for tests.
59
 
    """
60
 
    r = dict([(k, f.name) for k, f in request.FILES.items()])
61
 
    return HttpResponse(simplejson.dumps(r))
62
 
    
63
 
def file_upload_quota(request):
64
 
    """
65
 
    Dynamically add in an upload handler.
66
 
    """
67
 
    request.upload_handlers.insert(0, QuotaUploadHandler())
68
 
    return file_upload_echo(request)
69
 
        
70
 
def file_upload_quota_broken(request):
71
 
    """
72
 
    You can't change handlers after reading FILES; this view shouldn't work.
73
 
    """
74
 
    response = file_upload_echo(request)
75
 
    request.upload_handlers.insert(0, QuotaUploadHandler())
76
 
    return response
77
 
 
78
 
def file_upload_getlist_count(request):
79
 
    """
80
 
    Check the .getlist() function to ensure we receive the correct number of files.
81
 
    """
82
 
    file_counts = {}
83
 
 
84
 
    for key in request.FILES.keys():
85
 
        file_counts[key] = len(request.FILES.getlist(key))
86
 
    return HttpResponse(simplejson.dumps(file_counts))
87
 
 
88
 
def file_upload_errors(request):
89
 
    request.upload_handlers.insert(0, ErroringUploadHandler())
90
 
    return file_upload_echo(request)