def post(self):
      # Yes, who is it?
      uid = self.request.get('uid')
      now = datetime.datetime.now()

      # First update user's 'last_ping'.
      user_query = app_admin.User.gql(
                          'WHERE ANCESTOR IS :1 AND uid = :2',
                          app_admin.user_key(),
                          uid
                   )

      if user_query.count() != 1:
         self.response.out.write('Ooops!!')
      else:
         user = user_query[0]
         user.last_ping = now
         user.put()

      # Ditch users who did not ping for more more than NO_PING_TIMEOUT.
      deadline = now - app_admin.NO_PING_TIMEOUT
      ditched_users_query = app_admin.User.gql(
                               'WHERE ANCESTOR IS :1 AND last_ping < :2',
                               app_admin.user_key(),
                               deadline
                            )
      db.delete(ditched_users_query)
 

      dot = os.path.dirname(__file__)

      if user_is_inside(uid):
         # Bingo! You've been lucky or patient. Welcome inside.
         toilet_query = app_admin.ToiletSeat.gql(
                              'WHERE ANCESTOR IS :1 ',
                               app_admin.toilet_key()
                         )
         msg = CLEAN if toilet_query[0].clean else DIRTY
         template_path = os.path.join(dot, 'content', 'inside_content.html')
         template_values = {
            'state': msg,
         }
         content = template.render(template_path, template_values)
         self.response.out.write(content)
      else:
         # There is still someone else inside. See you next ping...
         self.response.out.write(open(os.path.join(
               dot,
               'content',
               'busy_content.html'
         )).read())
def identify_from_cookie(request_handler):
    """Return the 'uid' attribute from cookie if properly defined and
   in the data-base (up-to-date), or False otherwise."""

    try:
        return app_admin.User.gql(
            'WHERE ANCESTOR IS :1 AND uid = :2', app_admin.user_key(),
            request_handler.request.cookies.get('uid'))[0].uid
    except (KeyError, IndexError):
        # No cookie or no longer in the data-base, respectively.
        return False
def user_is_inside(uid):
   """Return 'True' if user has earliest 'first_ping' attribute and
   is still up-to-date, False otherwise."""

   try:
      return app_admin.User.gql(
                   'WHERE ANCESTOR IS :1 ' \
                 + 'ORDER BY first_ping ASC LIMIT 1',
                    app_admin.user_key()
             )[0].uid == uid
   except IndexError:
      return False
def user_is_inside(uid):
    """Return 'True' if user has earliest 'first_ping' attribute and
   is still up-to-date, False otherwise."""

    try:
        return app_admin.User.gql(
                     'WHERE ANCESTOR IS :1 ' \
                   + 'ORDER BY first_ping ASC LIMIT 1',
                      app_admin.user_key()
               )[0].uid == uid
    except IndexError:
        return False
    def post(self):
        # Yes, who is it?
        uid = self.request.get('uid')
        now = datetime.datetime.now()

        # First update user's 'last_ping'.
        user_query = app_admin.User.gql('WHERE ANCESTOR IS :1 AND uid = :2',
                                        app_admin.user_key(), uid)

        if user_query.count() != 1:
            self.response.out.write('Ooops!!')
        else:
            user = user_query[0]
            user.last_ping = now
            user.put()

        # Ditch users who did not ping for more more than NO_PING_TIMEOUT.
        deadline = now - app_admin.NO_PING_TIMEOUT
        ditched_users_query = app_admin.User.gql(
            'WHERE ANCESTOR IS :1 AND last_ping < :2', app_admin.user_key(),
            deadline)
        db.delete(ditched_users_query)

        dot = os.path.dirname(__file__)

        if user_is_inside(uid):
            # Bingo! You've been lucky or patient. Welcome inside.
            toilet_query = app_admin.ToiletSeat.gql('WHERE ANCESTOR IS :1 ',
                                                    app_admin.toilet_key())
            msg = CLEAN if toilet_query[0].clean else DIRTY
            template_path = os.path.join(dot, 'content', 'inside_content.html')
            template_values = {
                'state': msg,
            }
            content = template.render(template_path, template_values)
            self.response.out.write(content)
        else:
            # There is still someone else inside. See you next ping...
            self.response.out.write(
                open(os.path.join(dot, 'content', 'busy_content.html')).read())
def identify_from_cookie(request_handler):
   """Return the 'uid' attribute from cookie if properly defined and
   in the data-base (up-to-date), or False otherwise."""

   try:
      return app_admin.User.gql(
                   'WHERE ANCESTOR IS :1 AND uid = :2',
                   app_admin.user_key(),
                   request_handler.request.cookies.get('uid')
             )[0].uid
   except (KeyError, IndexError):
      # No cookie or no longer in the data-base, respectively.
      return False