Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
authorPawel Solyga <Pawel.Solyga@gmail.com>
Thu, 16 Oct 2008 23:31:57 +0000
changeset 365 74dec172944e
parent 364 ab47d3f494b3
child 366 0b8700836d4f
Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines. Patch by: Pawel Solyga Review by: to-be-reviewed
app/soc/views/docs/show.py
app/soc/views/helper/decorators.py
app/soc/views/helper/requests.py
app/soc/views/helper/responses.py
app/soc/views/person/profile.py
app/soc/views/simple.py
app/soc/views/site/docs/edit.py
app/soc/views/site/docs/list.py
app/soc/views/site/home.py
app/soc/views/site/settings.py
app/soc/views/site/sponsor/list.py
app/soc/views/site/sponsor/profile.py
app/soc/views/site/user/list.py
app/soc/views/site/user/profile.py
app/soc/views/sponsor/profile.py
app/soc/views/user/profile.py
app/soc/views/user/roles.py
--- a/app/soc/views/docs/show.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/docs/show.py	Thu Oct 16 23:31:57 2008 +0000
@@ -29,6 +29,7 @@
 from soc.logic.models import document
 from soc.views import helper
 from soc.views import simple
+from soc.views.helper import decorators
 
 import soc.views.helper.responses
 import soc.views.helper.templates
@@ -36,6 +37,7 @@
 
 DEF_DOCS_PUBLIC_TMPL = 'soc/docs/public.html'
 
+@decorators.view
 def public(request, page=None, partial_path=None, link_name=None,
            template=DEF_DOCS_PUBLIC_TMPL):
   """How the "general public" sees a Document.
@@ -82,4 +84,4 @@
   doc.content = helper.templates.unescape(doc.content)
   context['document'] = doc
 
-  return helper.responses.respond(request, template, context)
+  return helper.responses.respond(request, template, context)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/views/helper/decorators.py	Thu Oct 16 23:31:57 2008 +0000
@@ -0,0 +1,50 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Views decorators.
+"""
+
+__authors__ = [
+  '"Pawel Solyga" <pawel.solyga@gmail.com>',
+  ]
+
+
+import logging
+
+from functools import wraps
+
+from google.appengine.runtime import DeadlineExceededError
+
+from django import http
+
+
+def view(func):
+  """Decorator that insists that exceptions are handled by view."""
+  @wraps(func)
+  def view_wrapper(request, *args, **kwds):
+    try:
+      return func(request, *args, **kwds)
+    except DeadlineExceededError:
+      logging.exception('DeadlineExceededError')
+      return http.HttpResponse('DeadlineExceededError')
+    except MemoryError:
+      logging.exception('MemoryError')
+      return http.HttpResponse('MemoryError')
+    except AssertionError:
+      logging.exception('AssertionError')
+      return http.HttpResponse('AssertionError')
+
+  return view_wrapper
\ No newline at end of file
--- a/app/soc/views/helper/requests.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/helper/requests.py	Thu Oct 16 23:31:57 2008 +0000
@@ -21,6 +21,7 @@
   '"Todd Larsen" <tlarsen@google.com>',
   ]
 
+
 import urlparse
 
 
--- a/app/soc/views/helper/responses.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/helper/responses.py	Thu Oct 16 23:31:57 2008 +0000
@@ -23,10 +23,7 @@
   ]
 
 
-import logging
-
 from google.appengine.api import users
-from google.appengine.runtime import DeadlineExceededError
 
 from django import http
 from django.template import loader
@@ -67,19 +64,9 @@
   if response_args is None:
     response_args = {}
 
-  try:
-    response_args['content'] = response_args.get(
-        'content', loader.render_to_string(template, dictionary=context))
-    return http.HttpResponse(**response_args)
-  except DeadlineExceededError:
-    logging.exception('DeadlineExceededError')
-    return http.HttpResponse('DeadlineExceededError')
-  except MemoryError:
-    logging.exception('MemoryError')
-    return http.HttpResponse('MemoryError')
-  except AssertionError:
-    logging.exception('AssertionError')
-    return http.HttpResponse('AssertionError')
+  response_args['content'] = response_args.get(
+      'content', loader.render_to_string(template, dictionary=context))
+  return http.HttpResponse(**response_args)
 
 
 def getUniversalContext(request):
@@ -111,7 +98,8 @@
 
   if id:
     context['id'] = id
-    context['user'] = soc.logic.models.user.logic.getFromFields(email=id.email())
+    context['user'] = soc.logic.models.user.logic.getFromFields(
+        email=id.email())
     context['is_admin'] = id_user.isIdDeveloper(id=id)
 
   context['is_debug'] = system.isDebug()
