app/soc/views/helper/responses.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 22 Feb 2009 10:32:24 +0000
changeset 1454 37eb949c3267
parent 1398 e6a11f0dba68
child 1457 9fd31955cc49
permissions -rw-r--r--
Add some missing useJavaScript calls Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
128
f6abf00efc62 Updated docs in response_helpers.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 119
diff changeset
    17
"""Helpers used to render response.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
  ]
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
    25
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
from google.appengine.api import users
863
261344661531 Fix missing import identified by pylint.
Todd Larsen <tlarsen@google.com>
parents: 855
diff changeset
    27
from google.appengine.ext import db
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    28
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
from django import http
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    30
from django.template import loader
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
578
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
    32
from soc import release
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    33
from soc.logic import accounts
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    34
from soc.logic import system
534
c31cfbf1a20f Replace HomeSettings with Presence Model class. Replace SiteSettings with
Todd Larsen <tlarsen@google.com>
parents: 513
diff changeset
    35
from soc.logic.models import site
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    36
from soc.views import helper
854
bb43b74c58f6 A better way to add site-wide ToS link to *all* pages on the site.
Todd Larsen <tlarsen@google.com>
parents: 702
diff changeset
    37
from soc.views.helper import redirects
646
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
    38
from soc.views.helper import templates
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    39
from soc.views.sitemap import sidebar
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    40
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    41
import soc.logic
309
7190b224c701 Made the existing code use the new soc.logic.models
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
    42
import soc.logic.models.user
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    43
import soc.views.helper.requests
309
7190b224c701 Made the existing code use the new soc.logic.models
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
    44
322
6641e941ef1e Fixed imports sorting based on comments for r751.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    45
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    46
def respond(request, template, context=None, response_args=None,
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    47
            response_headers=None):
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
  """Helper to render a response, passing standard stuff to the response.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    49
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
  Args:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    51
    request: the Django HTTP request object
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    52
    template: the template (or search list of templates) to render
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    53
    context: the context supplied to the template (implements dict)
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    54
    response_args: keyword arguments passed to http.HttpResponse()
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    55
      (response_args['content'] is created with
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    56
      render_to_string(template, dictionary=context) if it is not present)
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    57
    response_headers: optional dict containing HTTP response header names
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    58
      and corresponding values to set in the HttpResponse object before it
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    59
      is returned; default is None
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
  Returns:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    62
    django.shortcuts.render_to_response(template, context) results
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
  Raises:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    65
    Any exceptions that django.template.loader.render_to_string() or
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    66
    django.http.HttpResponse() might raise.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    68
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    69
  if not context:
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1398
diff changeset
    70
    from soc.views.helper import params
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    71
    context = getUniversalContext(request)
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1398
diff changeset
    72
    useJavaScript(context, params.DEF_JS_USES_LIST)
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    73
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    74
  if response_args is None:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    75
    response_args = {}
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    76
365
74dec172944e 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 359
diff changeset
    77
  response_args['content'] = response_args.get(
74dec172944e 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 359
diff changeset
    78
      'content', loader.render_to_string(template, dictionary=context))
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    79
  http_response = http.HttpResponse(**response_args)
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    80
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    81
  if response_headers:
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    82
    for key, value in response_headers.iteritems():
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    83
      http_response[key] = value
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    84
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    85
  return http_response
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    86
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    87
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    88
def getUniversalContext(request):
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    89
  """Constructs a template context dict will many common variables defined.
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    90
  
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    91
  Args:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    92
    request: the Django HTTP request object
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    93
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    94
  Returns:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    95
    a new context dict containing:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    96
    
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    97
    {
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    98
      'request': the Django HTTP request object passed in by the caller
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    99
      'account': the logged-in Google Account if there is one
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   100
      'user': the User entity corresponding to the Google Account in
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   101
        context['account']
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   102
      'is_admin': True if users.is_current_user_admin() is True
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   103
      'is_debug': True if system.isDebug() is True
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   104
      'sign_in': a Google Account login URL
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   105
      'sign_out': a Google Account logout URL
200
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   106
      'sidebar_menu_html': an HTML string that renders the sidebar menu
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   107
    }
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   108
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   109
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   110
  account = users.get_current_user()
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   111
  user = None
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   112
  is_admin = False
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   113
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   114
  context = {}
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   115
  context['request'] = request
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   116
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   117
  if account:
1181
6fb4134e1dfc Calling getForCurrentAccount where necessary.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1031
diff changeset
   118
    user = soc.logic.models.user.logic.getForCurrentAccount()
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   119
    is_admin = accounts.isDeveloper(account=account)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   120
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   121
  context['account'] = account
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   122
  context['user'] = user
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   123
  context['is_admin'] = is_admin
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   124
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   125
  context['is_debug'] = system.isDebug()
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   126
  context['sign_in'] = users.create_login_url(request.path)
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   127
  context['sign_out'] = users.create_logout_url(request.path)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   128
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   129
  context['sidebar_menu_items'] = sidebar.getSidebar(account, user)
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   130
578
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   131
  context['soc_release'] = release.RELEASE_TAG
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   132
  context['gae_version'] = system.getAppVersion()
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   133
971
1d852b58b182 Use getSingleton in getUniversalContext
Sverre Rabbelier <srabbelier@gmail.com>
parents: 891
diff changeset
   134
  settings = site.logic.getSingleton()
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   135
411
c8ac5c1161e2 Addresses comments to r866.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 405
diff changeset
   136
  if settings:
c8ac5c1161e2 Addresses comments to r866.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 405
diff changeset
   137
    context['ga_tracking_num'] = settings.ga_tracking_num
1031
9a163d82635d Add gmaps_api_key to context is settings entity exists.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1017
diff changeset
   138
    context['gmaps_api_key'] = settings.gmaps_api_key
891
3d40190f35b6 Move getToSLink() to soc.views.helper.redirects.getToSRedirect().
Todd Larsen <tlarsen@google.com>
parents: 863
diff changeset
   139
  context['tos_link'] = redirects.getToSRedirect(settings)
854
bb43b74c58f6 A better way to add site-wide ToS link to *all* pages on the site.
Todd Larsen <tlarsen@google.com>
parents: 702
diff changeset
   140
 
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   141
  return context
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   142
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   143
def useJavaScript(context, uses):
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   144
  """Updates the context for JavaScript usage.
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   145
  """
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   146
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   147
  for use in uses:
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   148
    context['uses_%s' % use] = True
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   149
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   150
def redirectToChangedSuffix(
179
4882d6c5630d Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 169
diff changeset
   151
    request, old_suffix, new_suffix=None, params=None):
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   152
  """Changes suffix of URL path and returns an HTTP redirect response.
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   153
  
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   154
  Args:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   155
    request: the Django HTTP request object; redirect path is derived from
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   156
      request.path
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
   157
    old_suffix, new_suffix, params:  see helper.requests.replaceSuffix()
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   158
      
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   159
  Returns:
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 493
diff changeset
   160
    a Django HTTP redirect response pointing to the altered path.
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   161
  """
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
   162
  path = helper.requests.replaceSuffix(request.path, old_suffix, new_suffix,
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
   163
                                       params=params)
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   164
  return http.HttpResponseRedirect(path)
646
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   165
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   166
648
4f879defb15b Brown paperbag fix in recent cycle breaking
Sverre Rabbelier <srabbelier@gmail.com>
parents: 646
diff changeset
   167
def errorResponse(error, request, template=None, context=None):
646
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   168
  """Creates an HTTP response from the soc.views.out_of_band.Error exception.
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   169
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   170
  Args:
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   171
    errror: a out_of_band.Error object
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   172
    request: a Django HTTP request
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   173
    template: the "sibling" template (or a search list of such templates)
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   174
      from which to construct the actual template name (or names)
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   175
    context: optional context dict supplied to the template, which is
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   176
      modified (so supply a copy if such modification is not acceptable)
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   177
  """
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   178
  if not context:
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   179
    context = error.context
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   180
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   181
  if not context:
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1398
diff changeset
   182
    from soc.views.helper import params
646
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   183
    context = getUniversalContext(request)
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1398
diff changeset
   184
    useJavaScript(context, params.DEF_JS_USES_LIST)
646
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   185
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   186
  if not template:
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   187
    template = []
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   188
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   189
  # make a list of possible "sibling" templates, then append a default
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   190
  sibling_templates = templates.makeSiblingTemplatesList(template,
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   191
      error.TEMPLATE_NAME, default_template=error.DEF_TEMPLATE)
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   192
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   193
  context['status'] = error.response_args.get('status')
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   194
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   195
  if not context.get('message'):
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   196
    # supplied context did not explicitly override the message
648
4f879defb15b Brown paperbag fix in recent cycle breaking
Sverre Rabbelier <srabbelier@gmail.com>
parents: 646
diff changeset
   197
    context['message'] = error.message_fmt % context
646
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   198
860e17e5118f 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 586
diff changeset
   199
  return respond(request, sibling_templates, context=context,
854
bb43b74c58f6 A better way to add site-wide ToS link to *all* pages on the site.
Todd Larsen <tlarsen@google.com>
parents: 702
diff changeset
   200
                 response_args=error.response_args)