def _calculate_scores(self, comp): """Calculate the scores for a completed competition.""" all_scores = UserComp.all_scores_submitted(comp) if not all_scores: return False results = [] for photo in Photo.competition_photos(comp): total_score = Scores.photo_score(photo) results.append((total_score, photo)) results.sort(reverse=True) # calculate positions position = 1 prev_score = 1000000 # full_results = [] for i, (score, photo) in enumerate(results, start=1): if score != prev_score: position = i # full_results.append((position, score, photo)) photo.position = position photo.total_score = score photo.put() prev_score = score return True
def view_open(self, user, comp_id, comp, data): """Create the competition page when its status is Open.""" # for p in Photo.competition_photos(comp): photo_count = len(list(Photo.competition_photos(comp))) data.update({"photo_count": photo_count}) self.render("competition-open.html", **data)
def _last_positions(self, data): '''Update UserStat records for all users who have a photo that was last in a competition.''' for comp in Competition.get_by_status(COMPLETED): photos = list(Photo.competition_photos(comp)) last_position = max(photos, key=lambda x: x.position).position #logging.info('%s: last: %d' % (comp, last_position)) for photo in filter(lambda x: x.position == last_position, photos): user_stat = data[photo.user.id()] user_stat.last_place += 1 user_stat.medals += 1
def view_scoring(self, user, comp_id, comp, user_comp, data): """Create the competition page when its status is Scoring.""" competitor = bool(user) and bool(user_comp) to_score = user_comp and not user_comp.submitted_scores photos = [] for p in Photo.competition_photos(comp): # for p in self.get_competition_photos(comp_id, comp=comp): title, url, thumb, _, _, _, _ = p.data(128) if user: user_photo = p.user.get() == user if not to_score: s = Scores.score_from_user(p, user) score = s.score if s else None else: score = None else: user_photo = False score = None photos.append((p, title, url, thumb, score, user_photo)) data.update({"competitor": competitor, "photos": photos, "to_score": to_score}) self.render("competition-scoring.html", **data)