app/soc/views/helper/responses.py
author Todd Larsen <tlarsen@google.com>
Wed, 21 Jan 2009 22:21:40 +0000
changeset 891 3d40190f35b6
parent 863 261344661531
child 971 1d852b58b182
permissions -rw-r--r--
Move getToSLink() to soc.views.helper.redirects.getToSRedirect(). Fixes a circular import problem that prevented the site-wide ToS link from being displayed by checkAgreesToSiteToS(). This is now completely implemented. Patch by: Todd Larsen Review 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
#
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
     3
# Copyright 2008 the Melange authors.
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
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
from google.appengine.api import users
863
261344661531 Fix missing import identified by pylint.
Todd Larsen <tlarsen@google.com>
parents: 855
diff changeset
    26
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
    27
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
    28
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
    29
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
    30
578
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
    31
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
    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
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    35
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
    36
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
    37
from soc.views.helper import templates
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    38
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
    39
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    40
import soc.logic
309
7190b224c701 Made the existing code use the new soc.logic.models
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
    41
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
    42
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
    43
322
6641e941ef1e Fixed imports sorting based on comments for r751.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    44
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    45
def respond(request, template, context=None, response_args=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
    46
  """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
    47
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
  Args:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    49
    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
    50
    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
    51
    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
    52
    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
    53
      (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
    54
      render_to_string(template, dictionary=context) if it is not present)
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
    55
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
  Returns:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    57
    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
    58
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
  Raises:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    60
    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
    61
    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
    62
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    63
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    64
  if not context:
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    65
    context = 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
    66
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    67
  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
    68
    response_args = {}
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    69
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
    70
  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
    71
      'content', loader.render_to_string(template, dictionary=context))
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
    72
  return http.HttpResponse(**response_args)
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    73
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    74
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    75
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
    76
  """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
    77
  
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    78
  Args:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    79
    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
    80
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    81
  Returns:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    82
    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
    83
    
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
      '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
    86
      '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
    87
      '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
    88
        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
    89
      '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
    90
      '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
    91
      '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
    92
      '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
    93
      '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
    94
    }
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    95
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    96
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 = users.get_current_user()
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    98
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
    99
  context = {}
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   100
  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
   101
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
   102
  if account:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   103
    context['account'] = account
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 445
diff changeset
   104
    context['user'] = soc.logic.models.user.logic.getForFields(
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
   105
        {'account': account}, unique=True)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   106
    context['is_admin'] = accounts.isDeveloper(account=account)
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   107
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 276
diff changeset
   108
  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
   109
  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
   110
  context['sign_out'] = users.create_logout_url(request.path)
586
a4a36b06a870 Make the sidebar dynamic depending on the user's rights
Sverre Rabbelier <srabbelier@gmail.com>
parents: 585
diff changeset
   111
  context['sidebar_menu_items'] = sidebar.getSidebar(request)
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   112
578
0f34318f5693 Add release tag string, Melange "badge" image, and Google App Engine version
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   113
  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
   114
  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
   115
702
e3b91b6955ff Remove bogus scope from Site
Sverre Rabbelier <srabbelier@gmail.com>
parents: 648
diff changeset
   116
  settings = site.logic.getFromFields(link_id=site.logic.DEF_SITE_LINK_ID)
405
f3525c1288ed Add Google Analytics support to Site Settings. The reason I created additional SettingsValidationForm is because you cannot inherit from Form that has already defined Meta class, so it's sort of workaround for that. I didn't want to have same validation functions in both Form classes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 365
diff changeset
   117
  
411
c8ac5c1161e2 Addresses comments to r866.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 405
diff changeset
   118
  if settings:
c8ac5c1161e2 Addresses comments to r866.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 405
diff changeset
   119
    context['ga_tracking_num'] = settings.ga_tracking_num
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
   120
 
891
3d40190f35b6 Move getToSLink() to soc.views.helper.redirects.getToSRedirect().
Todd Larsen <tlarsen@google.com>
parents: 863
diff changeset
   121
  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
   122
 
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   123
  return context
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   124
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   125
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   126
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
   127
    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
   128
  """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
   129
  
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   130
  Args:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   131
    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
   132
      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
   133
    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
   134
      
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   135
  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
   136
    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
   137
  """
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
   138
  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
   139
                                       params=params)
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   140
  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
   141
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
   142
648
4f879defb15b Brown paperbag fix in recent cycle breaking
Sverre Rabbelier <srabbelier@gmail.com>
parents: 646
diff changeset
   143
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
   144
  """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
   145
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
   146
  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
   147
    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
   148
    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
   149
    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
   150
      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
   151
    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
   152
      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
   153
  """
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
   154
  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
   155
    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
   156
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
   157
  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
   158
    context = getUniversalContext(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
   159
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
   160
  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
   161
    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
   162
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
   163
  # 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
   164
  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
   165
      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
   166
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
   167
  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
   168
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
  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
   170
    # 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
   171
    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
   172
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
  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
   174
                 response_args=error.response_args)