--- a/app/soc/views/person/profile.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/person/profile.py	Thu Oct 16 23:31:57 2008 +0000
@@ -30,12 +30,12 @@
 
 from google.appengine.api import users
 
-from django import forms
 from django import http
 from django import shortcuts
 
 from soc.models import person
 from soc.views import helper
+from soc.views.helper import decorators
 
 import soc.views.helper.forms
 
@@ -54,6 +54,7 @@
     exclude = ['user']
 
 
+@decorators.view
 def edit(request, page=None, program=None, link_name=None,
          template='soc/person/profile/edit.html'):
   """View for a Person to modify the properties of a Person Model.
@@ -85,7 +86,7 @@
   #      display name to use in the greeting
 
   form = EditForm()
-  if request.method=='POST':
+  if request.method == 'POST':
     form = EditForm(request.POST)
 
     if not form.errors:
@@ -93,4 +94,4 @@
 
   return shortcuts.render_to_response(
       template, dictionary={'template': template, 'form': form, 'user': user,
-                            'program': program, 'link_name': link_name})
+                            'program': program, 'link_name': link_name})
\ No newline at end of file
--- a/app/soc/views/simple.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/simple.py	Thu Oct 16 23:31:57 2008 +0000
@@ -22,13 +22,13 @@
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
-from google.appengine.api import users
 
 from django.utils.translation import ugettext_lazy
 
 from soc.logic import out_of_band
 from soc.logic.site import id_user
 from soc.views import helper
+from soc.views.helper import decorators
 
 import soc.views.helper.responses
 import soc.views.helper.templates
@@ -36,6 +36,7 @@
 
 DEF_PUBLIC_TMPL = 'soc/base.html'
 
+@decorators.view
 def public(request, page=None, template=DEF_PUBLIC_TMPL, link_name=None,
            context=None):
   """A simple template view that expects a link_name extracted from the URL.
--- a/app/soc/views/site/docs/edit.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/docs/edit.py	Thu Oct 16 23:31:57 2008 +0000
@@ -36,6 +36,7 @@
 from soc.views import helper
 from soc.views import simple
 from soc.views.helper import access
+from soc.views.helper import decorators
 from soc.views.user import profile
 
 import soc.models.document
@@ -108,6 +109,7 @@
 
 DEF_SITE_DOCS_CREATE_TMPL = 'soc/site/docs/edit.html'
 
+@decorators.view
 def create(request, page=None, template=DEF_SITE_DOCS_CREATE_TMPL):
   """View for a Developer to create a new Document entity.
 
@@ -166,6 +168,7 @@
                                       required=False)
 
 
+@decorators.view
 def edit(request, page=None, partial_path=None, link_name=None,
          template=DEF_SITE_DOCS_EDIT_TMPL):
   """View for a Developer to modify the properties of a Document Model entity.
@@ -248,7 +251,7 @@
             'title': doc.title, 'partial_path': doc.partial_path,
             'link_name': doc.link_name, 'short_name': doc.short_name,
             'content': doc.content, 'author': doc.author,
-            'is_featured': doc.is_featured, 'created_by': author_link_name})       
+            'is_featured': doc.is_featured, 'created_by': author_link_name})
       else:
         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
           # redirect to aggressively remove 'Profile saved' query parameter
@@ -271,6 +274,7 @@
   return helper.responses.respond(request, template, context)
 
 
+@decorators.view
 def delete(request, page=None, partial_path=None, link_name=None,
            template=DEF_SITE_DOCS_EDIT_TMPL):
   """Request handler for a Developer to delete Document Model entity.
@@ -315,4 +319,4 @@
   if existing_doc:
     document.logic.delete(existing_doc)
 
-  return http.HttpResponseRedirect('/site/docs/list')
+  return http.HttpResponseRedirect('/site/docs/list')
\ No newline at end of file
--- a/app/soc/views/site/docs/list.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/docs/list.py	Thu Oct 16 23:31:57 2008 +0000
@@ -23,9 +23,9 @@
 
 
 from soc.logic.models import work
-from soc.views import simple
 from soc.views import helper
 from soc.views.helper import access
+from soc.views.helper import decorators
 
 import soc.logic
 import soc.models.document
@@ -37,6 +37,7 @@
 DEF_SITE_DOCS_LIST_ALL_TMPL = 'soc/site/docs/list/all.html'
 
 
+@decorators.view
 def all(request, page=None, template=DEF_SITE_DOCS_LIST_ALL_TMPL):
   """Show a list of all Documents (limit rows per page).
   
