def post(self):
     update_session_time()
     session = get_current_session()
     check_session_status()
         
     if session.is_active():
         selected = self.request.get('selected_competition_group')
         
         edit = self.request.get('edit')
         ranking = self.request.get('ranking')
         
         if selected != "default":
             selected_competition_group_key = Key(selected)
             
             if edit:
                 global_group = CACompetitonGroup.all().filter("privacy =", True).fetch(1)[0]
                 
                 if (selected_competition_group_key != global_group.key()):
                     self.redirect('/edit/group?id=' + selected)
                 else:
                     self.redirect('/list/groups/view')
             elif ranking:
                 competition_group = CACompetitonGroup.get(selected_competition_group_key)
                 
                 group_ranking = CAGroupRanking.all().filter("group =", selected_competition_group_key).fetch(10000)
                 
                 active_user = session['active_user']
                 active_user_football_pools = active_user.football_pools
                 
                 active_user_football_pools_keys = []
                 for football_pool in active_user_football_pools: 
                     active_user_football_pools_keys.append(football_pool.key())
                 
                 group_ranking_list = []
                 
                 for rank in group_ranking:
                     if rank.football_pool.key() in active_user_football_pools_keys:
                         selected = True
                     else:
                         selected = False
                     
                     if rank.football_pool.user.type == 0:
                         name = rank.football_pool.user.google_user.nickname()
                     elif rank.football_pool.user.type == 1:
                         name = rank.football_pool.user.facebook_user.name
                     else:
                         name = rank.football_pool.user.native_user.name
                         
                     group_ranking_list.append((name, rank.football_pool.name, get_total_points(rank.football_pool), selected, rank.football_pool.key()))
                     
                 sorted_group_ranking_list = sorted(group_ranking_list,key=lambda position: position[2], reverse=True)    
                 comments = CAGroupComment.all().filter("group =", competition_group).fetch(10000)
                 comments_info = []
                 
                 for comment in comments:
                     if comment.user.type == 0:
                         name = comment.user.google_user.nickname()
                     elif comment.user.type == 1:
                         name = comment.user.facebook_user.name
                     else:
                         name = comment.user.native_user.name
                         
                     comments_info.append((name, str(comment.date.day) + '/' + str(comment.date.month) + '/' + str(comment.date.year) + ' ' + str(comment.date.hour) + ':' + str(comment.date.minute), comment.comment))
                     
                 template_values = {
                     'session_status': True,
                     'user': active_user,
                     'competition_group_name': competition_group.name,
                     'group_ranking': sorted_group_ranking_list,
                     'top_scorers': get_top_scorers(),
                     'top_users': get_top_users_global_ranking(),
                     'last_jackpot': get_last_jackpot(),
                     'comments': comments_info,
                     'selected_group_key': self.request.get('selected_competition_group'),
                 }
                 
                 render_template(self, 'ranking.html', template_values)
         else:
             if edit:
                 self.redirect('/list/groups/view')
             elif ranking:
                 self.redirect('/list/groups/ranking')
     else:
         self.redirect('/')
Exemplo n.º 2
0
    def post(self):
        update_session_time()
        session = get_current_session()
        check_session_status()

        if session.is_active():
            comment_text = self.request.get("comment-text")
            selected_competition_group_key = Key(self.request.get("selected_group_key"))
            competition_group = CACompetitonGroup.get(selected_competition_group_key)

            google_time = datetime.datetime.today()
            delta = datetime.timedelta(minutes=270)
            comment_time = google_time - delta

            comment = CAGroupComment(
                group=competition_group, user=session["active_user"], comment=comment_text, date=comment_time
            )
            comment.put()

            group_ranking = CAGroupRanking.all().filter("group =", selected_competition_group_key).fetch(10000)

            active_user = session["active_user"]
            active_user_football_pools = active_user.football_pools

            active_user_football_pools_keys = []
            for football_pool in active_user_football_pools:
                active_user_football_pools_keys.append(football_pool.key())

            group_ranking_list = []

            for rank in group_ranking:
                if rank.football_pool.key() in active_user_football_pools_keys:
                    selected = True
                else:
                    selected = False

                if rank.football_pool.user.type == 0:
                    name = rank.football_pool.user.google_user.nickname()
                elif rank.football_pool.user.type == 1:
                    name = rank.football_pool.user.facebook_user.name
                else:
                    name = rank.football_pool.user.native_user.name

                group_ranking_list.append(
                    (
                        name,
                        rank.football_pool.name,
                        get_total_points(rank.football_pool),
                        selected,
                        rank.football_pool.key(),
                    )
                )

            comments = CAGroupComment.all().filter("group =", competition_group).fetch(10000)
            comments_info = []

            for comment in comments:
                if comment.user.type == 0:
                    name = comment.user.google_user.nickname()
                elif comment.user.type == 1:
                    name = comment.user.facebook_user.name
                else:
                    name = comment.user.native_user.name

                comments_info.append(
                    (
                        name,
                        str(comment.date.day)
                        + "/"
                        + str(comment.date.month)
                        + "/"
                        + str(comment.date.year)
                        + " "
                        + str(comment.date.hour)
                        + ":"
                        + str(comment.date.minute),
                        comment.comment,
                    )
                )

            template_values = {
                "session_status": True,
                "user": active_user,
                "competition_group_name": competition_group.name,
                "group_ranking": group_ranking_list,
                "top_scorers": get_top_scorers(),
                "top_users": get_top_users_global_ranking(),
                "last_jackpot": get_last_jackpot(),
                "comments": comments_info,
                "selected_group_key": self.request.get("selected_group_key"),
            }

            render_template(self, "ranking.html", template_values)
        else:
            self.redirect("/")