def win(): app = create_app() with app.app_context(): items = Item.query.all() for item in items: if not item.winner and item.due.date() == datetime.now().date(): if item.wishers.count(): item.winner = random.choice(item.wishers.all()) print "Winner of item %d is user %d" %\ (item.id, item.winner.id) else: item.due = item.due.date() + timedelta(days=1) print "There is no wisher for item %d. Next due: %s" %\ (item.id, item.due.strftime('%m/%d')) db.session.commit()