def send_add_photo_notifications(bout, from_user_email): if bout and from_user_email: Notification.create('photo_add', bout.owner, from_user_email, bout) photos = Photo.for_(bout) if len(photos)>0: for photo in photos: if photo.owner_email != from_user_email and photo.owner_email != bout.owner.email: Notification.create('photo_add', photo.user, from_user_email, bout)
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_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