{"id":1851,"date":"2009-02-18T22:57:40","date_gmt":"2009-02-18T22:57:40","guid":{"rendered":"http:\/\/justthesam.com\/2009\/02\/google-app-engine-a-better-requesthandler\/"},"modified":"2009-02-18T22:57:40","modified_gmt":"2009-02-18T22:57:40","slug":"google-app-engine-a-better-requesthandler","status":"publish","type":"post","link":"https:\/\/justthesam.com\/?p=1851","title":{"rendered":"Google App Engine &#8211; a better RequestHandler"},"content":{"rendered":"<div>I&#39;ve developed a very simple webapp.RequestHandler subclass that does a couple of useful things that I need in my GAE app. They seem like pretty basic things that are worth sharing &#8211; though anyone could have done it themselves in five minutes.<\/div>\n<p><\/p>\n<div>\n<div><span style=\"font-family: Courier;\">class BBRequestHandler(webapp.RequestHandler):<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;# Render a template with standard data mixed in.<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;def render(self, template_name, template_data = {}):<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;template_data.update(self.sundries())<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;self.response.out.write(template.render(&#39;view\/&#39; + template_name, template_data))<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;# Forward the request to a different handler. Assumes GET.<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;def forward(self, handler_class):<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;handler = handler_class()<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;handler.initialize(self.request, self.response)<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;handler.get()<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;# Standard template data required for most pages.<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160;def sundries(self):<\/span><\/div>\n<div><span style=\"font-family: Courier;\">&#0160;&#0160; &#0160;return {<span style=\"font-family: &#39;Trebuchet MS&#39;; \">&#39;logout&#39;:users.create_logout_url(&#39;\/&#39;)<span style=\"font-family: Courier; \">}<\/span><\/span><\/span><\/div>\n<div><span style=\"font-family: Courier;\"><br \/><\/span><\/div>\n<div>The intention is that sundries() should return whatever standard data your pages require that&#39;s not specific to individual pages. In my case that&#39;s data that&#39;s required for the header, footer and sidebar &#8211; like the logout URL.<\/div>\n<p><\/p>\n<div>You may notice that I&#39;m not using the Python docstring format. I just can&#39;t quite bring myself to do that just yet, though I have got used to the indentation-is-block thing.<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve developed a very simple webapp.RequestHandler subclass that does a couple of useful things that I need in my GAE app. They seem like pretty basic things that are worth sharing &#8211; though anyone could have done it themselves in five minutes. class BBRequestHandler(webapp.RequestHandler): &#0160;&#0160;# Render a template with standard data mixed in. &#0160;&#0160;def render(self, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,33],"tags":[],"class_list":["post-1851","post","type-post","status-publish","format-standard","hentry","category-programming","category-webtech"],"_links":{"self":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1851"}],"version-history":[{"count":0,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1851\/revisions"}],"wp:attachment":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}