Example #1
0
 def fetch(self):
     u = UserModel.fetch(self._user_id)
     if u is None:
         UserModel.create(self._user_id, self.nickname())
         # Use the default properties for a newly created user
         return
     self._nickname = u.nickname
     self._inactive = u.inactive
     self._preferences = u.prefs
Example #2
0
def deferred_update():
    """ Update all users in the datastore with lowercase nick and full name """
    logging.info("Deferred user update starting")
    CHUNK_SIZE = 200
    scan = 0
    count = 0
    with ndb.Client().context():
        try:
            q = UserModel.query()
            for um in iter_q(q, chunk_size=CHUNK_SIZE):
                scan += 1
                if um.email and not um.email.islower():
                    um.email = um.email.lower()
                    um.put()
                    count += 1
                if scan % 1000 == 0:
                    logging.info("Completed scanning {0} and updating {1} user records".format(scan, count))
        except Exception as e:
            logging.info(
                "Exception in deferred_update(): {0}, already scanned {1} records and updated {2}"
                .format(e, scan, count)
            )
            # Do not retry the task
            # !!! TODO: Alternative solution for Python 3 GAE environment
            # raise deferred.PermanentTaskFailure()
    logging.info("Completed scanning {0} and updating {1} user records".format(scan, count))
Example #3
0
def deferred_update():
    """ Update all users in the datastore with lowercase nick and full name """
    logging.info("Deferred user update starting")
    CHUNK_SIZE = 200
    count = 0
    offset = 0
    Context.disable_cache()
    try:
        q = UserModel.query()
        while True:
            ulist = []
            chunk = 0
            for um in q.fetch(CHUNK_SIZE, offset = offset):
                chunk += 1
                if um.nick_lc is None:
                    try:
                        um.nick_lc = um.nickname.lower()
                        um.name_lc = um.prefs.get("full_name", "").lower() if um.prefs else ""
                        ulist.append(um)
                    except Exception as e:
                        logging.info("Exception in deferred_update() when setting nick_lc: {0}".format(e))
            if ulist:
                try:
                    ndb.put_multi(ulist)
                    count += len(ulist)
                except Exception as e:
                    logging.info("Exception in deferred_update() when updating ndb: {0}".format(e))
            if chunk < CHUNK_SIZE:
                break
            offset += CHUNK_SIZE
    except Exception as e:
        logging.info("Exception in deferred_update(): {0}, already updated {1} records".format(e, count))
        # Do not retry the task
        raise deferred.PermanentTaskFailure()
    logging.info("Completed updating {0} user records".format(count))
Example #4
0
    def update(self):
        """ Update the user's record in the database and in the memcache """
        with User._lock:
            # Use a lock to avoid the scenaro where a user is fetched by another
            # request in the interval between a database update and a memcache update
            um = UserModel.fetch(self._user_id)
            assert um is not None
            um.nickname = self._nickname
            um.nick_lc = self._nickname.lower()
            um.name_lc = self.full_name().lower()
            um.inactive = self._inactive
            um.prefs = self._preferences
            um.ready = self._ready
            um.ready_timed = self._ready_timed
            um.elo = self._elo
            um.human_elo = self._human_elo
            um.highest_score = self._highest_score
            um.highest_score_game = self._highest_score_game
            um.best_word = self._best_word
            um.best_word_score = self._best_word_score
            um.best_word_game = self._best_word_game
            um.put()

            # Note: the namespace version should be incremented each time
            # that the class properties change
            memcache.set(self._user_id, self, time=User._CACHE_EXPIRY, namespace=User._NAMESPACE)
Example #5
0
    def update(self):
        """ Update the user's record in the database and in the memcache """
        with User._lock:
            # Use a lock to avoid the scenaro where a user is fetched by another
            # request in the interval between a database update and a memcache update
            um = UserModel.fetch(self._user_id)
            assert um is not None
            um.nickname = self._nickname
            um.nick_lc = self._nickname.lower()
            um.name_lc = self.full_name().lower()
            um.inactive = self._inactive
            um.prefs = self._preferences
            um.ready = self._ready
            um.ready_timed = self._ready_timed
            um.elo = self._elo
            um.human_elo = self._human_elo
            um.highest_score = self._highest_score
            um.highest_score_game = self._highest_score_game
            um.best_word = self._best_word
            um.best_word_score = self._best_word_score
            um.best_word_game = self._best_word_game
            um.put()

            # Note: the namespace version should be incremented each time
            # that the class properties change
            memcache.set(self._user_id,
                         self,
                         time=User._CACHE_EXPIRY,
                         namespace=User._NAMESPACE)
