~oubiwann/adytum-collection/Projects

« back to all changes in this revision

Viewing changes to MacGregorSite/cms/utility.py

  • Committer: oubiwann
  • Date: 2009-03-20 04:32:44 UTC
  • Revision ID: svn-v4:a5ecbb21-ded8-0310-8b19-a7ee6de54fb5:Projects:880
* Moved admin security code into a security module.
* Moved response and page not found into new view publisher module.
* Moved edit_instance and edit_updated_page into admin module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Utility methods."""
19
19
 
20
20
import logging
 
21
 
21
22
import configuration
22
23
 
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
 
25
 
28
26
import models
29
27
 
30
28
 
31
 
def respond(request, template, params=None):
32
 
  """Helper to render a response.
33
 
 
34
 
  This function assumes that the user is logged in.
35
 
 
36
 
  Args:
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.
40
 
 
41
 
  Returns:
42
 
    Whatever render_to_response(template, params) returns.
43
 
 
44
 
  Raises:
45
 
    Whatever render_to_response(template, params) raises.
46
 
 
47
 
  """
48
 
  if params is None:
49
 
    params = {}
50
 
 
51
 
  if request.user:
52
 
    params['user'] = request.user
53
 
    params['sign_out'] = users.CreateLogoutURL('/')
54
 
    params['is_admin'] = users.is_current_user_admin()
55
 
  else:
56
 
    params['sign_in'] = users.CreateLoginURL(request.path)
57
 
 
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
62
 
  else:
63
 
    params['is_superuser'] = False
64
 
    params['sidebar'] = models.Sidebar.render(None)
65
 
    
66
 
  params['configuration'] = configuration
67
 
 
68
 
  if not template.endswith('.html'):
69
 
    template += '.html'
70
 
 
71
 
  return shortcuts.render_to_response(template, params)
72
 
 
73
 
 
74
 
def forbidden(request, error_message=None):
75
 
  """Returns a 403 response based on a template.
76
 
 
77
 
  Args:
78
 
    request: the http request that was forbidden
79
 
    error_message: a message to display that will override the default message
80
 
 
81
 
  Returns:
82
 
    A http response with the status code of 403
83
 
 
84
 
  """
85
 
  response = respond(request, '403', {'error_message': error_message})
86
 
  response.status_code = 403
87
 
  return response
88
 
 
89
 
 
90
 
def page_not_found(request, error_message=None):
91
 
  """Returns a 404 response based on a template.
92
 
 
93
 
  Args:
94
 
    request: the http request that was forbidden
95
 
    error_message: a message to display that will override the default message
96
 
 
97
 
  Returns:
98
 
    A http response with the status code of 404
99
 
  """
100
 
  response = respond(request, '404', {'error_message': error_message})
101
 
  response.status_code = 404
102
 
  return response
103
 
 
104
 
 
105
 
def edit_updated_page(page_id, message_id='', tab_name=''):
106
 
  """Issues a redirect to the edit form for page_id.
107
 
 
108
 
  Args:
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
111
 
                the page is reloaded
112
 
    tab_name: the name of the tab to default to when the page is reloaded
113
 
 
114
 
  Returns:
115
 
    A http redirect to the edit form for page_id
116
 
 
117
 
  """
118
 
  url = urlresolvers.reverse('views.admin.edit_page', args=[str(page_id)])
119
 
  if message_id:
120
 
    url = '%s?m=%s' % (url, message_id)
121
 
  if tab_name:
122
 
    url = '%s#%s' % (url, tab_name)
123
 
  return http.HttpResponseRedirect(url)
124
 
 
125
 
 
126
29
def memcache_get(key):
127
30
  """Gets data from the memcache.
128
31
 
149
52
    logging.error('Failed to clear the cache!')
150
53
 
151
54
 
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.
156
 
 
157
 
  Args:
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
165
 
 
166
 
  Returns:
167
 
    A HTTP response, either a redirect to the success_url or the edit form.
168
 
 
169
 
  """
170
 
  editing = False
171
 
  type_instance = None
172
 
  if object_id:
173
 
    editing = True
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',
177
 
                                       object_id)
178
 
 
179
 
  form = model_form_type(data=request.POST or None, instance=type_instance)
180
 
 
181
 
  kwargs['form'] = form
182
 
  kwargs['type_instance'] = type_instance
183
 
  kwargs['editing'] = editing
184
 
 
185
 
  if not request.POST:
186
 
    return respond(request, edit_template, kwargs)
187
 
 
188
 
  errors = form.errors
189
 
  if not errors:
190
 
    try:
191
 
      type_instance = form.save(commit=False)
192
 
    except ValueError, err:
193
 
      errors['__all__'] = unicode(err)
194
 
  if errors:
195
 
    return respond(request, edit_template, kwargs)
196
 
 
197
 
  if 'callback' in kwargs:
198
 
    kwargs['callback'](type_instance, kwargs['params'])
199
 
 
200
 
  type_instance.put()
201
 
 
202
 
  return http.HttpResponseRedirect(success_url)
203
 
 
204
 
 
205
55
def set_up_data_store():
206
56
  """Function to initialize a new installation.
207
57
 
216
66
  root.put()
217
67
  return root
218
68
 
 
69
 
219
70
def get_domain(request):
220
71
    return request.META["SERVER_NAME"]