Add a public() convenience wrapper around templateWithLinkName() for the common
authorTodd Larsen <tlarsen@google.com>
Fri, 29 Aug 2008 02:52:02 +0000
changeset 122 95d0d954fe0b
parent 121 50f0999aee66
child 123 45d10b6af158
Add a public() convenience wrapper around templateWithLinkName() for the common public.html case. Add a requestLogin() view. Patch by: Todd Larsen Review by: to-be-reviewed
app/soc/templates/soc/login.html
app/soc/views/simple.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/login.html	Fri Aug 29 02:52:02 2008 +0000
@@ -0,0 +1,38 @@
+{% extends "soc/base.html" %}
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+{% block page_title %}
+ {%if login_title %}
+  {{ login_title }}
+ {% else %}
+  Sign In Required
+ {% endif %}
+{% endblock %}
+{% block header_title %}
+ {%if login_header %}
+  {{ login_header }}
+ {% else %}
+  {% comment %}
+   Attempt to use login_title again if login_header is not defined.
+  {% endcomment %}
+  {%if login_title %}
+   {{ login_title }}
+  {% else %}
+   Sign In Required
+  {% endif %}
+ {% endif %}
+{% endblock %}
+{% block body %}
+<p>{{ login_message|safe }}</p>
+{% endblock %}
\ No newline at end of file
--- a/app/soc/views/simple.py	Fri Aug 29 00:00:34 2008 +0000
+++ b/app/soc/views/simple.py	Fri Aug 29 02:52:02 2008 +0000
@@ -65,6 +65,23 @@
   return response_helpers.respond(request, template, context)
 
 
+def public(request, template, linkname, context):
+  """A convenience wrapper around templateWithLinkName() using 'public.html'.
+
+  Args:
+    request, linkname, context: see templateWithLinkName()
+    template: the "sibling" template (or a search list of such templates)
+      from which to construct the public.html template name (or names)
+
+  Returns:
+    A subclass of django.http.HttpResponse containing the generated page.
+  """
+  return simple.templateWithLinkName(
+      request, linkname=linkname, context=context,
+      template=template_helpers.makeSiblingTemplatesList(
+          template, 'public.html'))
+
+
 DEF_ERROR_TMPL = 'soc/error.html'
 
 def errorResponse(request, error, template, context):
@@ -93,3 +110,37 @@
 
   return response_helpers.respond(request, error_templates, context=context,
                                   response_args=error.response_args)
+
+
+DEF_LOGIN_TMPL = 'soc/login.html'
+DEF_LOGIN_MSG_FMT = 'Please <a href="%(sign_in)s">sign in</a> to continue.'
+
+def requestLogin(request, template, context, login_message_fmt=None):
+  """Displays a login request page with custom message and login link.
+  
+  Args:
+    request: the standard Django HTTP request object
+    template: the "sibling" template (or a search list of such templates)
+      from which to construct the login.html template name (or names)
+    login_message_fmt: a custom message format string used to create a
+      message displayed on the login page; the format string can contain
+      named format specifiers for any of the keys in context, but should at
+      least contain %(sign_in)s
+    context: the context dict supplied to the template, which is modified
+        (so supply a copy if such modification is not acceptable); 
+      login_message: the caller can completely construct the message supplied
+        to the login template in lieu of using login_message_fmt
+  """
+  context = response_helpers.getUniversalContext(request, context=context)
+  
+  # make a list of possible "sibling" templates, then append a default
+  login_templates = template_helpers.makeSiblingTemplatesList(template,
+                                                              'login.html')
+  login_templates.append(DEF_LOGIN_TMPL)
+  
+  if not context.get('login_message'):
+    if not login_message_fmt:
+      login_message_fmt = DEF_LOGIN_MSG_FMT
+    context['login_message'] = login_message_fmt % context  
+  
+  return response_helpers.respond(request, login_templates, context=context)