Example #6
0
def deferred_update():
    """ Update all users in the datastore with lowercase nick and full name """
    logging.info("Deferred user update starting")
    CHUNK_SIZE = 200
    count = 0
    offset = 0
    Context.disable_cache()
    try:
        q = UserModel.query()
        while True:
            ulist = []
            chunk = 0
            for um in q.fetch(CHUNK_SIZE, offset = offset):
                chunk += 1
                if um.nick_lc is None:
                    try:
                        um.nick_lc = um.nickname.lower()
                        um.name_lc = um.prefs.get("full_name", "").lower() if um.prefs else ""
                        ulist.append(um)
                    except Exception as e:
                        logging.info("Exception in deferred_update() when setting nick_lc: {0}".format(e))
            if ulist:
                try:
                    ndb.put_multi(ulist)
                    count += len(ulist)
                except Exception as e:
                    logging.info("Exception in deferred_update() when updating ndb: {0}".format(e))
            if chunk < CHUNK_SIZE:
                break
            offset += CHUNK_SIZE
    except Exception as e:
        logging.info("Exception in deferred_update(): {0}, already updated {1} records".format(e, count))
        # Do not retry the task
        raise deferred.PermanentTaskFailure()
    logging.info("Completed updating {0} user records".format(count))
Example #7
0
 def _fetch(self):
     """ Fetch the user's record from the database """
     um = UserModel.fetch(self._user_id)
     if um is None:
         # Use the default properties for a newly created user
         UserModel.create(self._user_id, self.nickname()) # This updates the database
     else:
         # Obtain the properties from the database entity
         self._nickname = um.nickname
         self._inactive = um.inactive
         self._preferences = um.prefs
         self._ready = um.ready
         self._ready_timed = um.ready_timed
         self._elo = um.elo
         self._human_elo = um.human_elo
         self._highest_score = um.highest_score
         self._highest_score_game = um.highest_score_game
         self._best_word = um.best_word
         self._best_word_score = um.best_word_score
         self._best_word_game = um.best_word_game
Example #8
0
def _write_stats(timestamp, urecs):
    """ Writes the freshly calculated statistics records to the database """
    # Delete all previous stats with the same timestamp, if any
    StatsModel.delete_ts(timestamp = timestamp)
    um_list = []
    for sm in urecs.values():
        # Set the reference timestamp for the entire stats series
        sm.timestamp = timestamp
        # Fetch user information to update Elo statistics
        if sm.user:
            # Not robot
            um = UserModel.fetch(sm.user.id())
            if um:
                um.elo = sm.elo
                um.human_elo = sm.human_elo
                um_list.append(um)
    # Update the statistics records
    StatsModel.put_multi(urecs.values())
    # Update the user records
    UserModel.put_multi(um_list)
Example #9
0
def _write_stats(timestamp, urecs):
    """ Writes the freshly calculated statistics records to the database """
    # Delete all previous stats with the same timestamp, if any
    StatsModel.delete_ts(timestamp=timestamp)
    um_list = []
    for sm in urecs.values():
        # Set the reference timestamp for the entire stats series
        sm.timestamp = timestamp
        # Fetch user information to update Elo statistics
        if sm.user:
            # Not robot
            um = UserModel.fetch(sm.user.id())
            if um:
                um.elo = sm.elo
                um.human_elo = sm.human_elo
                um_list.append(um)
    # Update the statistics records
    StatsModel.put_multi(urecs.values())
    # Update the user records
    UserModel.put_multi(um_list)
Example #10
0
 def _fetch(self):
     """ Fetch the user's record from the database """
     um = UserModel.fetch(self._user_id)
     if um is None:
         # Use the default properties for a newly created user
         self.set_new_bag(True)  # Fresh users get the new bag by default
         UserModel.create(self._user_id, self.nickname(),
                          self._preferences)  # This updates the database
     else:
         # Obtain the properties from the database entity
         self._nickname = um.nickname
         self._inactive = um.inactive
         self._preferences = um.prefs
         self._ready = um.ready
         self._ready_timed = um.ready_timed
         self._elo = um.elo
         self._human_elo = um.human_elo
         self._highest_score = um.highest_score
         self._highest_score_game = um.highest_score_game
         self._best_word = um.best_word
         self._best_word_score = um.best_word_score
         self._best_word_game = um.best_word_game
Example #11
0
def admin_usercount():
    """ Return a count of UserModel entities """
    count = UserModel.count()
    return jsonify(count = count)
Example #12
0
def admin_usercount():
    """ Return a count of UserModel entities """
    count = UserModel.count()
    return jsonify(count=count)
Example #13
0
 def update(self):
     UserModel.update(self._user_id, self._nickname, self._inactive, self._preferences)