[Hejes-devel] [933] controllers/blog.py: added user-friendly exception handling

hejes-devel at nytud.hu hejes-devel at nytud.hu
Wed Sep 11 17:02:19 CEST 2013


Revision: 933
Author:   mihaltz
Date:     2013-09-11 17:02:19 +0200 (Wed, 11 Sep 2013)
Log Message:
-----------
controllers/blog.py: added user-friendly exception handling

Modified Paths:
--------------
    trunk/web2py/applications/helyesiras_webdev/controllers/blog.py
    trunk/web2py/applications/helyesiras_webdev/views/blog/show.html

Modified: trunk/web2py/applications/helyesiras_webdev/controllers/blog.py
===================================================================
--- trunk/web2py/applications/helyesiras_webdev/controllers/blog.py	2013-09-11 14:44:34 UTC (rev 932)
+++ trunk/web2py/applications/helyesiras_webdev/controllers/blog.py	2013-09-11 15:02:19 UTC (rev 933)
@@ -8,6 +8,10 @@
 from util import safeint
 from blogutil import get_feed
 
+EXCEPTIONMSG = 'Elnézést kérünk, váratlan hiba történt. Kérjük, próbálkozzon újra később.'
+"""Flashed when an exception is caught in any controller. If emtpy string, no flashing."""
+
+
 def index():
   """Starting page for the blog.
      returns following keys:
@@ -17,34 +21,37 @@
      ntotal: total number of posts available
      page: page number (1...) for pagination
      perpage: number of posts per page
-     TODO:
-     check GET vars (category, tag(s?)) and filter posts accordingly
   """
-  # all categories & tags for sidebar
-  cats = dbblog().select(dbblog.categories.ALL, orderby=dbblog.categories.id)
-  tags = dbblog().select(dbblog.tags.ALL, orderby=dbblog.tags.name)
-  # assemble query:  posts filtered by category or tag specified via args
-  query = dbblog.posts.category == dbblog.categories.id
-  usercat = request.vars.get('category')
-  if usercat:
-    query &= (dbblog.categories.name == usercat)
-  usertag = request.vars.get('tag')
-  if usertag:
-    query &= (dbblog.posts.tags.contains(usertag))
-  # prepare pagination
-  rset = dbblog(query)
-  ntotal = rset.count()
-  if ntotal == 0: # 0 articles found: redirect to index page with no args
-    redirect(URL(c='blog', f='index'))
-  perpage = 5
-  npages = (ntotal / perpage) + (1 if ntotal % perpage > 0 else 0)
-  tmp = safeint(request.vars.get('page'))
-  page = tmp if tmp != None and tmp > 0 and tmp <= npages else 1
-  offset = (page - 1) * perpage
-  limitby = (offset, offset + perpage)  
-  # fetch desired rows
-  posts = rset.select(orderby=~dbblog.posts.created_on, limitby=limitby)
-  return dict(categories=cats, tags=tags, posts=posts, ntotal=ntotal, page=page, perpage=perpage)
+  try:
+    # all categories & tags for sidebar
+    cats = dbblog().select(dbblog.categories.ALL, orderby=dbblog.categories.id)
+    tags = dbblog().select(dbblog.tags.ALL, orderby=dbblog.tags.name)
+    # assemble query:  posts filtered by category or tag specified via args
+    query = dbblog.posts.category == dbblog.categories.id
+    usercat = request.vars.get('category')
+    if usercat:
+      query &= (dbblog.categories.name == usercat)
+    usertag = request.vars.get('tag')
+    if usertag:
+      query &= (dbblog.posts.tags.contains(usertag))
+    # prepare pagination
+    rset = dbblog(query)
+    ntotal = rset.count()
+    if ntotal == 0: # 0 articles found: redirect to index page with no args
+      redirect(URL(c='blog', f='index'))
+    perpage = 5
+    npages = (ntotal / perpage) + (1 if ntotal % perpage > 0 else 0)
+    tmp = safeint(request.vars.get('page'))
+    page = tmp if tmp != None and tmp > 0 and tmp <= npages else 1
+    offset = (page - 1) * perpage
+    limitby = (offset, offset + perpage)  
+    # fetch desired rows
+    posts = rset.select(orderby=~dbblog.posts.created_on, limitby=limitby)
+    return dict(categories=cats, tags=tags, posts=posts, ntotal=ntotal, page=page, perpage=perpage)
+  except:
+    global EXCEPTIONMSG
+    response.flash = EXCEPTIONMSG
+    return dict(categories=[], tags=[], posts=[], ntotal=0, page=1, perpage=5)
 
 
 def show():
@@ -52,19 +59,24 @@
      slug of post to show is passed as an unnamed argument with HTTP GET.
      If no argumement is present, or if slug is not found in DB, redirect to index page (+flash message?)
      Returns keys:
-     post: a Row object, first row from a combined select from posts, categories, tags, images
+     post: a Row object, first row from a combined select from posts, categories, tags
      categories: Rows object with all category names and ids (for sidebar)
      tags: Rows object with all tag names and ids (for sidebar)
   """
-  cats = dbblog().select(dbblog.categories.ALL, orderby=dbblog.categories.id)
-  tags = dbblog().select(dbblog.tags.ALL, orderby=dbblog.tags.name)
-  slug = request.args(0)
-  if slug == None: # no arg: redirect to index
-    redirect(URL(c='blog', f='index'))
-  rows = dbblog( (dbblog.posts.category==dbblog.categories.id) & (dbblog.posts.slug==slug)).select()
-  if len(rows) == 0: # post not found: redirect to index
-    redirect(URL(c='blog', f='index'))
-  return dict(categories=cats, tags=tags, post=rows.first())
+  try:
+    cats = dbblog().select(dbblog.categories.ALL, orderby=dbblog.categories.id)
+    tags = dbblog().select(dbblog.tags.ALL, orderby=dbblog.tags.name)
+    slug = request.args(0)
+    if slug == None: # no arg: redirect to index
+      redirect(URL(c='blog', f='index'))
+    rows = dbblog( (dbblog.posts.category==dbblog.categories.id) & (dbblog.posts.slug==slug)).select()
+    if len(rows) == 0: # post not found: redirect to index
+      redirect(URL(c='blog', f='index'))
+    return dict(categories=cats, tags=tags, post=rows.first())
+  except:
+    global EXCEPTIONMSG
+    response.flash = EXCEPTIONMSG
+    return dict(categories=[], tags=[], post=None)
 
 
 @auth.requires_login()

Modified: trunk/web2py/applications/helyesiras_webdev/views/blog/show.html
===================================================================
--- trunk/web2py/applications/helyesiras_webdev/views/blog/show.html	2013-09-11 14:44:34 UTC (rev 932)
+++ trunk/web2py/applications/helyesiras_webdev/views/blog/show.html	2013-09-11 15:02:19 UTC (rev 933)
@@ -25,8 +25,9 @@
 
 {{#=response.toolbar()}}
 <a href="{{=URL(c='blog', f='index')}}"> <img src="http://htp-devel.nytud.hu/helyesiras_webdev/static/images/blog_h.png"> </a>
+
+{{if post != None:}}
     
-    
 <div class="blog-head">
 	<h1>{{=post.posts.title}}</h1>
 	<div class="blog-cat-name">{{=post.categories.name}}</div>
@@ -72,4 +73,6 @@
 </div>
 </div>
 
+{{pass}} {{# (if post != None:) }}
+
 </div> <!-- <div id="blog-container"> -->




More information about the Hejes-devel mailing list