Ejemplo n.º 1
0
def get_users_by_page_by_order(account, offset, limit, order_by, asc = "ASC"):
  """
  Retrieve chunks of users from users table by specifying offset
  """
  logging.info("Retrieving users for account: " + account.email + " and ordering by: " + order_by)
  users = Users.gql("WHERE accountRef = :1 ORDER BY " + order_by + " " + asc, account).fetch(limit, offset)
  
  return users
Ejemplo n.º 2
0
def get_users_by_page_by_order(account, offset, limit, order_by, asc="ASC"):
    """
  Retrieve chunks of users from users table by specifying offset
  """
    logging.info("Retrieving users for account: " + account.email +
                 " and ordering by: " + order_by)
    users = Users.gql("WHERE accountRef = :1 ORDER BY " + order_by + " " + asc,
                      account).fetch(limit, offset)

    return users
Ejemplo n.º 3
0
def get_entity(key_name, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    e = memcache.get(key=key_name, namespace=ent_type)
    if e:
        try:
            e = deserialize(e)
        except:
            logging.error(
                "Memcache_db: Unable to deserialize entity of type %s" %
                ent_type)
            e = None

    if not e:
        memcache.delete(key=key_name, namespace=ent_type)
        if ent_type == "Accounts":
            e = Accounts.get_by_key_name(key_name)
        elif ent_type == "Badges":
            e = Badges.get_by_key_name(key_name)
        elif ent_type == "BadgeInstance":
            e = BadgeInstance.get_by_key_name(key_name)
        elif ent_type == "BadgeImage":
            e = BadgeImage.get_by_key_name(key_name)
        elif ent_type == "Users":
            e = Users.get_by_key_name(key_name)
        elif ent_type == "TrophyCase":
            e = TrophyCase.get_by_key_name(key_name)
        elif ent_type == "Points":
            e = Points.get_by_key_name(key_name)
        elif ent_type == "Notifier":
            e = Notifier.get_by_key_name(key_name)
        elif ent_type == "Rank":
            e = Rank.get_by_key_name(key_name)
        elif ent_type == "PassPhrase":
            e = PassPhrase.get_by_key_name(key_name)
        elif ent_type == "Milestones":
            e = Milestones.get_by_key_name(key_name)
        elif ent_type == "Leaderboard":
            e = Leaderboard.get_by_key_name(key_name)
        else:
            raise Exception()
        if e:
            memcache.add(key=key_name,
                         value=str(serialize(e)),
                         namespace=ent_type)
    return e
Ejemplo n.º 4
0
def __batch_get_entity(key_names, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    es = memcache.get_multi(keys=key_names, namespace=ent_type)
    ents = []
    db_ents = {}
    for key in key_names:
        e = None
        if key in es:
            try:
                e = deserialize(e)
                ents.append(e)
            except Exception, ex:
                logging.error(
                    "Memcache_db: Unable to deserialize entity of type %s with %s"
                    % (ent_type, str(ex)))
                e = None
        if not e:
            # These puts are in a loop, making this function slow
            memcache.delete(key=key, namespace=ent_type)
            if ent_type == "Accounts":
                dbent = Accounts.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "Badges":
                dbent = Badges.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "BadgeInstance":
                dbent = BadgeInstance.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "BadgeImage":
                dbent = BadgeImage.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "Users":
                dbent = Users.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "TrophyCases":
                dbent = TrophyCases.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            else:
                raise Exception()
Ejemplo n.º 5
0
def create_new_user(account_id, user_id):
    """
  Create a new user entity and save
  Look up account with account_id and use in reference when creating new user 
  """
    account_entity = memcache_db.get_entity(account_id, "Accounts")

    user_key = get_user_key(account_id, user_id)
    new_user = Users(key_name=user_key,
                     userid=user_id,
                     isEnabled="yes",
                     accountRef=account_entity)

    try:
        memcache_db.save_entity(new_user, user_key)
    except:
        logging.error("Error saving new user entity")
        return
Ejemplo n.º 6
0
def __batch_get_entity(key_names, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  es = memcache.get_multi(keys=key_names, namespace=ent_type)
  ents = []
  db_ents = {}
  for key in key_names:
    e = None
    if key in es:
      try:
        e = deserialize(e) 
        ents.append(e)
      except Exception, ex:
        logging.error("Memcache_db: Unable to deserialize entity of type %s with %s"%(ent_type, str(ex)))
        e = None
    if not e:
      # These puts are in a loop, making this function slow
      memcache.delete(key=key, namespace=ent_type)
      if ent_type == "Accounts":
        dbent = Accounts.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "Badges":
        dbent = Badges.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "BadgeInstance":
        dbent = BadgeInstance.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "BadgeImage":
        dbent = BadgeImage.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "Users":
        dbent = Users.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "TrophyCases":
        dbent = TrophyCases.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      else:
        raise Exception()
Ejemplo n.º 7
0
def get_entity(key_name, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  e = memcache.get(key=key_name, namespace=ent_type)
  if e:
    try:
      e = deserialize(e)
    except:
      logging.error("Memcache_db: Unable to deserialize entity of type %s"%ent_type)
      e = None 

  if not e:
    memcache.delete(key=key_name, namespace=ent_type)
    if ent_type == "Accounts":
      e = Accounts.get_by_key_name(key_name)
    elif ent_type == "Badges":
      e = Badges.get_by_key_name(key_name)
    elif ent_type == "BadgeInstance":
      e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "BadgeImage":
      e = BadgeImage.get_by_key_name(key_name)
    elif ent_type == "Users":
      e = Users.get_by_key_name(key_name)
    elif ent_type == "TrophyCase":
      e = TrophyCase.get_by_key_name(key_name)
    elif ent_type == "Points":
      e = Points.get_by_key_name(key_name)
    elif ent_type == "Notifier":
      e = Notifier.get_by_key_name(key_name)
    elif ent_type == "Rank":
      e = Rank.get_by_key_name(key_name)
    elif ent_type == "PassPhrase":
      e = PassPhrase.get_by_key_name(key_name)
    elif ent_type == "Milestones":
      e = Milestones.get_by_key_name(key_name)
    elif ent_type == "Leaderboard":
      e = Leaderboard.get_by_key_name(key_name)
    else:
      raise Exception()
    if e:
      memcache.add(key=key_name,value=str(serialize(e)),namespace=ent_type)
  return e
Ejemplo n.º 8
0
def is_in_db(key_name, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  e = None
  if ent_type == "Accounts":
    e = Accounts.get_by_key_name(key_name)
  elif ent_type == "Badges":
    e = Badges.get_by_key_name(key_name)
  elif ent_type == "BadgeInstance":
    e = BadgeInstance.get_by_key_name(key_name)
  elif ent_type == "BadgeImage":
    e = BadgeInstance.get_by_key_name(key_name)
  elif ent_type == "Users":
    e = Users.get_by_key_name(key_name)
  elif ent_type == "TrophyCases":
    e = TrophyCase.get_by_key_name(key_name)
  else:
    raise Exception()

  if e: return True
  else: return False
Ejemplo n.º 9
0
def is_in_db(key_name, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    e = None
    if ent_type == "Accounts":
        e = Accounts.get_by_key_name(key_name)
    elif ent_type == "Badges":
        e = Badges.get_by_key_name(key_name)
    elif ent_type == "BadgeInstance":
        e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "BadgeImage":
        e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "Users":
        e = Users.get_by_key_name(key_name)
    elif ent_type == "TrophyCases":
        e = TrophyCase.get_by_key_name(key_name)
    else:
        raise Exception()

    if e: return True
    else: return False
Ejemplo n.º 10
0
    def post(self):
        current_session = Session().get_current_session(self)
        account_entity = current_session.get_account_entity()
        email = account_entity.email
        new_user_id = self.request.get("id")
        if new_user_id == constants.ANONYMOUS_USER:
            self.redirect('/adminconsole/users?error=NoUserID')
            return
        profile_name = self.request.get("name")
        profile_link = self.request.get("profile")
        profile_img = self.request.get("image")
        user_key = users_dao.get_user_key(email, new_user_id)

        new_user = Users(key_name=user_key,
                         userid=new_user_id,
                         isEnabled="yes",
                         accountRef=account_entity,
                         profileName=profile_name,
                         profileLink=profile_link,
                         profileImg=profile_img)
        users_dao.save_user(new_user, user_key)
        self.redirect('/adminconsole/users')
Ejemplo n.º 11
0
def get_or_create_user(account_id, user_id, acc_ref):
    """
  Create the user if it doesnt exist
  """
    user_ref = get_user(account_id, user_id)
    if not user_ref:
        # insert a new user, but lacking optional fields
        user_key = get_user_key(account_id, user_id)
        new_user = Users(key_name=user_key,
                         userid=user_id,
                         isEnabled="yes",
                         accountRef=acc_ref)
        try:
            memcache_db.save_entity(new_user, user_key)
        except:
            logging.error("Error saving user with key %s, userid %s for account %s"%\
                   (user_key, user_id, account_id))
            return None
        user_ref = get_user(account_id, user_id)
        if not user_ref:
            logging.error("Error getting user with key %s, userid %s for account %s"%\
                 (user_key, user_id, account_id))
            return None
    return user_ref
Ejemplo n.º 12
0
  def post(self):
    start = time.time()
    clean = XssCleaner()
    api_key = self.request.get('apikey')
    account_id = self.request.get('accountid')
    new_user_id = self.request.get('userid')
    # Anything that can possibly be rended should be cleaned 
    profile_link = self.request.get('profile_link')
  
    # We can't clean it because it will not render if embedded into a site
    # Be wary of doing any queries with this data
    #profile_link = clean.strip(profile_link)
    profile_img = self.request.get('profile_img') 
    #profile_img = clean.strip(profile_img)
    profile_name = self.request.get('profile_name')
    profile_name = clean.strip(profile_name)
    logdiction = {'event':'loginuser', 
                  'api': 'update_user',
                  'is_api':'yes',
                  'ip':self.request.remote_addr,
                  'user':new_user_id,
                  'account':account_id,
                  'success':'true'}
    if not account_id or not new_user_id or not api_key:
      self.response.out.write(bad_args())
      logdiction['success'] = 'false'
      logdiction['details'] = bad_args()
      logs.create(logdiction)
      return

    acc = accounts_dao.authorize_api(account_id, api_key)
    if not acc:
      self.response.out.write(auth_error())
      logdiction['success'] = 'false'
      logdiction['details'] = auth_error()
      logs.create(logdiction)
      return 

    # Create a new user
    user_key = users_dao.get_user_key(account_id, new_user_id)

    #Update
    user_ref = users_dao.get_user_with_key(user_key)
    if user_ref:
      dict = {}
      update = False
      if profile_link and profile_link != user_ref.profileLink: 
        dict["profileLink"] = profile_link
        update = True
      if profile_img and profile_img != user_ref.profileImg: 
        dict["profileImg"] = profile_img
        update = True
      if profile_name and profile_name != user_ref.profileName: 
        dict["profileName"] = profile_name
        update = True
      if update: 
        logdiction['event'] = 'updateuser'
        try:
          users_dao.update_user(user_key, dict, None)
        except:
          logdiction['success'] = 'false'
          logdiction['details'] = db_error()
          logs.create(logdiction)
          self.response.out.write(db_error())
          error("Error updating user with id %s"%new_user_id)
          return  

      logs.create(logdiction)

      self.response.out.write(success_ret())
      timing(start)
      return  

    if not profile_img:   
      profile_img = constants.IMAGE_PARAMS.USER_AVATAR

    new_user = Users(key_name=user_key,
                     userid=new_user_id,
                     isEnabled="yes",
                     accountRef=acc,
                     profileName=profile_name,
                     profileLink=profile_link,
                     profileImg=profile_img)
    logdiction['event'] = 'createuser'
    try:
      users_dao.save_user(new_user, user_key)
    except:
      logdiction['success'] = 'false'
      logdiction['details'] = db_error()
      logs.create(logdiction)
      self.response.out.write(db_error())
      error("Error getting user with key %s"%key)
      return  

    logs.create(logdiction)
    self.response.out.write(success_ret())
    timing(start)
    return