def update_last_visit(self, current_time): from admintools import apply_updates apply_updates(self) prev_visit = LastModified.get(self._fullname, "Visit") if prev_visit and current_time - prev_visit < timedelta(days=1): return g.log.debug("Updating last visit for %s from %s to %s" % (self.name, prev_visit, current_time)) LastModified.touch(self._fullname, "Visit")
def update_last_visit(self, current_time): from admintools import apply_updates timer = g.stats.get_timer("account.update_last_visit") timer.start() apply_updates(self, timer) prev_visit = LastModified.get(self._fullname, "Visit") timer.intermediate("get_last_modified") if prev_visit and current_time - prev_visit < timedelta(days=1): timer.intermediate("set_last_modified.noop") timer.stop() return LastModified.touch(self._fullname, "Visit") timer.intermediate("set_last_modified.done") timer.stop()
def fast_query(cls, thing1, thing2s): """Find relationships between thing1 and various thing2s.""" thing2s, thing2s_is_single = tup(thing2s, ret_is_single=True) if not thing1: return {} # don't bother looking up relationships for items that were created # since the last time the thing1 created a relationship of this type if cls._last_modified_name: from r2.models.last_modified import LastModified timestamp = LastModified.get(thing1._fullname, cls._last_modified_name) if timestamp: thing2s = [thing2 for thing2 in thing2s if thing2._date <= timestamp] else: thing2s = [] if not thing2s: return {} # fetch the row from cassandra. if it doesn't exist, thing1 has no # relation of this type to any thing2! try: columns = [thing2._id36 for thing2 in thing2s] results = cls._cf.get(thing1._id36, columns) except NotFoundException: results = {} # return the data in the expected format if not thing2s_is_single: # {(thing1, thing2) : value} thing2s_by_id = {thing2._id36 : thing2 for thing2 in thing2s} return {(thing1, thing2s_by_id[k]) : v for k, v in results.iteritems()} else: if results: assert len(results) == 1 return results.values()[0] else: raise NotFound("<%s %r>" % (cls.__name__, (thing1._id36, thing2._id36)))