app/soc/views/simple.py
changeset 272 00cea07656c0
parent 270 7dd6d8347b56
child 293 1edd01373e71
equal deleted inserted replaced
271:01e90bb21b7e 272:00cea07656c0
    26 from django.utils.translation import ugettext_lazy
    26 from django.utils.translation import ugettext_lazy
    27 
    27 
    28 from soc.logic import out_of_band
    28 from soc.logic import out_of_band
    29 from soc.logic.site import id_user
    29 from soc.logic.site import id_user
    30 from soc.views import helper
    30 from soc.views import helper
       
    31 import soc.views.helper.responses
    31 import soc.views.helper.templates
    32 import soc.views.helper.templates
    32 from soc.views.helpers import response_helpers
       
    33 
    33 
    34 
    34 
    35 def templateWithLinkName(request,
    35 def templateWithLinkName(request,
    36                          template='soc/base.html', linkname=None,
    36                          template='soc/base.html', linkname=None,
    37                          context=None):
    37                          context=None):
    50 
    50 
    51   Returns:
    51   Returns:
    52     A subclass of django.http.HttpResponse containing the generated page.
    52     A subclass of django.http.HttpResponse containing the generated page.
    53   """
    53   """
    54   context['linkname'] = linkname
    54   context['linkname'] = linkname
    55   context = response_helpers.getUniversalContext(request, context=context)
    55   context = helper.responses.getUniversalContext(request, context=context)
    56 
    56 
    57   try:
    57   try:
    58     context['linkname_user'] = id_user.getUserIfLinkName(linkname)
    58     context['linkname_user'] = id_user.getUserIfLinkName(linkname)
    59   except out_of_band.ErrorResponse, error:
    59   except out_of_band.ErrorResponse, error:
    60     return errorResponse(request, error, template, context)
    60     return errorResponse(request, error, template, context)
    61 
    61 
    62   return response_helpers.respond(request, template, context)
    62   return helper.responses.respond(request, template, context)
    63 
    63 
    64 
    64 
    65 def public(request, template, linkname, context):
    65 def public(request, template, linkname, context):
    66   """A convenience wrapper around templateWithLinkName() using 'public.html'.
    66   """A convenience wrapper around templateWithLinkName() using 'public.html'.
    67 
    67 
    93         (so supply a copy if such modification is not acceptable)
    93         (so supply a copy if such modification is not acceptable)
    94       error_message: the error message string from error.message
    94       error_message: the error message string from error.message
    95       error_status: error.response_args['status'], or None if a status code
    95       error_status: error.response_args['status'], or None if a status code
    96         was not supplied to the ErrorResponse
    96         was not supplied to the ErrorResponse
    97   """
    97   """
    98   context = response_helpers.getUniversalContext(request, context=context)
    98   context = helper.responses.getUniversalContext(request, context=context)
    99   
    99   
   100   # make a list of possible "sibling" templates, then append a default
   100   # make a list of possible "sibling" templates, then append a default
   101   error_templates = helper.templates.makeSiblingTemplatesList(
   101   error_templates = helper.templates.makeSiblingTemplatesList(
   102       template, 'error.html', default_template=DEF_ERROR_TMPL)
   102       template, 'error.html', default_template=DEF_ERROR_TMPL)
   103 
   103 
   104   context['error_status'] = error.response_args.get('status')
   104   context['error_status'] = error.response_args.get('status')
   105   context['error_message'] = error.message
   105   context['error_message'] = error.message
   106 
   106 
   107   return response_helpers.respond(request, error_templates, context=context,
   107   return helper.responses.respond(request, error_templates, context=context,
   108                                   response_args=error.response_args)
   108                                   response_args=error.response_args)
   109 
   109 
   110 
   110 
   111 DEF_LOGIN_TMPL = 'soc/login.html'
   111 DEF_LOGIN_TMPL = 'soc/login.html'
   112 DEF_LOGIN_MSG_FMT = ugettext_lazy(
   112 DEF_LOGIN_MSG_FMT = ugettext_lazy(
   126     context: the context dict supplied to the template, which is modified
   126     context: the context dict supplied to the template, which is modified
   127         (so supply a copy if such modification is not acceptable); 
   127         (so supply a copy if such modification is not acceptable); 
   128       login_message: the caller can completely construct the message supplied
   128       login_message: the caller can completely construct the message supplied
   129         to the login template in lieu of using login_message_fmt
   129         to the login template in lieu of using login_message_fmt
   130   """
   130   """
   131   context = response_helpers.getUniversalContext(request, context=context)
   131   context = helper.responses.getUniversalContext(request, context=context)
   132   
   132   
   133   # make a list of possible "sibling" templates, then append a default
   133   # make a list of possible "sibling" templates, then append a default
   134   login_templates = helper.templates.makeSiblingTemplatesList(
   134   login_templates = helper.templates.makeSiblingTemplatesList(
   135       template, 'login.html', default_template=DEF_LOGIN_TMPL)
   135       template, 'login.html', default_template=DEF_LOGIN_TMPL)
   136   
   136   
   137   if not context.get('login_message'):
   137   if not context.get('login_message'):
   138     if not login_message_fmt:
   138     if not login_message_fmt:
   139       login_message_fmt = DEF_LOGIN_MSG_FMT
   139       login_message_fmt = DEF_LOGIN_MSG_FMT
   140     context['login_message'] = login_message_fmt % context  
   140     context['login_message'] = login_message_fmt % context  
   141   
   141   
   142   return response_helpers.respond(request, login_templates, context=context)
   142   return helper.responses.respond(request, login_templates, context=context)
   143 
   143 
   144 
   144 
   145 def getAltResponseIfNotLoggedIn(request, context=None,
   145 def getAltResponseIfNotLoggedIn(request, context=None,
   146                                 template=DEF_LOGIN_TMPL, id=None,
   146                                 template=DEF_LOGIN_TMPL, id=None,
   147                                 login_message_fmt=DEF_LOGIN_MSG_FMT):
   147                                 login_message_fmt=DEF_LOGIN_MSG_FMT):
   165   if id:
   165   if id:
   166     # a Google Account is logged in, so no need to ask user to sign in  
   166     # a Google Account is logged in, so no need to ask user to sign in  
   167     return None
   167     return None
   168 
   168 
   169   # if missing, create default template context for use with any templates
   169   # if missing, create default template context for use with any templates
   170   context = response_helpers.getUniversalContext(request, context=context)
   170   context = helper.responses.getUniversalContext(request, context=context)
   171 
   171 
   172   return requestLogin(request, template, context,
   172   return requestLogin(request, template, context,
   173                       login_message_fmt=login_message_fmt)
   173                       login_message_fmt=login_message_fmt)
   174 
   174 
   175 DEF_NO_USER_LOGIN_MSG_FMT = ugettext_lazy(
   175 DEF_NO_USER_LOGIN_MSG_FMT = ugettext_lazy(
   198 
   198 
   199   if user_exist:
   199   if user_exist:
   200     return None
   200     return None
   201 
   201 
   202   # if missing, create default template context for use with any templates
   202   # if missing, create default template context for use with any templates
   203   context = response_helpers.getUniversalContext(request, context=context)
   203   context = helper.responses.getUniversalContext(request, context=context)
   204 
   204 
   205   return requestLogin(request, template, context,
   205   return requestLogin(request, template, context,
   206                       login_message_fmt=DEF_NO_USER_LOGIN_MSG_FMT)
   206                       login_message_fmt=DEF_NO_USER_LOGIN_MSG_FMT)
   207 
   207 
   208 DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
   208 DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
   232     response that should be returned by the calling view.
   232     response that should be returned by the calling view.
   233   """
   233   """
   234   id = id_user.getIdIfMissing(id)
   234   id = id_user.getIdIfMissing(id)
   235 
   235 
   236   # if missing, create default template context for use with any templates
   236   # if missing, create default template context for use with any templates
   237   context = response_helpers.getUniversalContext(request, context=context)
   237   context = helper.responses.getUniversalContext(request, context=context)
   238 
   238 
   239   if not id:
   239   if not id:
   240     return requestLogin(request, template, context,
   240     return requestLogin(request, template, context,
   241         login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
   241         login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
   242 
   242