@@ -77,4 +78,4 @@
       request, context, docs, 
       offset=offset, limit=limit, list_templates=list_templates)
 
-  return helper.responses.respond(request, template, context)
+  return helper.responses.respond(request, template, context)
\ No newline at end of file
--- a/app/soc/views/site/home.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/home.py	Thu Oct 16 23:31:57 2008 +0000
@@ -25,11 +25,11 @@
   ]
 
 
-from google.appengine.api import users
 from google.appengine.ext import db
 
 from soc.logic import models
 from soc.views import helper
+from soc.views.helper import decorators
 
 import soc.logic.models.site_settings
 import soc.views.helper.responses
@@ -38,6 +38,7 @@
 
 DEF_SITE_HOME_PUBLIC_TMPL = 'soc/site/home/public.html'
 
+@decorators.view
 def public(request, page=None, template=DEF_SITE_HOME_PUBLIC_TMPL):
   """How the "general public" sees the Melange site home page.
 
@@ -69,4 +70,4 @@
       site_doc.content = helper.templates.unescape(site_doc.content)
       context['site_document'] = site_doc
 
-  return helper.responses.respond(request, template, context=context)
+  return helper.responses.respond(request, template, context=context)
\ No newline at end of file
--- a/app/soc/views/site/settings.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/settings.py	Thu Oct 16 23:31:57 2008 +0000
@@ -27,21 +27,16 @@
   ]
 
 
-from google.appengine.api import users
 from google.appengine.ext import db
 
 from django import forms
-from django import http
-from django import shortcuts
 
 from soc.logic import models
-from soc.logic import out_of_band
 from soc.logic import validate
 from soc.logic.models import document
-from soc.logic.site import id_user
-from soc.views import simple
 from soc.views import helper
 from soc.views.helper import access
+from soc.views.helper import decorators
 
 import soc.logic.models.site_settings
 import soc.models.document
@@ -95,6 +90,7 @@
 
 DEF_SITE_HOME_EDIT_TMPL = 'soc/site/settings/edit.html'
 
+@decorators.view
 def edit(request, page=None, template=DEF_SITE_HOME_EDIT_TMPL):
   """View for Developer to edit content of Melange site home page.
 
@@ -125,7 +121,7 @@
 
     if document_form.is_valid() and settings_form.is_valid():
       link_name = models.site_settings.logic.DEF_SITE_HOME_DOC_LINK_NAME
-      partial_path=models.site_settings.logic.DEF_SITE_SETTINGS_PATH
+      partial_path = models.site_settings.logic.DEF_SITE_SETTINGS_PATH
       logged_in_id = users.get_current_user()
       author = models.user.logic.getFromFields(email=logged_in_id.email())
 
@@ -179,4 +175,4 @@
   context.update({'document_form': document_form,
                   'settings_form': settings_form })
   
-  return helper.responses.respond(request, template, context)
+  return helper.responses.respond(request, template, context)
\ No newline at end of file
--- a/app/soc/views/site/sponsor/list.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/sponsor/list.py	Thu Oct 16 23:31:57 2008 +0000
@@ -23,10 +23,9 @@
 
 
 from soc.logic import models
-from soc.logic.models import sponsor
 from soc.views import helper
-from soc.views import simple
 from soc.views.helper import access
+from soc.views.helper import decorators
 
 import soc.logic
 import soc.models.sponsor as sponsor_model
@@ -37,6 +36,7 @@
 
 DEF_SITE_SPONSOR_LIST_ALL_TMPL = 'soc/group/list/all.html'
 
+@decorators.view
 def all(request, page=None, template=DEF_SITE_SPONSOR_LIST_ALL_TMPL):
   """Show a list of all Sponsors (limit rows per page).
   
@@ -82,4 +82,4 @@
                   'entity_type_plural': sponsor_model.Sponsor.GROUP_TYPE_PLURAL,
                   'entity_type_short': sponsor_model.Sponsor.GROUP_TYPE_SHORT})
 
-  return helper.responses.respond(request, template, context)
+  return helper.responses.respond(request, template, context)
\ No newline at end of file
--- a/app/soc/views/site/sponsor/profile.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/sponsor/profile.py	Thu Oct 16 23:31:57 2008 +0000
@@ -31,10 +31,10 @@
 from soc.logic import out_of_band
 from soc.logic import validate
 from soc.logic.models import sponsor
-from soc.logic.site import id_user
 from soc.views import helper
 from soc.views import simple
 from soc.views.helper import access
+from soc.views.helper import decorators
 from soc.views.user import profile
 
 import soc.logic
@@ -88,7 +88,9 @@
                           ' <a href="/site/sponsor/profile">Create ' \
                           'a New Sponsor</a> page.'
 
-def edit(request, page=None, link_name=None, template=DEF_SITE_SPONSOR_PROFILE_EDIT_TMPL):
+@decorators.view
+def edit(request, page=None, link_name=None,
+         template=DEF_SITE_SPONSOR_PROFILE_EDIT_TMPL):
   """View for a Developer to modify the properties of a Sponsor Model entity.
 
   Args:
@@ -152,7 +154,8 @@
         fields['founder'] = user
       
       form_ln = fields['link_name']
-      form_sponsor = models.sponsor.logic.updateOrCreateFromFields(fields, link_name=form_ln)
+      form_sponsor = models.sponsor.logic.updateOrCreateFromFields(
+          fields, link_name=form_ln)
       
       if not form_sponsor:
         return http.HttpResponseRedirect('/')
@@ -201,13 +204,16 @@
 
 DEF_SITE_SPONSOR_PROFILE_CREATE_TMPL = 'soc/group/profile/edit.html'
 
+@decorators.view
 def create(request, page=None, template=DEF_SITE_SPONSOR_PROFILE_CREATE_TMPL):
   """create() view is same as edit() view, but with no link_name supplied.
   """
   return edit(request, page, link_name=None, template=template)
 
 
-def delete(request, page=None, link_name=None, template=DEF_SITE_SPONSOR_PROFILE_EDIT_TMPL):
+@decorators.view
+def delete(request, page=None, link_name=None,
+           template=DEF_SITE_SPONSOR_PROFILE_EDIT_TMPL):
   """Request handler for a Developer to delete Sponsor Model entity.
 
   Args:
--- a/app/soc/views/site/user/list.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/user/list.py	Thu Oct 16 23:31:57 2008 +0000
@@ -23,10 +23,9 @@
 
 
 from soc.logic import models
-from soc.logic.site import id_user
 from soc.views import helper
-from soc.views import simple
 from soc.views.helper import access
+from soc.views.helper import decorators
 
 import soc.logic
 import soc.models.user
@@ -37,7 +36,7 @@
 
 DEF_SITE_USER_LIST_ALL_TMPL = 'soc/site/user/list/all.html'
 
-
+@decorators.view
 def all(request, page=None, template=DEF_SITE_USER_LIST_ALL_TMPL):
   """Show a list of all Users (limit rows per page).
   
@@ -78,5 +77,4 @@
       request, context, users,
       offset=offset, limit=limit, list_templates=list_templates)
 
-  return helper.responses.respond(request, template, context)
-                 
+  return helper.responses.respond(request, template, context)
\ No newline at end of file
--- a/app/soc/views/site/user/profile.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/site/user/profile.py	Thu Oct 16 23:31:57 2008 +0000
@@ -35,6 +35,7 @@
 from soc.views import simple
 from soc.views import helper
 from soc.views.helper import access
+from soc.views.helper import decorators
 from soc.views.user import profile
 
 import soc.logic
@@ -94,6 +95,7 @@
 
 DEF_SITE_USER_PROFILE_LOOKUP_TMPL = 'soc/site/user/profile/lookup.html'
 
+@decorators.view
 def lookup(request, page=None, template=DEF_SITE_USER_PROFILE_LOOKUP_TMPL):
   """View for a Developer to look up a User Model entity.
 
@@ -250,7 +252,9 @@
                           ' <a href="/site/user/profile">Create ' \
                           'a New User</a> page.'
 
-def edit(request, page=None, link_name=None, template=DEF_SITE_USER_PROFILE_EDIT_TMPL):
+@decorators.view
+def edit(request, page=None, link_name=None,
+         template=DEF_SITE_USER_PROFILE_EDIT_TMPL):
   """View for a Developer to modify the properties of a User Model entity.
 
   Args:
@@ -301,8 +305,6 @@
       
       user = models.user.logic.updateOrCreateFromKeyName(properties, key_name)
 
-      #raise forms.ValidationError("lesseee: " + new_link_name + " " +  user.link_name)
-
       if not user:
         return http.HttpResponseRedirect('/')
         
@@ -401,6 +403,7 @@
 
 DEF_SITE_CREATE_USER_PROFILE_TMPL = 'soc/site/user/profile/edit.html'
 
