app/soc/views/simple.py
changeset 141 e120c24b89e2
parent 139 93a9a22a4c38
child 270 7dd6d8347b56
equal deleted inserted replaced
140:c3d098d6fafa 141:e120c24b89e2
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14 # See the License for the specific language governing permissions and
    14 # See the License for the specific language governing permissions and
    15 # limitations under the License.
    15 # limitations under the License.
    16 
    16 
    17 """Simple views that depend entirely on the template and context.
    17 """Simple views that depend entirely on the template and context.
    18 
       
    19 simpleWithLinkName: a simple template view for URLs with a linkname
       
    20 
       
    21 errorResponse: renders an out_of_band.ErrorResponse page
       
    22 """
    18 """
    23 
    19 
    24 __authors__ = [
    20 __authors__ = [
    25   '"Todd Larsen" <tlarsen@google.com>',
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    26   ]
    23   ]
    27 
    24 
    28 
    25 
    29 from django.utils.translation import ugettext_lazy
    26 from django.utils.translation import ugettext_lazy
    30 
    27 
   172   context = response_helpers.getUniversalContext(request, context=context)
   169   context = response_helpers.getUniversalContext(request, context=context)
   173 
   170 
   174   return requestLogin(request, template, context,
   171   return requestLogin(request, template, context,
   175                       login_message_fmt=login_message_fmt)
   172                       login_message_fmt=login_message_fmt)
   176 
   173 
       
   174 DEF_NO_USER_LOGIN_MSG_FMT = ugettext_lazy(
       
   175     'Please create <a href="/user/profile">User Profile</a>'
       
   176     ' in order to view this page.')
       
   177 
       
   178 def getAltResponseIfNotUser(request, context=None,
       
   179                                 template=DEF_LOGIN_TMPL, id=None,
       
   180                                 login_message_fmt=DEF_LOGIN_MSG_FMT):
       
   181   """Returns an alternate HTTP response if Google Account has no User entity.
       
   182 
       
   183   Args:
       
   184     request: the standard django request object
       
   185     context: the context supplied to the template (implements dict)
       
   186     template: the "sibling" template (or a search list of such templates)
       
   187       from which to construct the public.html template name (or names)
       
   188     id: a Google Account (users.User) object, or None, in which case
       
   189       the current logged-in user is used
       
   190 
       
   191   Returns:
       
   192     None if User exists for id, or a subclass of django.http.HttpResponse
       
   193     which contains the alternate response that should be returned by the
       
   194     calling view.
       
   195   """
       
   196   user_exist = id_user.isIdUser(id)
       
   197 
       
   198   if user_exist:
       
   199     return None
       
   200 
       
   201   # if missing, create default template context for use with any templates
       
   202   context = response_helpers.getUniversalContext(request, context=context)
       
   203 
       
   204   return requestLogin(request, template, context,
       
   205                       login_message_fmt=DEF_NO_USER_LOGIN_MSG_FMT)
   177 
   206 
   178 DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
   207 DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
   179     'Please <a href="%(sign_in)s">sign in</a>'
   208     'Please <a href="%(sign_in)s">sign in</a>'
   180     ' as a site developer to view this page.')
   209     ' as a site developer to view this page.')
   181 
   210 
   182 DEF_DEF_LOGOUT_LOGIN_MSG_FMT = ugettext_lazy(
   211 DEF_DEV_LOGOUT_LOGIN_MSG_FMT = ugettext_lazy(
   183     'Please <a href="%(sign_out)s">sign out</a>'
   212     'Please <a href="%(sign_out)s">sign out</a>'
   184     ' and <a href="%(sign_in)s">sign in</a>'
   213     ' and <a href="%(sign_in)s">sign in</a>'
   185     ' again as a site developer to view this page.')
   214     ' again as a site developer to view this page.')
   186 
   215 
   187 def getAltResponseIfNotDeveloper(request, context=None,
   216 def getAltResponseIfNotDeveloper(request, context=None,
   210     return requestLogin(request, template, context,
   239     return requestLogin(request, template, context,
   211         login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
   240         login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
   212 
   241 
   213   if not id_user.isIdDeveloper(id=id):
   242   if not id_user.isIdDeveloper(id=id):
   214     return requestLogin(request, template, context,
   243     return requestLogin(request, template, context,
   215         login_message_fmt=DEF_DEF_LOGOUT_LOGIN_MSG_FMT)
   244         login_message_fmt=DEF_DEV_LOGOUT_LOGIN_MSG_FMT)
   216 
   245 
   217   # Google Account is logged in and is a Developer, so no need for sign in
   246   # Google Account is logged in and is a Developer, so no need for sign in
   218   return None
   247   return None