def get_trending_projects(query_params): timestamp = datetime.datetime.now() - datetime.timedelta(weeks=4) transactions_from_last_week = Transaction.query(Transaction.time_stamp > timestamp).fetch() projects = {} for t in transactions_from_last_week: projects[t.project] = 0 for t in transactions_from_last_week: projects[t.project] += t.money pk = sorted(projects, key=projects.__getitem__)[:query_params.page_size] return convert_to_json(Project.query(ndb.AND(Project.key.IN(pk), Project.status == Status.ACTIVE)).fetch())
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().clear_cache() User(name="FancyName", google_id="123id", key=ndb.Key(pairs=[(User, 1)])).put() Project(name="name123", creator=ndb.Key(pairs=[(User, 1)]), createdOn=datetime.datetime(2016, 4, 25), key=ndb.Key(pairs=[(Project, 1)])).put() Transaction(project=ndb.Key(pairs=[(Project, 1)]), user=ndb.Key(pairs=[(User, 1)]), time_stamp=datetime.datetime(2016, 4, 25), key=ndb.Key(pairs=[(Transaction, 1)])).put() self.transaction = Transaction.query(Transaction.key == ndb.Key(pairs=[(Transaction, 1)])).fetch()[0]