Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Patch by: Pawel Solyga
--- a/app/soc/views/helper/responses.py Tue Dec 02 11:20:08 2008 +0000
+++ b/app/soc/views/helper/responses.py Tue Dec 02 17:14:18 2008 +0000
@@ -33,6 +33,7 @@
from soc.logic import system
from soc.logic.models import site
from soc.views import helper
+from soc.views.helper import templates
from soc.views.sitemap import sidebar
import soc.logic
@@ -136,3 +137,37 @@
path = helper.requests.replaceSuffix(request.path, old_suffix, new_suffix,
params=params)
return http.HttpResponseRedirect(path)
+
+
+def errorResponse(self, error, request, template=None, context=None):
+ """Creates an HTTP response from the soc.views.out_of_band.Error exception.
+
+ Args:
+ errror: a out_of_band.Error object
+ request: a Django HTTP request
+ template: the "sibling" template (or a search list of such templates)
+ from which to construct the actual template name (or names)
+ context: optional context dict supplied to the template, which is
+ modified (so supply a copy if such modification is not acceptable)
+ """
+ if not context:
+ context = error.context
+
+ if not context:
+ context = getUniversalContext(request)
+
+ if not template:
+ template = []
+
+ # make a list of possible "sibling" templates, then append a default
+ sibling_templates = templates.makeSiblingTemplatesList(template,
+ error.TEMPLATE_NAME, default_template=error.DEF_TEMPLATE)
+
+ context['status'] = error.response_args.get('status')
+
+ if not context.get('message'):
+ # supplied context did not explicitly override the message
+ context['message'] = self.message_fmt % context
+
+ return respond(request, sibling_templates, context=context,
+ response_args=error.response_args)
\ No newline at end of file
--- a/app/soc/views/models/base.py Tue Dec 02 11:20:08 2008 +0000
+++ b/app/soc/views/models/base.py Tue Dec 02 17:14:18 2008 +0000
@@ -27,12 +27,6 @@
from django import http
from django.utils.translation import ugettext_lazy
-import soc.logic
-import soc.logic.lists
-import soc.views.helper.lists
-import soc.views.helper.responses
-import soc.views.helper.params
-
from soc.logic import dicts
from soc.views import helper
from soc.views import out_of_band
@@ -40,6 +34,12 @@
from soc.views.helper import forms
from soc.views import sitemap
+import soc.logic
+import soc.logic.lists
+import soc.views.helper.lists
+import soc.views.helper.responses
+import soc.views.helper.params
+
class View(object):
"""Views for entity classes.
@@ -96,7 +96,7 @@
try:
access.checkAccess('public', request, rights=params['rights'])
except out_of_band.Error, error:
- return error.response(request)
+ return helper.responses.errorResponse(error, request)
# create default template context for use with any templates
context = helper.responses.getUniversalContext(request)
@@ -111,8 +111,8 @@
key_fields = self._logic.getKeyFieldsFromDict(kwargs)
entity = self._logic.getIfFields(key_fields)
except out_of_band.Error, error:
- return error.response(request, template=params['error_public'],
- context=context)
+ return helper.responses.errorResponse(
+ error, request, template=params['error_public'], context=context)
self._public(request, entity, context)
@@ -185,7 +185,7 @@
try:
access.checkAccess('edit', request, rights=params['rights'])
except out_of_band.Error, error:
- return error.response(request)
+ return helper.responses.errorResponse(error, request)
context = helper.responses.getUniversalContext(request)
context['page_name'] = page_name
@@ -202,8 +202,8 @@
'entity_type_lower' : params['name'].lower(),
'entity_type' : params['name'],
'create' : params['missing_redirect']})
- return error.response(request, template=params['error_public'],
- context=context)
+ return helper.responses.errorResponse(
+ error, request, template=params['error_public'], context=context)
if request.method == 'POST':
return self.editPost(request, entity, context, params)
@@ -361,7 +361,7 @@
try:
access.checkAccess('list', request, rights=params['rights'])
except out_of_band.Error, error:
- return error.response(request)
+ return helper.responses.errorResponse(error, request)
content = helper.lists.getListContent(request, params, self._logic, filter)
contents = [content]
@@ -425,7 +425,7 @@
try:
access.checkAccess('delete', request, rights=params['rights'])
except out_of_band.Error, error:
- return error.response(request)
+ return helper.responses.errorResponse(error, request)
# create default template context for use with any templates
context = helper.responses.getUniversalContext(request)
@@ -441,8 +441,8 @@
'entity_type_lower' : params['name'].lower(),
'entity_type' : params['name'],
'create' : params['missing_redirect']})
- return error.response(request, template=params['error_edit'],
- context=context)
+ return helper.responses.errorResponse(
+ error, request, template=params['error_edit'], context=context)
if not entity:
#TODO: Create a proper error page for this
--- a/app/soc/views/models/request.py Tue Dec 02 11:20:08 2008 +0000
+++ b/app/soc/views/models/request.py Tue Dec 02 17:14:18 2008 +0000
@@ -42,6 +42,8 @@
import soc.logic.models.request
import soc.logic.dicts
import soc.views.helper
+import soc.views.helper.lists
+import soc.views.helper.responses
import soc.views.helper.widgets
@@ -142,7 +144,7 @@
try:
access.checkAccess('listSelf', request, params['rights'])
except out_of_band.Error, error:
- return error.response(request)
+ return helper.responses.errorResponse(error, request)
# get the current user
properties = {'account': users.get_current_user()}
--- a/app/soc/views/models/role.py Tue Dec 02 11:20:08 2008 +0000
+++ b/app/soc/views/models/role.py Tue Dec 02 17:14:18 2008 +0000
@@ -35,6 +35,8 @@
from soc.views.models import user as user_view
import soc.models.request
+import soc.views.helper.lists
+import soc.views.helper.responses
import soc.views.helper.widgets
@@ -100,7 +102,7 @@
try:
access.checkAccess('invite', request, rights=params['rights'])
except out_of_band.Error, error:
- return error.response(request)
+ return helper.responses.errorResponse(error, request)
content = helper.lists.getListContent(request, params, user_logic.logic)
contents = [content]
--- a/app/soc/views/models/user_self.py Tue Dec 02 11:20:08 2008 +0000
+++ b/app/soc/views/models/user_self.py Tue Dec 02 17:14:18 2008 +0000
@@ -138,7 +138,8 @@
try:
access.checkAccess('editSelf', request, params['rights'])
except out_of_band.Error, error:
- return error.response(request, template=self.EDIT_SELF_TMPL)
+ return helper.responses.errorResponse(error, request,
+ template=self.EDIT_SELF_TMPL)
account = users.get_current_user()
properties = {'account': account}
@@ -165,8 +166,8 @@
msg = self.DEF_USER_ACCOUNT_INVALID_MSG_FMT % {
'email': account.email()}
error = out_of_band.Error(msg)
- return error.response(request, template=self.EDIT_SELF_TMPL,
- context=context)
+ return helper.responses.errorResponse(
+ error, request, template=self.EDIT_SELF_TMPL, context=context)
user = soc.logic.models.user.logic.updateOrCreateFromFields(
properties, {'link_id': new_link_id})
--- a/app/soc/views/out_of_band.py Tue Dec 02 11:20:08 2008 +0000
+++ b/app/soc/views/out_of_band.py Tue Dec 02 17:14:18 2008 +0000
@@ -25,11 +25,6 @@
from django.utils.translation import ugettext_lazy
-from soc.views import helper
-
-import soc.views.helper.responses
-import soc.views.helper.templates
-
class Error(Exception):
"""Base exception for out-of-band responses raised by logic or views.
@@ -45,46 +40,14 @@
the response() method, produces the message to display on the
response page; this can be a simple string containing *no* named
format specifiers
- context: see response()
+ context: see soc.views.helper.responses.errorResponse()
**response_args: keyword arguments that are supplied directly to
django.http.HttpResponse; the most commonly used is 'status' to
set the HTTP status code for the response
"""
self.message_fmt = message_fmt
- self._context = context
- self._response_args = response_args
-
- def response(self, request, template=None, context=None):
- """Creates an HTTP response from the OutOfBandResponse exception.
-
- Args:
- request: a Django HTTP request
- template: the "sibling" template (or a search list of such templates)
- from which to construct the actual template name (or names)
- context: optional context dict supplied to the template, which is
- modified (so supply a copy if such modification is not acceptable)
- """
- if not context:
- context = self._context
-
- if not context:
- context = helper.responses.getUniversalContext(request)
-
- if not template:
- template = []
-
- # make a list of possible "sibling" templates, then append a default
- templates = helper.templates.makeSiblingTemplatesList(template,
- self.TEMPLATE_NAME, default_template=self.DEF_TEMPLATE)
-
- context['status'] = self._response_args.get('status')
-
- if not context.get('message'):
- # supplied context did not explicitly override the message
- context['message'] = self.message_fmt % context
-
- return helper.responses.respond(request, templates, context=context,
- response_args=self._response_args)
+ self.context = context
+ self.response_args = response_args
class LoginRequest(Error):
@@ -100,9 +63,8 @@
"""Constructor used to set response message and HTTP response arguments.
Args:
- message_fmt: same as Error.__init__() message_fmt, with the addition
- of a default value of None, in which case self.DEF_LOGIN_MSG_FMT is
- used
+ message_fmt: same as Error.__init__() message_fmt, with the addition of
+ a default value of None, in which case self.DEF_LOGIN_MSG_FMT is used
**response_args: see Error.__init__()
"""
if not message_fmt: