app/soc/views/simple.py
changeset 139 93a9a22a4c38
parent 125 155f43a0fa68
child 141 e120c24b89e2
equal deleted inserted replaced
138:e1167bdf71a4 139:93a9a22a4c38
    24 __authors__ = [
    24 __authors__ = [
    25   '"Todd Larsen" <tlarsen@google.com>',
    25   '"Todd Larsen" <tlarsen@google.com>',
    26   ]
    26   ]
    27 
    27 
    28 
    28 
    29 from django import http
    29 from django.utils.translation import ugettext_lazy
    30 from django.template import loader
       
    31 
    30 
    32 from soc.logic import out_of_band
    31 from soc.logic import out_of_band
    33 from soc.logic.site import id_user
    32 from soc.logic.site import id_user
    34 from soc.views.helpers import response_helpers
    33 from soc.views.helpers import response_helpers
    35 from soc.views.helpers import template_helpers
    34 from soc.views.helpers import template_helpers
   110   return response_helpers.respond(request, error_templates, context=context,
   109   return response_helpers.respond(request, error_templates, context=context,
   111                                   response_args=error.response_args)
   110                                   response_args=error.response_args)
   112 
   111 
   113 
   112 
   114 DEF_LOGIN_TMPL = 'soc/login.html'
   113 DEF_LOGIN_TMPL = 'soc/login.html'
   115 DEF_LOGIN_MSG_FMT = 'Please <a href="%(sign_in)s">sign in</a> to continue.'
   114 DEF_LOGIN_MSG_FMT = ugettext_lazy(
       
   115   'Please <a href="%(sign_in)s">sign in</a> to continue.')
   116 
   116 
   117 def requestLogin(request, template, context, login_message_fmt=None):
   117 def requestLogin(request, template, context, login_message_fmt=None):
   118   """Displays a login request page with custom message and login link.
   118   """Displays a login request page with custom message and login link.
   119   
   119   
   120   Args:
   120   Args:
   140     if not login_message_fmt:
   140     if not login_message_fmt:
   141       login_message_fmt = DEF_LOGIN_MSG_FMT
   141       login_message_fmt = DEF_LOGIN_MSG_FMT
   142     context['login_message'] = login_message_fmt % context  
   142     context['login_message'] = login_message_fmt % context  
   143   
   143   
   144   return response_helpers.respond(request, login_templates, context=context)
   144   return response_helpers.respond(request, login_templates, context=context)
       
   145 
       
   146 
       
   147 def getAltResponseIfNotLoggedIn(request, context=None,
       
   148                                 template=DEF_LOGIN_TMPL, id=None,
       
   149                                 login_message_fmt=DEF_LOGIN_MSG_FMT):
       
   150   """Returns an alternate HTTP response if Google Account is not logged in.
       
   151 
       
   152   Args:
       
   153     request: the standard django request object
       
   154     context: the context supplied to the template (implements dict)
       
   155     template: the "sibling" template (or a search list of such templates)
       
   156       from which to construct the public.html template name (or names)
       
   157     id: a Google Account (users.User) object, or None, in which case
       
   158       the current logged-in user is used
       
   159 
       
   160   Returns:
       
   161     None if id is logged in, or a subclass of django.http.HttpResponse
       
   162     which contains the alternate response that should be returned by the
       
   163     calling view.
       
   164   """
       
   165   id = id_user.getIdIfMissing(id)
       
   166 
       
   167   if id:
       
   168     # a Google Account is logged in, so no need to ask user to sign in  
       
   169     return None
       
   170 
       
   171   # if missing, create default template context for use with any templates
       
   172   context = response_helpers.getUniversalContext(request, context=context)
       
   173 
       
   174   return requestLogin(request, template, context,
       
   175                       login_message_fmt=login_message_fmt)
       
   176 
       
   177 
       
   178 DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
       
   179     'Please <a href="%(sign_in)s">sign in</a>'
       
   180     ' as a site developer to view this page.')
       
   181 
       
   182 DEF_DEF_LOGOUT_LOGIN_MSG_FMT = ugettext_lazy(
       
   183     'Please <a href="%(sign_out)s">sign out</a>'
       
   184     ' and <a href="%(sign_in)s">sign in</a>'
       
   185     ' again as a site developer to view this page.')
       
   186 
       
   187 def getAltResponseIfNotDeveloper(request, context=None,
       
   188                                  template=DEF_LOGIN_TMPL, id=None):
       
   189   """Returns an alternate HTTP response if Google Account is not a Developer.
       
   190 
       
   191   Args:
       
   192     request: the standard django request object
       
   193     context: the context supplied to the template (implements dict)
       
   194     template: the "sibling" template (or a search list of such templates)
       
   195       from which to construct the public.html template name (or names)
       
   196     id: a Google Account (users.User) object, or None, in which case
       
   197       the current logged-in user is used
       
   198 
       
   199   Returns:
       
   200     None if id is logged in and logged-in user is a Developer, or a
       
   201     subclass of django.http.HttpResponse which contains the alternate
       
   202     response that should be returned by the calling view.
       
   203   """
       
   204   id = id_user.getIdIfMissing(id)
       
   205 
       
   206   # if missing, create default template context for use with any templates
       
   207   context = response_helpers.getUniversalContext(request, context=context)
       
   208 
       
   209   if not id:
       
   210     return requestLogin(request, template, context,
       
   211         login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
       
   212 
       
   213   if not id_user.isIdDeveloper(id=id):
       
   214     return requestLogin(request, template, context,
       
   215         login_message_fmt=DEF_DEF_LOGOUT_LOGIN_MSG_FMT)
       
   216 
       
   217   # Google Account is logged in and is a Developer, so no need for sign in
       
   218   return None