def get(self):
        update_session_time()
        session = get_current_session()
        check_session_status()
            
        if session.is_active():
            member_request = CARequestGroupMembership.get(Key(self.request.get('request')))
            active_user = session['active_user']
            
            active_user.groups.append(member_request.group.key())
            active_user.put()
            
            active_user_football_pools = CAFootballPool.all().filter("user ="******"privacy =", False).fetch(1000)
            members = member_request.group.members.fetch(10000)
                
            for football_pool in active_user_football_pools:
                if football_pool.payment:
                    groups = active_user.groups
                        
                    group_ranking = CAGroupRanking(football_pool=football_pool, group=member_request.group, rank=len(members))
                    group_ranking.put()
                    
            CARequestGroupMembership.delete(member_request)
            
#            template_values = {
#                'session_status': True,
#                'user': session['active_user'],
#                'top_scorers': get_top_scorers(),
#                'top_users': get_top_users_global_ranking(),
#                'last_jackpot': get_last_jackpot()
#            }
#                        
#            render_template(self, 'home.html', template_values)
            self.redirect('/list/groups/view')
        else:
            self.redirect('/')
    def post(self):
        update_session_time()
        session = get_current_session()
        check_session_status()
            
        if session.is_active():
            search_user = self.request.get('search_user')
            save = self.request.get('save')
            
            if search_user:
                active_user = session['active_user']
                
                search_term = self.request.get('search')
                search_result = []
                
                if search_term:
                    users = CAUser.all().fetch(10000)
                    logging.debug('Iterando por usuarios')

                    for user in users:
                        if active_user.key() != user.key():
                            username = []
                            
                            logging.debug('tipo de usuario: ' + str(user.type))
                            
                            if user.type == 0:
                                nickname = user.google_user.nickname()
                                email = user.google_user.email()

                                if (search_term.lower() in str(nickname).lower()) or (search_term.lower() in str(email).lower()):
                                    username = nickname + ' ' + email
                            elif user.type == 1:
                                name = user.facebook_user.name

                                if search_term.lower() in str(name).lower():
                                    username = name
                            else:
                                logging.debug('Usuario nativo!')
                                logging.debug('key de usuario: ' + str(user.key()))
                                
                                name = user.native_user.name
                                email = user.native_user.email

                                if (search_term.lower() in str(name).lower()) or (search_term.lower() in str(email).lower()):
                                    username = name + ' ' + email

                            if username:
                                search_result.append((str(username), str(user.key())))
                            
                template_values = {
                    'session_status': True,
                    'user': session['active_user'],
                    'searched_users': search_result,
                    'members': eval(self.request.get('last-members')),
                    'name': self.request.get('name'),
                    'last_search': str(search_result),
                    'last_members': self.request.get('last-members'),
                    'top_scorers': get_top_scorers(),
                    'top_users': get_top_users_global_ranking(),
                    'last_jackpot': get_last_jackpot()
                }
                
                render_template(self, 'create_competition_group.html', template_values)
            elif save:
                active_user = session['active_user']                
                name = self.request.get('name')
               
                new_group = CACompetitonGroup(name=name, privacy=False)
                new_group.put()
                
                active_user.groups.append(new_group.key())
                active_user.put()
                
                active_user_football_pools = CAFootballPool.all().filter("user ="******"privacy =", False).fetch(1000)
                
                for football_pool in active_user_football_pools:
                    if football_pool.payment:
                        members = new_group.members.fetch(10000)
                        
                        group_ranking = CAGroupRanking(football_pool=football_pool, group=new_group, rank=len(members))
                        group_ranking.put()
                
                members = eval(self.request.get('last-members'))
                
                for member in members:
                    member_key = Key(member[1])
                    user = CAUser.get(member_key)
                    
                    users = []
                    users.append(active_user.key())
                    users.append(user.key())
                    
                    request_membership = CARequestGroupMembership(users=users, status=False, group=new_group)
                    request_membership.put()
                    
                #self.redirect('/list/groups/ranking')
                competition_groups = CACompetitonGroup.get(active_user.groups)
                
                template_values = {
                    'session_status': True,
                    'user': session['active_user'],
                    'groups': competition_groups,
                    'pending_membership_requests': get_pending_membership_requests(active_user),
                    'top_scorers': get_top_scorers(),
                    'top_users': get_top_users_global_ranking(),
                    'last_jackpot': get_last_jackpot(),
                    'message': 'Se han enviado con éxito las invitaciones a los usuarios para formar parte del grupo.'
                }
                
                render_template(self, 'list_competition_groups_to_ranking.html', template_values)
        else:
            self.redirect('/')