+@decorators.view
 def create(request, page=None, template=DEF_SITE_CREATE_USER_PROFILE_TMPL):
   """View for a Developer to create a new User Model entity.
 
@@ -438,7 +441,8 @@
         'is_developer': form.cleaned_data.get('is_developer'),
       }
 
-      user = models.user.logic.updateOrCreateFromFields(properties, email=form_id.email())
+      user = models.user.logic.updateOrCreateFromFields(properties, 
+                                                        email=form_id.email())
 
       if not user:
         return http.HttpResponseRedirect('/')
@@ -454,4 +458,4 @@
 
   context['form'] = form
 
-  return helper.responses.respond(request, template, context)
+  return helper.responses.respond(request, template, context)
\ No newline at end of file
--- a/app/soc/views/sponsor/profile.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/sponsor/profile.py	Thu Oct 16 23:31:57 2008 +0000
@@ -25,6 +25,7 @@
 from soc.logic import out_of_band
 from soc.views import helper
 from soc.views import simple
+from soc.views.helper import decorators
 
 import soc.logic.models.sponsor
 import soc.views.helper.responses
@@ -33,6 +34,7 @@
 
 DEF_SPONSOR_PUBLIC_TMPL = 'soc/group/profile/public.html'
 
+@decorators.view
 def public(request, page=None, link_name=None, 
            template=DEF_SPONSOR_PUBLIC_TMPL):
   """How the "general public" sees the Sponsor profile.
@@ -51,7 +53,8 @@
   context = helper.responses.getUniversalContext(request)
 
   try:
-    link_name_sponsor = soc.logic.models.sponsor.logic.getIfFields(link_name=link_name)
+    link_name_sponsor = soc.logic.models.sponsor.logic.getIfFields(
+        link_name=link_name)
   except out_of_band.ErrorResponse, error:
     # show custom 404 page when link name doesn't exist in Datastore
     return simple.errorResponse(request, page, error, template, context)
--- a/app/soc/views/user/profile.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/user/profile.py	Thu Oct 16 23:31:57 2008 +0000
@@ -26,7 +26,6 @@
 
 from django import forms
 from django import http
-from django import shortcuts
 from django.utils.translation import ugettext_lazy
 
 from soc.logic import models
@@ -35,6 +34,7 @@
 from soc.logic.site import id_user
 from soc.views import helper
 from soc.views import simple
+from soc.views.helper import decorators
 
 import soc.logic
 import soc.models.user
@@ -82,6 +82,7 @@
   SUBMIT_MSG_PARAM_NAME: SUBMIT_MSG_PROFILE_SAVED,
 }
 
+@decorators.view
 def edit(request, page=None, link_name=None, 
          template=DEF_USER_PROFILE_EDIT_TMPL):
   """View for a User to modify the properties of a User Model entity.
@@ -144,7 +145,8 @@
         'id': id,
       }
 
-      user = models.user.logic.updateOrCreateFromFields(properties, email=id.email())
+      user = models.user.logic.updateOrCreateFromFields(properties, 
+                                                        email=id.email())
 
       # redirect to new /user/profile/new_link_name?s=0
       # (causes 'Profile saved' message to be displayed)
@@ -183,7 +185,8 @@
   return helper.responses.respond(request, template, context)
 
 
+@decorators.view
 def create(request, page=None, template=DEF_USER_PROFILE_EDIT_TMPL):
   """create() view is same as edit() view, but with no link_name supplied.
   """
-  return edit(request, page, link_name=None, template=template)
+  return edit(request, page, link_name=None, template=template)
\ No newline at end of file
--- a/app/soc/views/user/roles.py	Thu Oct 16 23:26:58 2008 +0000
+++ b/app/soc/views/user/roles.py	Thu Oct 16 23:31:57 2008 +0000
@@ -26,13 +26,11 @@
   ]
 
 
-from google.appengine.api import users
-
-from django import http
-
-from soc.views.helpers import response_helpers
+from soc.views.helper import decorators
+from soc.views.helper import responses
 
 
+@decorators.view
 def dashboard(request, page=None, link_name=None,
               template='soc/user/roles/dashboard.html'):
   """A per-User dashboard of that User's Roles on the site.
@@ -58,10 +56,11 @@
   #   template, a corresponding foo/bar/public.html template must
   #   also exist...
 
-  return response_helpers.respond(request,
+  return responses.respond(request,
       template, {'template': template})
 
 
+@decorators.view
 def public(request, page=None, link_name=None,
            template='soc/user/roles/public.html'):
   """A "general public" view of a User's Roles on the site.
@@ -81,6 +80,5 @@
   # TODO: if link_name is empty or not a valid link_name on the site, display
   # some sort of "user does not exist" page (a custom 404 page, maybe?).
   
-  return response_helpers.respond(request,
-      template, {'template': template})
-
+  return responses.respond(request,
+      template, {'template': template})
\ No newline at end of file