def post(self, comp_id=0): """A user is submitting scores.""" user_id, user = self.get_user() comp_id = int(comp_id) comp = Competition.get_by_id(comp_id) if not user or not comp: # stop some unauthorised post submissions. self.redirect("/competitions") return results = self.parse_scores(self.request.POST) for photo_id, score in results.iteritems(): photo = Photo.get_by_id(photo_id) # photo = self.get_photo(photo_id) new_score = Scores(photo=photo.key, user_from=user.key, score=score) new_score.put() # record that user has submitted scores for this comp usercomp = self.get_usercomp(user, comp) usercomp.submitted_scores = True usercomp.put() self.redirect("/competition/%d" % (comp_id))
def _create_scores(self, users, comps, photos): # the first of the two competitions is complete comp = comps[0] comp_photos = [p for p in photos if p.competition == comp.key] logging.info(comp) logging.info(photos) logging.info(comp_photos) scores = [] for photo, user in product(comp_photos, users): if photo.user.get() == user: continue score = Scores( photo=photo.key, user_from=user.key, score=randint(1, 10) ) logging.info(score) score_key = score.put() logging.info(score_key) scores.append(score) # calculate total scores results = [] for photo in comp_photos: logging.info(photo) total_score = 0 for score in scores: if score.photo == photo.key: total_score += score.score logging.info('total score: %s' % total_score) results.append((total_score, photo)) results.sort(reverse=True) # calculate positions position = 1 prev_score = 1000000 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