Exemplo n.º 1
0
 def test_get_current_user_only_cookie_user(self):
   dummy_mail = '*****@*****.**'
   self.cookie_login(dummy_mail)
   self.oauth_logout()
   current_user = auth_utils.get_current_user()
   self.assertEqual(current_user.email(), dummy_mail)
   self.cookie_logout()
Exemplo n.º 2
0
def get_nickname(email, never_me=False, request=None):
  """Return a nickname for an email address.

  If 'never_me' is True, 'me' is not returned if 'email' belongs to the
  current logged in user. If 'request' is a HttpRequest, it is used to
  cache the nickname returned by models.Account.get_nickname_for_email().
  """
  if isinstance(email, users.User):
    email = email.email()
  if not never_me:
    if request is not None:
      user = request.user
    else:
      user = auth_utils.get_current_user()
    if user is not None and email == user.email():
      return 'me'

  if request is None:
    return models.Account.get_nickname_for_email(email)

  # _nicknames is injected into request as a cache.
  # TODO(maruel): Use memcache instead.
  # Access to a protected member _nicknames of a client class
  # pylint: disable=W0212
  if getattr(request, '_nicknames', None) is None:
    request._nicknames = {}
  if email in request._nicknames:
    return request._nicknames[email]
  result = models.Account.get_nickname_for_email(email)
  request._nicknames[email] = result
  return result
Exemplo n.º 3
0
 def test_get_current_user_only_cookie_user(self):
     dummy_mail = '*****@*****.**'
     self.cookie_login(dummy_mail)
     self.oauth_logout()
     current_user = auth_utils.get_current_user()
     self.assertEqual(current_user.email(), dummy_mail)
     self.cookie_logout()
Exemplo n.º 4
0
def get_nickname(email, never_me=False, request=None):
    """Return a nickname for an email address.

  If 'never_me' is True, 'me' is not returned if 'email' belongs to the
  current logged in user. If 'request' is a HttpRequest, it is used to
  cache the nickname returned by models.Account.get_nickname_for_email().
  """
    if isinstance(email, users.User):
        email = email.email()
    if not never_me:
        if request is not None:
            user = request.user
        else:
            user = auth_utils.get_current_user()
        if user is not None and email == user.email():
            return 'me'

    if request is None:
        return models.Account.get_nickname_for_email(email)

    # _nicknames is injected into request as a cache.
    # TODO(maruel): Use memcache instead.
    # Access to a protected member _nicknames of a client class
    # pylint: disable=W0212
    if getattr(request, '_nicknames', None) is None:
        request._nicknames = {}
    if email in request._nicknames:
        return request._nicknames[email]
    result = models.Account.get_nickname_for_email(email)
    request._nicknames[email] = result
    return result
Exemplo n.º 5
0
  def process_request(self, request):
    request.user = auth_utils.get_current_user()
    request.user_is_admin = auth_utils.is_current_user_admin()

    # Update the cached value of the current user's Account
    account = None
    if request.user is not None:
      account = models.Account.get_account_for_user(request.user)
    models.Account.current_user_account = account
Exemplo n.º 6
0
    def process_request(self, request):
        request.user = auth_utils.get_current_user()
        request.user_is_admin = auth_utils.is_current_user_admin()

        # Update the cached value of the current user's Account
        account = None
        if request.user is not None:
            account = models.Account.get_account_for_user(request.user)
        models.Account.current_user_account = account
Exemplo n.º 7
0
def show_user(email, arg=None, _autoescape=None, _memcache_results=None):
    """Render a link to the user's dashboard, with text being the nickname."""
    if isinstance(email, users.User):
        email = email.email()
    if not arg:
        user = auth_utils.get_current_user()
        if user is not None and email == user.email():
            return 'me'

    ret = get_link_for_user(email)

    return django.utils.safestring.mark_safe(ret)
Exemplo n.º 8
0
def show_user(email, arg=None, _autoescape=None, _memcache_results=None):
  """Render a link to the user's dashboard, with text being the nickname."""
  if isinstance(email, users.User):
    email = email.email()
  if not arg:
    user = auth_utils.get_current_user()
    if user is not None and email == user.email():
      return 'me'

  ret = get_link_for_user(email)

  return django.utils.safestring.mark_safe(ret)
