Пример #1
0
 def get(self):
     session = get_current_session()
     
     check_session_status()
         
     if session.is_active():
         if session.has_key('active_user'):
             active_user = session['active_user']
             
             global_competition_group = CACompetitonGroup.all().filter("privacy =", True).fetch(1)[0]
             global_rank = CAGroupRanking.gql("WHERE group = :1 ORDER BY rank", global_competition_group.key()).fetch(10000)
             
             global_rank_list = []
             
             for rank in global_rank:
                 if rank.user.key() == active_user.key():
                     current_user = True
                 else:
                     current_user = False
                 
                 if rank.user.type == 0:
                     user_name = rank.user.google_user
                 elif rank.user.type == 1:
                     user_name = rank.user.facebook_user.name
                 else:
                     user_name = rank.user.native_user.name
                 
                 global_rank_list.append((user_name, rank.rank, current_user))
             
             template_values = {
                 'session_status': True,
                 'user': session['active_user'],
                 'ranking': global_rank_list,
                 'competition_group_name': global_competition_group.name,
                 'top_scorers': get_top_scorers(),
                 'top_users': get_top_users_global_ranking(),
                 'last_jackpot': get_last_jackpot()
             }
             
             render_template(self, 'view_global_ranking.html', template_values)
     else:
         self.redirect('/')
 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('/')