def get(self): logging.info('stats calculator...starting') # create a UserStat object for all Users in the db users = list(User.query().fetch()) data = dict( (user.key.id(), UserStats(user=user.key)) for user in users ) for user in users: user_stat = data[user.key.id()] user_stat.logins = user.login_count user_stat.logouts = user.logout_count user_stat.bio = 1 if user.bio else 0 for photo in Photo.query().fetch(): user_id = photo.user.id() user_stat = data[user_id] if photo.competition is None: user_stat.extra_photos += 1 else: if photo.competition.get().status != COMPLETED: # not interested in competition photos for incomplete # competitions continue user_stat.comp_photos += 1 user_stat.total_points += photo.total_score if photo.position == 1: user_stat.first_place += 1 user_stat.medals += 1 elif photo.position == 2: user_stat.second_place += 1 user_stat.medals += 1 elif photo.position == 3: user_stat.third_place += 1 user_stat.medals += 1 completed_comp_count = Competition.count() for user_stat in data.values(): if user_stat.comp_photos == completed_comp_count: user_stat.all_comps = 1 for comment in Comment.query().fetch(): # give data[comment.user.id()].comments_give += 1 # receive receiver = comment.photo.get().user.id() data[receiver].comments_receive += 1 for score in Scores.query().fetch(): receiver = score.photo.get().user.id() if score.score == 10: # give 10 data[score.user_from.id()].score_10_give += 1 # receive 10 data[receiver].score_10_receive += 1 elif score.score == 0: # give 0 data[score.user_from.id()].score_0_give += 1 # receive 0 data[receiver].score_0_recieve += 1 for note in Note.query().fetch(): data[note.user.id()].notes += 1 # is this person a GIVER for user in data.values(): if user.comments_give > user.comments_receive: user.giver += 1 if user.score_10_give > user.score_10_receive: user.giver += 1 # last place finishers self._last_positions(data) self._photo_with_most_comments(data) self._photo_with_high_score(data) self._houses(data) self._most(data, 'comments_give') self._most(data, 'comments_receive') self._most(data, 'notes') self._most(data, 'logins') self._most(data, 'logouts') self._most(data, 'medals') self._most(data, 'first_place') self._most(data, 'second_place') self._most(data, 'third_place') self._most(data, 'last_place') UserStats.delete_all() for stat in data.values(): stat.put() logging.info(data) logging.info('stats calculator...finished')