Exemplo n.º 9
0
    def test_get_current_user_both_cookie_and_oauth_user(self):
        cookie_mail = '*****@*****.**'
        oauth_mail = '*****@*****.**'
        self.cookie_login(cookie_mail)
        self.oauth_login(oauth_mail)

        # Make sure the OAuth API still works
        oauth_user = oauth.get_current_user(EMAIL_SCOPE)
        self.assertEqual(oauth_user.email(), oauth_mail)

        # Make sure current user is the Cookie user in the case both are set
        current_user = auth_utils.get_current_user()
        self.assertEqual(current_user.email(), cookie_mail)
        self.cookie_logout()
Exemplo n.º 10
0
  def test_get_current_user_both_cookie_and_oauth_user(self):
    cookie_mail = '*****@*****.**'
    oauth_mail = '*****@*****.**'
    self.cookie_login(cookie_mail)
    self.oauth_login(oauth_mail)

    # Make sure the OAuth API still works
    oauth_user = oauth.get_current_user(EMAIL_SCOPE)
    self.assertEqual(oauth_user.email(), oauth_mail)

    # Make sure current user is the Cookie user in the case both are set
    current_user = auth_utils.get_current_user()
    self.assertEqual(current_user.email(), cookie_mail)
    self.cookie_logout()
Exemplo n.º 11
0
def show_users(email_list, arg=None):
    """Render list of links to each user's dashboard."""
    new_email_list = []
    for email in email_list:
        if isinstance(email, users.User):
            email = email.email()
        new_email_list.append(email)

    links = get_links_for_users(new_email_list)

    if not arg:
        user = auth_utils.get_current_user()
        if user is not None:
            links[user.email()] = 'me'

    return django.utils.safestring.mark_safe(', '.join(
        links[email] for email in email_list))
Exemplo n.º 12
0
def show_users(email_list, arg=None):
  """Render list of links to each user's dashboard."""
  new_email_list = []
  for email in email_list:
    if isinstance(email, users.User):
      email = email.email()
    new_email_list.append(email)

  links = get_links_for_users(new_email_list)

  if not arg:
    user = auth_utils.get_current_user()
    if user is not None:
      links[user.email()] = 'me'

  return django.utils.safestring.mark_safe(', '.join(
      links[email] for email in email_list))
Exemplo n.º 13
0
def show_reviewers(reviewer_list, arg=None):
  """Render list of links to each reviewer's dashboard with color."""

  email_list = []
  for reviewer, _approval in reviewer_list.items():
    email = reviewer
    if isinstance(email, users.User):
      email = email.email()
    email_list.append(email)

  links = get_links_for_users(email_list)

  if not arg:
    user = auth_utils.get_current_user()
    if user is not None:
      links[user.email()] = 'me'

  return django.utils.safestring.mark_safe(', '.join(
      format_approval_text(links[r], a) for r, a in reviewer_list.items()))
Exemplo n.º 14
0
def _RenderDiffInternal(old_buff, new_buff, ndigits, tag, frag_list,
                        do_ir_diff, old_dict, new_dict,
                        old_patch, new_patch,
                        old_snapshot, new_snapshot,
                        debug, request):
  """Helper for _TableRowGenerator()."""
  obegin = (intra_region_diff.BEGIN_TAG %
            intra_region_diff.COLOR_SCHEME['old']['match'])
  nbegin = (intra_region_diff.BEGIN_TAG %
            intra_region_diff.COLOR_SCHEME['new']['match'])
  oend = intra_region_diff.END_TAG
  nend = oend
  user = auth_utils.get_current_user()

  for i in xrange(len(old_buff)):
    tg = tag
    old_valid, old_lineno, old_out = old_buff[i]
    new_valid, new_lineno, new_out = new_buff[i]
    old_intra_diff, old_has_newline, old_debug_info = old_out
    new_intra_diff, new_has_newline, new_debug_info = new_out

    frags = frag_list[i]
    # Render left text column
    frags.append(_RenderDiffColumn(old_valid, tag, ndigits,
                                   old_lineno, obegin, oend, old_intra_diff,
                                   do_ir_diff, old_has_newline, 'old'))

    # Render right text column
    frags.append(_RenderDiffColumn(new_valid, tag, ndigits,
                                   new_lineno, nbegin, nend, new_intra_diff,
                                   do_ir_diff, new_has_newline, 'new'))

    # End rendering the first row
    frags.append('</tr>\n')

    if debug:
      frags.append('<tr>')
      if old_debug_info:
        frags.append('<td class="debug-info">%s</td>' %
                     old_debug_info.replace('\n', '<br>'))
      else:
        frags.append('<td></td>')
      if new_debug_info:
        frags.append('<td class="debug-info">%s</td>' %
                     new_debug_info.replace('\n', '<br>'))
      else:
        frags.append('<td></td>')
      frags.append('</tr>\n')

    if old_patch or new_patch:
      # Start rendering the second row
      if ((old_valid and old_lineno in old_dict) or
          (new_valid and new_lineno in new_dict)):
        tg += '_comment'
        frags.append('<tr class="inline-comments" name="hook">')
      else:
        frags.append('<tr class="inline-comments">')

      # Render left inline comments
      frags.append(_RenderInlineComments(old_valid, old_lineno, old_dict,
                                         user, old_patch, old_snapshot, 'old',
                                         request))

      # Render right inline comments
      frags.append(_RenderInlineComments(new_valid, new_lineno, new_dict,
                                         user, new_patch, new_snapshot, 'new',
                                         request))

      # End rendering the second row
      frags.append('</tr>\n')

    # Yield the combined fragments
    yield tg, ''.join(frags)
