Search code examples
pythongoogle-app-engineblobstorewebapp2

Retrieving uploaded Blob info from a regular handler


I would like to use Google's BlobStore for my GAE app. I want the user to upload an image along with other form data. I know I can just use self.request.get event from within a BlobstoreUploadHandler to fetch other form data. However, I have created a subclass of appengine's generic handler (i.e. class Handler(webapp.RequestHandler)) that incorporates other methods I want all of my handlers to have. Is there I way I can still have my upload handler inherit from Handler and still get the blob info?

Normal way to fetch blobs

class UploadHandler(blobstore.BlobstoreUploadHandler):
    def post(self):
        blob_info = self.get_uploads("image")

Way I want to fetch blobs

class Handler(webapp.RequestHandler):
    def get_logged_in_user(self):
         #check cookie and session data
         return username

class UploadHandler(Handler):
    def post(self):
        image_info = # Somehow get blobinfo
        Data = Data(title=self.request.get("name"), image=image_info)

Note: I don't want to make Handler inherit from BlobstoreUploadHandler because I use the Handler class for other handlers as well.


Solution

  • You could create a mixin class which defines your special methods and use that to create handler. You could then use the same mixin to create the upload handler...

    class HandlerMixin(object):
        def get_logged_in_user(self):
            ...
    
    class Handler(webapp2.RequestHandler, HandlerMixin):
        pass
    
    class UploadHandler(blobstore.BlobstoreUploadHandler, HandlerMixin):
        def post(self):
            ...
    

    1Note that the BlobstoreUploadHandler is a subclass of webapp.RequestHandler rather than webapp2.RequestHandler. I think that in most ways, webapp and webapp2 are API compatible, but just know that if you do something funky in HandlerMixin which requires webapp2 instead of just webapp, that method won't work in UploadHandler.