app/soc/views/helper/responses.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Fri, 03 Apr 2009 17:43:41 +0000
changeset 2077 fd2e83a297c7
parent 1812 a0ab93cc3cd5
child 2333 221482a54238
permissions -rw-r--r--
Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper. Patch by: Pawel Solyga Reviewed by: to-be-reviewed
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>',
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1579
diff changeset
    23
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
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
    24
  ]
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
    25
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
    26
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
    27
from google.appengine.api import users
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
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
    32
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
    33
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
    34
from soc.logic.models import site
1526
5c31184594a5 Convert everything to use the new access methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1502
diff changeset
    35
from soc.logic.models.user import logic as user_logic
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
322
6641e941ef1e Fixed imports sorting based on comments for r751.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    41
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    42
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
    43
            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
    44
  """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
    45
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
    46
  Args:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    47
    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
    48
    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
    49
    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
    50
    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
    51
      (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
    52
      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
    53
    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
    54
      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
    55
      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
    56
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
    57
  Returns:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    58
    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
    59
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
  Raises:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    61
    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
    62
    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
    63
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    64
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    65
  if not context:
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1398
diff changeset
    66
    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
    67
    context = getUniversalContext(request)
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1398
diff changeset
    68
    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
    69
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    70
  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
    71
    response_args = {}
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    72
1812
a0ab93cc3cd5 Strip leading and trailing newlines from our responses
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1797
diff changeset
    73
  if 'content' not in response_args:
a0ab93cc3cd5 Strip leading and trailing newlines from our responses
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1797
diff changeset
    74
    content = loader.render_to_string(template, dictionary=context)
a0ab93cc3cd5 Strip leading and trailing newlines from our responses
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1797
diff changeset
    75
    response_args['content'] = content.strip('\n')
a0ab93cc3cd5 Strip leading and trailing newlines from our responses
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1797
diff changeset
    76
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    77
  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
    78
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    79
  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
    80
    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
    81
      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
    82
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1357
diff changeset
    83
  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
    84
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    85
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    86
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
    87
  """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
    88
  
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    89
  Args:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    90
    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
    91
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    92
  Returns:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    93
    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
    94
    
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    95
    {
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    96
      '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
    97
      '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
    98
      '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
    99
        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
   100
      '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
   101
      '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
   102
      '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
   103
      '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
   104
      '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
   105
    }
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   106
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   107
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1585
diff changeset
   108
  account = accounts.getCurrentAccount()
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   109
  user = None
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   110
  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
   111
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   112
  context = {}
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   113
  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
   114
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
   115
  if account:
1526
5c31184594a5 Convert everything to use the new access methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1502
diff changeset
   116
    user = user_logic.getForAccount(account)
5c31184594a5 Convert everything to use the new access methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1502
diff changeset
   117
    is_admin = user_logic.isDeveloper(account=account, user=user)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   118
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   119
  context['account'] = account
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   120
  context['user'] = user
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   121
  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
   122
1797
f0c83ca3bb08 isDebug now also checks melange version
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1600
diff changeset
   123
  context['is_local'] = system.isLocal()
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   124
  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
   125
  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
   126
  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
   127
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   128
  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
   129
1797
f0c83ca3bb08 isDebug now also checks melange version
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1600
diff changeset
   130
  context['gae_version'] = system.getAppVersion()
f0c83ca3bb08 isDebug now also checks melange version
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1600
diff changeset
   131
  context['soc_release'] = system.getMelangeVersion()
578
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   132
971
1d852b58b182 Use getSingleton in getUniversalContext
Sverre Rabbelier <srabbelier@gmail.com>
parents: 891
diff changeset
   133
  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
   134
1457
9fd31955cc49 Make use of site_name where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1454
diff changeset
   135
  context['ga_tracking_num'] = settings.ga_tracking_num
9fd31955cc49 Make use of site_name where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1454
diff changeset
   136
  context['gmaps_api_key'] = settings.gmaps_api_key
9fd31955cc49 Make use of site_name where possible
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1454
diff changeset
   137
  context['site_name'] = settings.site_name
1579
a06d60fcb23d Added a site-wide notice
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1526
diff changeset
   138
  context['site_notice'] = settings.site_notice
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)