#!/usr/bin/env python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""User (Model) query functions.
"""


from google.appengine.api import users
from google.appengine.ext import db

from soc.logic import accounts

from soc.models.user import User


def isFormerAccount(account):
  """Returns true if account is a former account of some User.
  """
  return User.all().filter('former_accounts', account).count() > 0


def forCurrentAccount():
  """Retrieves the user entity for the currently logged in account.

  Also Updates the user entity's unique identifier. getCurrentUser() should
  be favored over this method.

  If there is no user logged in, or they have no valid associated User
  entity, None is returned.
  """

  account = accounts.getCurrentAccount()

  if not account:
    return None

  user = forAccount(account)

  if user and not user.user_id and account.user_id():
    from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
    # update the user id that was added to GAE after Melange was launched
    try:
      user.user_id = account.user_id()
      user.put()
    except CapabilityDisabledError, e:
      # readonly mode, that's fine
      pass

  return user

def forCurrentUserId():
  """Retrieves the user entity for the currently logged in user id.

  If there is no user logged in, or they have no valid associated User
  entity, None is returned.
  """

  user_id = accounts.getCurrentUserId()

  if not user_id:
    return None

  user = forUserId(user_id)

  current_account = accounts.getCurrentAccount()
  if user and (str(user.account) != str(current_account)):
    # The account of the user has changed, we use this account to send system
    # emails to.
    try:
      user.account = current_account
      user.put()
    except CapabilityDisabledError, e:
      # readonly mode, that's fine
      pass

  return user

def current():
  """Retrieves the user entity for the currently logged in user.

  Returns:
    The User entity of the logged in user or None if not available.
  """
  # look up with the unique id first
  user = forCurrentUserId()

  if user:
    return user

  # look up using the account address thereby setting the unique id
  return forCurrentAccount()


def forAccount(account):
  """Retrieves the user entity for the specified account.

  If there is no user logged in, or they have no valid associated User
  entity, None is returned.
  """

  if not account:
    raise base.InvalidArgumentError("Missing argument 'account'")

  account = accounts.normalizeAccount(account)

  q = User.all()
  q.filter('account', account)
  q.filter('status', 'valid')
  return q.get()


def forUserId(user_id):
  """Retrieves the user entity for the specified user id.

  If there is no user logged in, or they have no valid associated User
  entity, None is returned.
  """

  if not user_id:
    raise base.InvalidArgumentError("Missing argument 'user_id'")

  q = User.all()
  q.filter('user_id', user_id)
  q.filter('status', 'valid')
  return q.get()


def isDeveloper(account=None, user=None):
  """Returns true iff the specified user is a Developer.

  Args:
    account: if not supplied, defaults to the current account
    user: if not specified, defaults to the current user
  """

  current = accounts.getCurrentAccount()

  if not account:
    # default account to the current logged in account
    account = current

  if account and (not user):
    # default user to the current logged in user
    user = forAccount(account)

  # pylint: disable=E1103
  if user and user.is_developer:
    return True

  if account and (account == current):
    return users.is_current_user_admin()


@@ -784,16 +784,63 @@
 t User%0A%0A
+MELANGE_DELETED_USER = 'melange_deleted_user'%0A%0A
 %0Adef isF
@@ -3420,33 +3420,32 @@
 returned.%0A  %22%22%22%0A
-%0A
   if not user_id
@@ -3460,33 +3460,29 @@
 ise 
-base.InvalidArgumentError
+exceptions.BadRequest
 (%22Mi
@@ -3507,32 +3507,37 @@
 er_id'%22)%0A%0A  q = 
+user.
 User.all()%0A  q.f
@@ -3645,16 +3645,20 @@
 ne, user
+_ent
 =None):%0A
@@ -3793,16 +3793,20 @@
     user
+_ent
 : if not
@@ -3852,17 +3852,16 @@
 r%0A  %22%22%22%0A
-%0A
   curren
@@ -4016,16 +4016,20 @@
 not user
+_ent
 ):%0A    #
@@ -4076,24 +4076,28 @@
 ser%0A    user
+_ent
  = forAccoun
@@ -4139,24 +4139,28 @@
 03%0A  if user
+_ent
  and user.is
@@ -4156,16 +4156,20 @@
 and user
+_ent
 .is_deve
@@ -4269,12 +4269,492 @@
 er_admin()%0A%0A
+%0Adef getOrCreateDummyMelangeDeletedUser():%0A  %22%22%22Fetches or creates the dummy melange deleted user entity.%0A  %22%22%22%0A  q = user.User.all().filter('link_id', MELANGE_DELETED_USER)%0A  user_ent = q.get()%0A%0A  # If the requested user does not exist, create one.%0A  if not user_ent:%0A    account = users.User(email=MELANGE_DELETED_USER)%0A    user_ent = user.User(%0A        key_name=MELANGE_DELETED_USER, account=account,%0A        link_id=MELANGE_DELETED_USER)%0A    user_ent.put()%0A%0A  return user_ent%0A

Implement a function to get or create a dummy user to represent deleted user entity.