2
\$\begingroup\$

Please review this simple code. When I review it I decide to rewrite so that it doesn't write to a template, but instead directly to output since the whole HTML is fetched from the data layer. I used the tool appengine_admin to get editable HTML for simple html capabilities to an appspot project:

 class Page(db.Model):
 html = db.TextProperty(required=True)
 class AdminPage(appengine_admin.ModelAdmin):
 model = Page
 listFields = (
 'html',
 )
 editFields = (
 'html',
 )
 application = webapp.WSGIApplication([ ('/page/([0-9]+)', PageHandler), ])
 class PageHandler(BaseHandler):
 def get(self, file_id):
 page = Page.get_by_id(long(file_id))
 if not page:
 self.error(404)
 return
 self.render_template(file_id+'.html', {
 'body': page.body,
 })

My 2 to-dos:

  1. change variable file_id to page_id
  2. change writing to direct output instead of template
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 26, 2011 at 2:26
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$
 application = webapp.WSGIApplication([ ('/page/([0-9]+)', PageHandler), ])

I'd put the list of urls as a global constant, I think that would make it easier to read.

 class PageHandler(BaseHandler):
 def get(self, file_id):
 page = Page.get_by_id(long(file_id))
 if not page:

If you are checking against None here, I'd use if page is None:

 self.error(404)
 return

I'd use else instead of returning here

self.render_template(file_id+'.html', {
 'body': page.body,
})
answered Oct 26, 2011 at 4:05
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.