def set_winner(bout): bout.change_status(2) participants = sorted(Photo.for_(bout), key=lambda x: Vote.count(x), reverse=True) if len(participants) > 0: max_vote_count = Vote.count(participants[0]) if max_vote_count > 0: for participant in participants: current_vote_count = Vote.count(participant) if current_vote_count == max_vote_count: winner = User.get_by_key_name(participant.owner_email) Winner.create(winner, bout) Notification.create('winner', winner, winner.email, bout) elif current_vote_count < max_vote_count: break
def get(self): response = [] bout_id = long(self.request.get('bout_id')) bout = Bout.get_by_id(bout_id) for rank, photo in enumerate(sorted(Photo.for_(bout), key=lambda x: Vote.count(x), reverse=True), start=1): user_dict = {} owner = User.get_by_key_name(photo.owner_email) user_dict['votes'] = Vote.count(photo) user_dict['rank'] = rank user_dict['email'] = photo.owner_email user_dict['first_name'] = owner.first_name user_dict['last_name'] = owner.last_name user_dict['profile_picture'] = owner.profile_picture response.append(user_dict) self.response.write(json.dumps(response))
def make_photo_dict(photo, email): photo_dict = {} owner = User.get_by_key_name(photo.owner_email) photo_dict['image'] = photo.image_url photo_dict['owner_email'] = photo.owner_email photo_dict['owner_first_name'] = owner.first_name photo_dict['owner_last_name'] = owner.last_name photo_dict['num_votes'] = Vote.count(photo) photo_dict['num_comments'] = len(Comment.for_(photo)) photo_dict['is_voted'] = Vote.is_voted(email, photo) photo_dict['profile_picture'] = owner.profile_picture return photo_dict
def post(self): response = {} user = util.get_user_from_session() email = user.key().name() owner_email = self.request.get('owner_email') bout_id = long(self.request.get('bout_id')) bout = Bout.get_by_id(bout_id) photo = Photo.get_by_key_name(owner_email, parent=bout) if Vote.update(email, photo, bout): Notification.create('photo_vote', bout.owner, user.email, bout) message = "%s voted on your photo in the Bout %s."%(user.name, bout.name) util.send_push_notification(photo.user.email, message, bout.id) response = {"success": True, "voted": True} else: response = {"success": True, "voted": False} vote_count = Vote.count(photo) response["vote_count"] = vote_count self.response.write(json.dumps(response))
def make_bout_dict(bout, email): bout_dict = {} bout_dict['id'] = bout.id bout_dict['name'] = bout.name bout_dict['description'] = bout.description bout_dict['time_left'] = bout.time_left_string bout_dict['ended'] = bout.ended bout_dict['photos'] = [] user_in_session_photo = Photo.for_bout_user(bout, email) if user_in_session_photo: photo_dict = make_photo_dict(user_in_session_photo, email) bout_dict['photos'].append(photo_dict) for photo in sorted(Photo.for_(bout), key=lambda x: Vote.count(x), reverse=True): if photo.owner_email != email: photo_dict = make_photo_dict(photo, email) bout_dict['photos'].append(photo_dict) if bout.ended: bout_dict['winners'] = [] winners = Winner.for_bout(bout) if len(winners) > 0: for winner in winners: bout_dict['winners'].append(winner.email) return bout_dict