Search code examples
pythongoogle-app-engineblobstorerequesthandler

GAE: how can I combine a BlobstoreUploadHandler and a RequestHandler in 1 webpage


I am making a mailing app in Python on the Google App Engine.

I want to enable an attachment upload (which posts to a BlobstoreUploadHandler) in a 'normal' webpage (posting to a RequestHandler).

If the user has filled part of the 'normal' form, how can I preserve those values after the user has uploaded his (or her) attachment (other then copying all the fields with javascript before submitting the post)?


Solution

  • You can write a request handler that derives from two classes:

    class YourRequestHandler(BlobstoreUploadHandler, RequestHandler):
        pass
    

    I also tried this with webapp2's RequestHandlers and it works.

    P.S.: In order to prevent orphaned blobs because the user uploaded more files than your application expects (this can easily happen as you have no control over the user's browser), I suggest to write your post handler along the following lines:

    def post(self):
        uploads = self.get_uploads()
        try:
            pass  # Put your application-specific code here.
            # As soon as you have stored a blob key in the database (using a transaction),
            # remove the corresponding upload from the uploads array.
        finally:
            keys = [upload.key() for upload in uploads]
            blobstore.delete_multi(keys)