Exemplo n.º 15
0
    def test_get_current_user_success(self):
        oauth_mail = '*****@*****.**'
        self.oauth_login(oauth_mail)

        current_user = auth_utils.get_current_user()
        self.assertEqual(current_user.email(), oauth_mail)
Exemplo n.º 16
0
  def test_get_current_user_success(self):
    oauth_mail = '*****@*****.**'
    self.oauth_login(oauth_mail)

    current_user = auth_utils.get_current_user()
    self.assertEqual(current_user.email(), oauth_mail)
Exemplo n.º 17
0
 def test_get_current_user_no_users(self):
     self.cookie_logout()
     self.oauth_logout()
     self.assertIsNone(auth_utils.get_current_user())
Exemplo n.º 18
0
def _RenderDiffInternal(old_buff, new_buff, ndigits, tag, frag_list,
                        do_ir_diff, old_dict, new_dict, old_patch, new_patch,
                        old_snapshot, new_snapshot, debug, request):
    """Helper for _TableRowGenerator()."""
    obegin = (intra_region_diff.BEGIN_TAG %
              intra_region_diff.COLOR_SCHEME['old']['match'])
    nbegin = (intra_region_diff.BEGIN_TAG %
              intra_region_diff.COLOR_SCHEME['new']['match'])
    oend = intra_region_diff.END_TAG
    nend = oend
    user = auth_utils.get_current_user()

    for i in xrange(len(old_buff)):
        tg = tag
        old_valid, old_lineno, old_out = old_buff[i]
        new_valid, new_lineno, new_out = new_buff[i]
        old_intra_diff, old_has_newline, old_debug_info = old_out
        new_intra_diff, new_has_newline, new_debug_info = new_out

        frags = frag_list[i]
        # Render left text column
        frags.append(
            _RenderDiffColumn(old_valid, tag, ndigits, old_lineno, obegin,
                              oend, old_intra_diff, do_ir_diff,
                              old_has_newline, 'old'))

        # Render right text column
        frags.append(
            _RenderDiffColumn(new_valid, tag, ndigits, new_lineno, nbegin,
                              nend, new_intra_diff, do_ir_diff,
                              new_has_newline, 'new'))

        # End rendering the first row
        frags.append('</tr>\n')

        if debug:
            frags.append('<tr>')
            if old_debug_info:
                frags.append('<td class="debug-info">{0!s}</td>'.format(
                    old_debug_info.replace('\n', '<br>')))
            else:
                frags.append('<td></td>')
            if new_debug_info:
                frags.append('<td class="debug-info">{0!s}</td>'.format(
                    new_debug_info.replace('\n', '<br>')))
            else:
                frags.append('<td></td>')
            frags.append('</tr>\n')

        if old_patch or new_patch:
            # Start rendering the second row
            if ((old_valid and old_lineno in old_dict)
                    or (new_valid and new_lineno in new_dict)):
                tg += '_comment'
                frags.append('<tr class="inline-comments" name="hook">')
            else:
                frags.append('<tr class="inline-comments">')

            # Render left inline comments
            frags.append(
                _RenderInlineComments(old_valid, old_lineno, old_dict, user,
                                      old_patch, old_snapshot, 'old', request))

            # Render right inline comments
            frags.append(
                _RenderInlineComments(new_valid, new_lineno, new_dict, user,
                                      new_patch, new_snapshot, 'new', request))

            # End rendering the second row
            frags.append('</tr>\n')

        # Yield the combined fragments
        yield tg, ''.join(frags)
Exemplo n.º 19
0
 def test_get_current_user_no_users(self):
   self.cookie_logout()
   self.oauth_logout()
   self.assertIsNone(auth_utils.get_current_user())