18
18
"""Utility methods."""
21
22
import configuration
23
from django import http
24
from django import shortcuts
25
from django.core import urlresolvers
26
24
from google.appengine.api import memcache
27
from google.appengine.api import users
31
def respond(request, template, params=None):
32
"""Helper to render a response.
34
This function assumes that the user is logged in.
37
request: The request object
38
template: The template name; '.html' is appended automatically.
39
params: A dict giving the template parameters; modified in-place.
42
Whatever render_to_response(template, params) returns.
45
Whatever render_to_response(template, params) raises.
52
params['user'] = request.user
53
params['sign_out'] = users.CreateLogoutURL('/')
54
params['is_admin'] = users.is_current_user_admin()
56
params['sign_in'] = users.CreateLoginURL(request.path)
58
if hasattr(request, 'profile') and request.profile is not None:
59
profile = request.profile
60
params['sidebar'] = models.Sidebar.render(profile)
61
params['is_superuser'] = profile.is_superuser
63
params['is_superuser'] = False
64
params['sidebar'] = models.Sidebar.render(None)
66
params['configuration'] = configuration
68
if not template.endswith('.html'):
71
return shortcuts.render_to_response(template, params)
74
def forbidden(request, error_message=None):
75
"""Returns a 403 response based on a template.
78
request: the http request that was forbidden
79
error_message: a message to display that will override the default message
82
A http response with the status code of 403
85
response = respond(request, '403', {'error_message': error_message})
86
response.status_code = 403
90
def page_not_found(request, error_message=None):
91
"""Returns a 404 response based on a template.
94
request: the http request that was forbidden
95
error_message: a message to display that will override the default message
98
A http response with the status code of 404
100
response = respond(request, '404', {'error_message': error_message})
101
response.status_code = 404
105
def edit_updated_page(page_id, message_id='', tab_name=''):
106
"""Issues a redirect to the edit form for page_id.
109
page_id: the id of the page that is being edited
110
message_id: the id of the message element to be displayed to the user once
112
tab_name: the name of the tab to default to when the page is reloaded
115
A http redirect to the edit form for page_id
118
url = urlresolvers.reverse('views.admin.edit_page', args=[str(page_id)])
120
url = '%s?m=%s' % (url, message_id)
122
url = '%s#%s' % (url, tab_name)
123
return http.HttpResponseRedirect(url)
126
29
def memcache_get(key):
127
30
"""Gets data from the memcache.
149
52
logging.error('Failed to clear the cache!')
152
def edit_instance(request, model_type, model_form_type,
153
edit_template, success_url, object_id, **kwargs):
154
# pylint: disable-msg=R0913
155
"""Generic method to handle editing objects with Django forms.
158
request: the http request
159
model_type: the class of object being edited
160
model_form_type: the form type to use for editing this object
161
edit_template: the template to use for editing the object
162
success_url: the URL to redirect the user to when the editing is succesful
163
object_id: the ID of the object to edit, or None if creating a new object
164
kwargs: additional data to be passed to the edit form
167
A HTTP response, either a redirect to the success_url or the edit form.
174
type_instance = model_type.get_by_id(int(object_id))
175
if type_instance is None:
176
return http.HttpResponseNotFound('No object exists with key %r',
179
form = model_form_type(data=request.POST or None, instance=type_instance)
181
kwargs['form'] = form
182
kwargs['type_instance'] = type_instance
183
kwargs['editing'] = editing
186
return respond(request, edit_template, kwargs)
191
type_instance = form.save(commit=False)
192
except ValueError, err:
193
errors['__all__'] = unicode(err)
195
return respond(request, edit_template, kwargs)
197
if 'callback' in kwargs:
198
kwargs['callback'](type_instance, kwargs['params'])
202
return http.HttpResponseRedirect(success_url)
205
55
def set_up_data_store():
206
56
"""Function to initialize a new installation.