Lucas
Lucas

Reputation: 949

How to properly implement inheritance in App Engine with webapp2

Given this simple class:

class RenderResponseHandler(webapp2.RequestHandler):
    def __init__(self):
        self.nombreUsuario = obtenerUsuarioTablaUsuario().nombre if obtenerUsuarioTablaUsuario() else None
    def doRender(self, tname='index.html',values = {}):
        temp = os.path.join(os.path.dirname(__file__),
                            'templates/'+tname)
        if not os.path.isfile(temp):
            return False

        self.nombreUsuario = obtenerUsuarioTablaUsuario().nombre if obtenerUsuarioTablaUsuario() else None

        newval = dict(values)
        newval['path'] = self.request.path

        if nombreUsuario:
            url_linktext = 'logout'
        else:
            url_linktext = 'login'

        newval['url_linktext'] = url_linktext
        newval['nombreUsuario'] = nombreUsuario

        template = jinja_environment.get_template(tname)
        outstr = template.render(newval)
        self.response.out.write(outstr)

        return True

    def obtenerPagina(self, pOpcion = None, pMensajeInformacion = None):
        opcion = pOpcion if pOpcion is not None else self.request.get('opcion')
        if opcion == 'index':
            self.doRender('index.html')
        ####

If I do this, the page shows normally

class MainPage(webapp2.RequestHandler):
    def get(self):
        r = RenderResponseHandler()
        r.request = self.request
        r.response = self.response 
        r.obtenerPagina('index')

Although, When trying to inherit from RenderResponseHandler,like this:

class MainPage(RenderResponseHandler):
    def get(self):
        if self.doRender(self.request.path):
            return
        self.obtenerPagina('index') 

I get the following exception:

Traceback (most recent call last):   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1536, in
__call__
    rv = self.handle_exception(request, response, e)   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1530, in
__call__
    rv = self.router.dispatch(request, response)   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1101, in
__call__
    handler = self.handler(request, response) TypeError: __init__() takes exactly 1 argument (3 given)

What am I doing wrong?

Thanks in advance!

Upvotes: 3

Views: 1183

Answers (2)

Suat Atan PhD
Suat Atan PhD

Reputation: 1382

As it seen below; we can call myfunc function from other Google App Engine class without statament in class definition line. _init _ function is confuses GAE because his initial function is get()

class Test2(webapp2.RequestHandler):
    def get(self):
          tx=Test()
          self.response.out.write(tx.myfunc())


class Test(webapp2.RequestHandler):
  def myfunc(self):
      return "Hooooooooooooooop"
  def get (self,request=None, response=None):
      self.response.out.write("test2")

Upvotes: 0

Shay Erlichmen
Shay Erlichmen

Reputation: 31928

You need to implement the __init__ of RenderResponseHandler Like this: def __init__(self, request=None, response=None) and pass request and response to the base class.

The layers that call the request handler class relay on the contract that the ctor will have those paramters.

Upvotes: 6

Related Questions