trying define base request handling
class webapp pages may inherit basic methods , variable otherwise required repeatedly defined each page of application. sort of similar functionality django preprocessors
. base class other pages inherit:
class basepage(webapp.requesthandler): def __init__(self): self.user = users.get_current_user() self.template_values = { 'user': self.user, 'environ': self, #i don't idea of passing whole environ object template ##the below 3 functions cannot executed during _init_ because of absence of self.request #'openid_providers': self.openid_providers(), #'logout_url': self.get_logout_url(), #'request': self.get_request(), } ##a sort of similar functionality render_to_response in django def render_template(self, template_name, values = none, *args, **kwargs): #path directory containing templates if values: value in values: self.template_values[value] = values[value] self.response.out.write(template.render(path+template_name, self.template_values, *args, **kwargs)) ##returns request name suggests def logout_url(self): return users.create_logout_url(self.request.url) ##returns request name suggests def request(self): return request ##returns openid login urls def openid_providers(self): #openid_poviders list of dictionary p in openid_providers: p['login_url'] = users.create_login_url(self.request.get('next', '/') , p['name'], p['url']) return openid_providers
everything working fine except cannot pass variables during initialization self.request not available. workaround did pass on whole self variable template variable.
is there other way provide template variables (request, logout_url etc) templates?
i've solved problem in appengine code using template method pattern
basically, base class looks like:
class mybasepage(webapp.requesthandler): def __init__(self): # common setup/init stuff here, # omitted discussion def setup(self): # request handling setup code needed in both get/post methods, # checking user login, getting session cookies, etc. # omitted discussion def get(self, *args): self.setup() # call derived class' 'doget' method has # logic inside self.doget(*args) def post(self, *args): self.setup() # call derived class' 'dopost' method self.dopost(*args) def doget(self, *args): ''' derived classes override method , put of logic inside. base class nothing.''' pass def dopost(self, *args): ''' derived classes override method , put of post logic inside. base class nothing.''' pass
...your derived classes need worry guts of doget()
, dopost()
methods.
Comments
Post a Comment