app/soc/views/simple.py
changeset 295 651d9eabdef7
parent 293 1edd01373e71
child 299 a1cc853a56e5
equal deleted inserted replaced
294:1fdaab4a6ef2 295:651d9eabdef7
   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 helper.responses.respond(request, login_templates, context=context)
   142   return helper.responses.respond(request, login_templates, context=context)
   143 
   143 
   144 
       
   145 def getAltResponseIfNotLoggedIn(request, context=None,
       
   146                                 template=DEF_LOGIN_TMPL, id=None,
       
   147                                 login_message_fmt=DEF_LOGIN_MSG_FMT):
       
   148   """Returns an alternate HTTP response if Google Account is not logged in.
       
   149 
       
   150   Args:
       
   151     request: the standard django request object
       
   152     context: the context supplied to the template (implements dict)
       
   153     template: the "sibling" template (or a search list of such templates)
       
   154       from which to construct the public.html template name (or names)
       
   155     id: a Google Account (users.User) object, or None, in which case
       
   156       the current logged-in user is used
       
   157 
       
   158   Returns:
       
   159     None if id is logged in, or a subclass of django.http.HttpResponse
       
   160     which contains the alternate response that should be returned by the
       
   161     calling view.
       
   162   """
       
   163   id = id_user.getIdIfMissing(id)
       
   164 
       
   165   if id:
       
   166     # a Google Account is logged in, so no need to ask user to sign in  
       
   167     return None
       
   168 
       
   169   # if missing, create default template context for use with any templates
       
   170   context = helper.responses.getUniversalContext(request, context=context)
       
   171 
       
   172   return requestLogin(request, template, context,
       
   173                       login_message_fmt=login_message_fmt)
       
   174 
       
   175 DEF_NO_USER_LOGIN_MSG_FMT = ugettext_lazy(
       
   176     'Please create <a href="/user/profile">User Profile</a>'
       
   177     ' in order to view this page.')
       
   178 
       
   179 def getAltResponseIfNotUser(request, context=None,
       
   180                                 template=DEF_LOGIN_TMPL, id=None,
       
   181                                 login_message_fmt=DEF_LOGIN_MSG_FMT):
       
   182   """Returns an alternate HTTP response if Google Account has no User entity.
       
   183 
       
   184   Args:
       
   185     request: the standard django request object
       
   186     context: the context supplied to the template (implements dict)
       
   187     template: the "sibling" template (or a search list of such templates)
       
   188       from which to construct the public.html template name (or names)
       
   189     id: a Google Account (users.User) object, or None, in which case
       
   190       the current logged-in user is used
       
   191 
       
   192   Returns:
       
   193     None if User exists for id, or a subclass of django.http.HttpResponse
       
   194     which contains the alternate response that should be returned by the
       
   195     calling view.
       
   196   """
       
   197   user_exist = id_user.isIdUser(id)
       
   198 
       
   199   if user_exist:
       
   200     return None
       
   201 
       
   202   # if missing, create default template context for use with any templates
       
   203   context = helper.responses.getUniversalContext(request, context=context)
       
   204 
       
   205   return requestLogin(request, template, context,
       
   206                       login_message_fmt=DEF_NO_USER_LOGIN_MSG_FMT)
       
   207 
       
   208 DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
       
   209     'Please <a href="%(sign_in)s">sign in</a>'
       
   210     ' as a site developer to view this page.')
       
   211 
       
   212 DEF_DEV_LOGOUT_LOGIN_MSG_FMT = ugettext_lazy(
       
   213     'Please <a href="%(sign_out)s">sign out</a>'
       
   214     ' and <a href="%(sign_in)s">sign in</a>'
       
   215     ' again as a site developer to view this page.')
       
   216 
       
   217 def getAltResponseIfNotDeveloper(request, context=None,
       
   218                                  template=DEF_LOGIN_TMPL, id=None):
       
   219   """Returns an alternate HTTP response if Google Account is not a Developer.
       
   220 
       
   221   Args:
       
   222     request: the standard django request object
       
   223     context: the context supplied to the template (implements dict)
       
   224     template: the "sibling" template (or a search list of such templates)
       
   225       from which to construct the public.html template name (or names)
       
   226     id: a Google Account (users.User) object, or None, in which case
       
   227       the current logged-in user is used
       
   228 
       
   229   Returns:
       
   230     None if id is logged in and logged-in user is a Developer, or a
       
   231     subclass of django.http.HttpResponse which contains the alternate
       
   232     response that should be returned by the calling view.
       
   233   """
       
   234   id = id_user.getIdIfMissing(id)
       
   235 
       
   236   # if missing, create default template context for use with any templates
       
   237   context = helper.responses.getUniversalContext(request, context=context)
       
   238 
       
   239   if not id:
       
   240     return requestLogin(request, template, context,
       
   241         login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
       
   242 
       
   243   if not id_user.isIdDeveloper(id=id):
       
   244     return requestLogin(request, template, context,
       
   245         login_message_fmt=DEF_DEV_LOGOUT_LOGIN_MSG_FMT)
       
   246 
       
   247   # Google Account is logged in and is a Developer, so no need for sign in
       
   248   return None