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 callback(ch, method, properties, body): print(" [x] Received %r" % body) output_json = json.loads(body) # add title print('add title') print(output_json["data"]["title"], output_json["data"]["date_start_text"]) Title.Insert(output_json["data"]["title"], output_json["data"]["state"]) # add tournamte trnmt = "" print('add tournamte', output_json["data"]["title"]) tmp = output_json["data"]["tournament"] if len(tmp) == 2: print(tmp["name"]) Tournament.Insert(tmp["name"], output_json["data"]["title"], output_json["data"]["date_start_text"]) trnmt = tmp["name"] else: print(output_json["data"]["tournament"]) Tournament.Insert(output_json["data"]["tournament"], output_json["data"]["title"], output_json["data"]["date_start_text"]) trnmt = output_json["data"]["tournament"] # add team print('add team') for tmp in output_json["data"]["teams"]: Team.Insert(tmp["id"], tmp["name"]) print(tmp["id"], tmp["name"]) # add Matches print('add matches') print(output_json["data"]["scores"][0]["team"]) print(output_json["data"]["scores"][1]["team"]) Matches.Insert(trnmt, output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][1]["team"]) # add Score print('add Score') # Insert(_Tournament, _Team,_Score,_winner,_Team1,_Team2) Scores.Insert(trnmt, output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][0]["score"], output_json["data"]["scores"][0]["winner"], output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][1]["team"]) Scores.Insert(trnmt, output_json["data"]["scores"][1]["team"], output_json["data"]["scores"][1]["score"], output_json["data"]["scores"][1]["winner"], output_json["data"]["scores"][0]["team"], output_json["data"]["scores"][1]["team"])
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 _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
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)
def test_score(self): with pytest.raises(scoresError) as context: result = Scores.Insert("turnamnet", "team1", "1", "true", "team1", "team2") print(str(context.value))
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')