Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Exports
3__all__ = (
4 "BaseAjaxStorage",
5)
7# Classes
10class BaseAjaxStorage(object):
11 """Base class for consistently implementing an AJAX upload backend."""
13 BUFFER_SIZE = 10485760 # 10MB
15 def __init__(self, **kwargs):
16 self.__dict__.update(kwargs)
18 def setup(self, filename, *args, **kwargs):
19 """Responsible for doing any pre-processing needed before the upload starts."""
20 raise NotImplementedError()
22 def update_filename(self, request, filename, *args, **kwargs):
23 """Returns a new name for the file being uploaded."""
24 raise NotImplementedError()
26 def upload_chunk(self, chunk, *args, **kwargs):
27 """Called when a string was read from the client, responsible for writing that string to the destination file.
28 """
29 raise NotImplementedError()
31 def upload_complete(self, request, filename, *args, **kwargs):
32 """Overriden to performs any actions needed post-upload, and returns a dict to be added to the render / json
33 context."""
34 raise NotImplementedError()
36 def upload(self, uploaded, filename, raw_data, *args, **kwargs):
37 try:
38 if raw_data:
39 # File was uploaded via ajax, and is streaming in.
40 chunk = uploaded.read(self.BUFFER_SIZE)
41 while len(chunk) > 0:
42 self.upload_chunk(chunk, *args, **kwargs)
43 chunk = uploaded.read(self.BUFFER_SIZE)
44 else:
45 # File was uploaded via a POST, and is here.
46 for chunk in uploaded.chunks():
47 self.upload_chunk(chunk, *args, **kwargs)
48 return True
49 except:
50 # things went badly.
51 return False