16 |
16 |
17 """Helpers used to display various views that are forms. |
17 """Helpers used to display various views that are forms. |
18 """ |
18 """ |
19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
|
21 '"Todd Larsen" <tlarsen@google.com>', |
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
22 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
22 ] |
23 ] |
23 |
24 |
24 import os |
|
25 |
25 |
26 from google.appengine.api import users |
26 from google.appengine.api import users |
|
27 |
27 from django import http |
28 from django import http |
28 from django import shortcuts |
29 from django import shortcuts |
29 |
|
30 IS_DEV = os.environ['SERVER_SOFTWARE'].startswith('Dev') |
|
31 |
30 |
32 # DeadlineExceededError can live in two different places |
31 # DeadlineExceededError can live in two different places |
33 try: |
32 try: |
34 # When deployed |
33 # When deployed |
35 from google.appengine.runtime import DeadlineExceededError |
34 from google.appengine.runtime import DeadlineExceededError |
36 except ImportError: |
35 except ImportError: |
37 # In the development server |
36 # In the development server |
38 from google.appengine.runtime.apiproxy_errors import DeadlineExceededError |
37 from google.appengine.runtime.apiproxy_errors import DeadlineExceededError |
39 |
38 |
40 def respond(request, template, params=None): |
39 from soc.logic import system |
|
40 from soc.logic.site import id_user |
|
41 |
|
42 |
|
43 def respond(request, template, context=None): |
41 """Helper to render a response, passing standard stuff to the response. |
44 """Helper to render a response, passing standard stuff to the response. |
42 |
45 |
43 Args: |
46 Args: |
44 request: The request object. |
47 request: the Django HTTP request object |
45 template: The template name; '.html' is appended automatically. |
48 template: the template (or search list of templates) to render |
46 params: A dict giving the template parameters; modified in-place. |
49 context: the context supplied to the template (implements dict) |
47 |
50 |
48 Returns: |
51 Returns: |
49 Whatever render_to_response(template, params) returns. |
52 django.shortcuts.render_to_response(template, context) results |
50 |
53 |
51 Raises: |
54 Raises: |
52 Whatever render_to_response(template, params) raises. |
55 Whatever django.shortcuts.render_to_response(template, context) raises. |
53 """ |
56 """ |
54 if params is None: |
57 context = getUniversalContext(request, context=context) |
55 params = {} |
58 |
56 |
|
57 params['request'] = request |
|
58 params['id'] = users.get_current_user() |
|
59 params['is_admin'] = users.is_current_user_admin() |
|
60 params['is_dev'] = IS_DEV |
|
61 params['sign_in'] = users.create_login_url(request.path) |
|
62 params['sign_out'] = users.create_logout_url(request.path) |
|
63 try: |
59 try: |
64 return shortcuts.render_to_response(template, params) |
60 return shortcuts.render_to_response(template, context) |
65 except DeadlineExceededError: |
61 except DeadlineExceededError: |
66 logging.exception('DeadlineExceededError') |
62 logging.exception('DeadlineExceededError') |
67 return http.HttpResponse('DeadlineExceededError') |
63 return http.HttpResponse('DeadlineExceededError') |
68 except MemoryError: |
64 except MemoryError: |
69 logging.exception('MemoryError') |
65 logging.exception('MemoryError') |
70 return http.HttpResponse('MemoryError') |
66 return http.HttpResponse('MemoryError') |
71 except AssertionError: |
67 except AssertionError: |
72 logging.exception('AssertionError') |
68 logging.exception('AssertionError') |
73 return http.HttpResponse('AssertionError') |
69 return http.HttpResponse('AssertionError') |
|
70 |
|
71 |
|
72 def getUniversalContext(request, context=None): |
|
73 """Constructs a template context dict will many common variables defined. |
|
74 |
|
75 Args: |
|
76 request: the Django HTTP request object |
|
77 context: the template context dict to be updated in-place (pass in a copy |
|
78 if the original must not be modified), or None if a new one is to be |
|
79 created; any existing fields already present in the context dict passed |
|
80 in by the caller are left unaltered |
|
81 |
|
82 Returns: |
|
83 updated template context dict supplied by the caller, or a new context |
|
84 dict if the caller supplied None |
|
85 |
|
86 { |
|
87 'request': the Django HTTP request object passed in by the caller |
|
88 'id': the logged-in Google Account if there is one |
|
89 'user': the User entity corresponding to the Google Account in |
|
90 context['id'] |
|
91 'is_admin': True if users.is_current_user_admin() is True |
|
92 'is_debug': True if system.isDebug() is True |
|
93 'sign_in': a Google Account login URL |
|
94 'sign_out': a Google Account logout URL |
|
95 } |
|
96 """ |
|
97 if context is None: |
|
98 context = {} |
|
99 |
|
100 # set some universal values if caller did not already set them |
|
101 context['request'] = context.get('request', request) |
|
102 context['id'] = id_user.getIdIfMissing(context.get('id', None)) |
|
103 context['user'] = id_user.getUserIfMissing(context.get('user', None), |
|
104 context['id']) |
|
105 context['is_admin'] = context.get( |
|
106 'is_admin', users.is_current_user_admin()) |
|
107 context['is_debug'] = context.get('is_debug', system.isDebug()) |
|
108 context['sign_in'] = context.get( |
|
109 'sign_in', users.create_login_url(request.path)) |
|
110 context['sign_out'] = context.get( |
|
111 'sign_out', users.create_logout_url(request.path)) |
|
112 |
|
113 return context |