~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Upload handlers to test the upload API.
 
3
"""
 
4
 
 
5
from django.core.files.uploadhandler import FileUploadHandler, StopUpload
 
6
 
 
7
class QuotaUploadHandler(FileUploadHandler):
 
8
    """
 
9
    This test upload handler terminates the connection if more than a quota
 
10
    (5MB) is uploaded.
 
11
    """
 
12
    
 
13
    QUOTA = 5 * 2**20 # 5 MB
 
14
    
 
15
    def __init__(self, request=None):
 
16
        super(QuotaUploadHandler, self).__init__(request)
 
17
        self.total_upload = 0
 
18
        
 
19
    def receive_data_chunk(self, raw_data, start):
 
20
        self.total_upload += len(raw_data)
 
21
        if self.total_upload >= self.QUOTA:
 
22
            raise StopUpload(connection_reset=True)
 
23
        return raw_data
 
24
            
 
25
    def file_complete(self, file_size):
 
26
        return None
 
27
 
 
28
class CustomUploadError(Exception):
 
29
    pass
 
30
 
 
31
class ErroringUploadHandler(FileUploadHandler):
 
32
    """A handler that raises an exception."""
 
33
    def receive_data_chunk(self, raw_data, start):
 
34
        raise CustomUploadError("Oops!")