Esempio n. 1
0
def group_details(request):
    if request.user.is_authenticated and request.user.id != None:
        group_id = request.GET['group_id']
        user = User.objects.get(id=request.user.id)
        group = Group.objects.get(id=group_id)
        if group.members.filter(id=user.id).exists() or group.owners.filter(
                id=user.id).exists():
            allow_amount = request.user.groups.filter(name='allow_amount')
            all_users_ids = list(group.members.values_list(
                'id', flat=True)) + list(
                    group.owners.values_list('id', flat=True))
            all_users = User.objects.filter(
                id__in=all_users_ids).order_by('id')
            all_matchs = group.event.matchs.all().order_by('when')
            all_bets = {}
            for match in all_matchs:
                match_bets = []
                for a_user in all_users:
                    bets = Bet.objects.filter(match__id=match.id,
                                              owner__id=a_user.id)
                    if not bets.exists():
                        bet = Bet()
                        bet.clean()
                        bet.owner = a_user
                        bet.match = match
                        bet.when = datetime.datetime.now()
                        bet.winner = None
                        score = Score()
                        score.name = match.name
                        score.first = 0
                        score.second = 0
                        score.save()
                        bet.result = score
                        bet.save()
                        match_bets.append(bet)
                    else:
                        match_bets.append(bets[0])

                all_bets[match.name] = match_bets
            context = {
                'all_bets': all_bets,
                'all_users': all_users,
                'all_matchs': all_matchs,
                'today': datetime.date.today().strftime('%Y-%m-%d'),
                'allow_amount': allow_amount
            }
            return render(request, 'group_details.html', context)
    raise PermissionDenied()
Esempio n. 2
0
def group_details(request):
    if request.user.is_authenticated and request.user.id!=None:
        group_id = request.GET['group_id']
        user = User.objects.get(id=request.user.id)
        group = Group.objects.get(id=group_id)
        if group.members.filter(id=user.id).exists() or group.owners.filter(id=user.id).exists():
            allow_amount = request.user.groups.filter(name='allow_amount')
            all_users_ids = list(group.members.values_list('id', flat=True)) + list(group.owners.values_list('id', flat=True))
            all_users = User.objects.filter(id__in=all_users_ids).order_by('id')
            all_matchs = group.event.matchs.all().order_by('when')
            all_bets = {}
            for match in all_matchs:
                match_bets = []
                for a_user in all_users:
                    bets = Bet.objects.filter(match__id=match.id, owner__id=a_user.id)
                    if not bets.exists():
                        bet = Bet()
                        bet.clean()
                        bet.owner = a_user
                        bet.match = match
                        bet.when = datetime.datetime.now()
                        bet.winner = None
                        score = Score()
                        score.name = match.name
                        score.first = 0
                        score.second = 0
                        score.save()
                        bet.result = score
                        bet.save()
                        match_bets.append(bet)
                    else:
                        match_bets.append(bets[0])
                        
                all_bets[match.name] = match_bets
            context = {'all_bets':all_bets, 'all_users': all_users, 'all_matchs': all_matchs, 'today': datetime.date.today().strftime('%Y-%m-%d'), 'allow_amount': allow_amount}
            return render(request,'group_details.html', context) 
    raise PermissionDenied()
Esempio n. 3
0
def index(request):
    now = datetime.datetime.today()
    begin = dt.combine(datetime.date.today(), dt.min.time())
    end = dt.combine(dates.AddDay(begin, 14), dt.max.time())
    all_dates = Match.objects.filter(when__gte=begin, when__lte=end).order_by('when').dates('when','day')
    all_dates = [a_date.strftime('%Y-%m-%d') for a_date in all_dates]
    active_events = BettableEvent.objects.filter(end_date__gte=datetime.date.today).order_by('name')
    if request.user.is_authenticated and request.user.id!=None:
        user = User.objects.get(id=request.user.id)
        if get_user_meta(user)==None:
            initialize_user_meta(user)
        rankings = UserRanking.objects.filter(owner__id=user.id)
        if not rankings.exists():
            initial_ranking = UserRanking()
            initial_ranking.owner = user
            initial_ranking.group = None
            initial_ranking.overall_score = 0
            initial_ranking.rank = None
            initial_ranking.save()
            rankings = UserRanking.objects.filter(owner__id=user.id)
        global_ranking = UserRanking.objects.filter(owner__id=user.id, group=None)
        if global_ranking.exists():
            global_ranking = global_ranking[0]
        else:
            global_ranking = None

        admin_groups = Group.objects.filter(Q(owners__id__exact=request.user.id)).distinct().order_by('name')
        member_groups = Group.objects.filter(Q(members__id__exact=request.user.id) | Q(owners__id__exact=request.user.id)).distinct().order_by('name')

        winner_setups = []
        for group in list(admin_groups) + list(member_groups):
            setup = WinnerSetup.objects.filter(group__id=group.id, group__event__end_date__gte=now)
            if setup.exists():
                setup = setup[0]
                all_steps = setup.setup.all()
                if all_steps.exists():
                    if not group.event.id in winner_setups:
                        winner_setups.append(group.event.id)

        all_matches = Match.objects.filter(when__gte=begin, when__lte=end).order_by('when')
        all_bets = {}
        for match in all_matches:
            bet = Bet.objects.filter(owner__id=request.user.id, match__id=match.id)
            if bet.exists():
                bet = bet[0]
            else:
                bet = Bet()
                bet.clean()
                bet.owner = user
                bet.match = match
                bet.when = datetime.datetime.now()
                bet.winner = None
                score = Score()
                score.name = match.name
                score.first = 0
                score.second = 0
                score.save()
                bet.result = score
                bet.save()
            all_bets[bet.match.id] = {'id': bet.id, 'score': {'first':bet.result.first, 'second':bet.result.second}, 'enabled':str(bet.match.when + datetime.timedelta(minutes=10)>=now).lower(), 'amount': bet.amount if bet.amount!=None else 0}
        allow_amount = request.user.groups.filter(name='allow_amount')

        context = {'winner_setups': winner_setups, 'admin_groups': admin_groups,'member_groups': member_groups, 'all_dates': all_dates, 'all_bets': all_bets, 'rankings': rankings, 'global_rank':global_ranking, 'events': active_events, 'allow_amount': allow_amount, 'events': active_events}
    else:
        context = {'all_dates': all_dates, 'events': active_events, 'all_bets': []}
    return render(request,'index.html', context)
Esempio n. 4
0
def index(request):
    now = datetime.datetime.today()
    begin = dt.combine(datetime.date.today(), dt.min.time())
    end = dt.combine(dates.AddDay(begin, 14), dt.max.time())
    all_dates = Match.objects.filter(when__gte=begin,
                                     when__lte=end).order_by('when').dates(
                                         'when', 'day')
    all_dates = [a_date.strftime('%Y-%m-%d') for a_date in all_dates]
    active_events = BettableEvent.objects.filter(
        end_date__gte=datetime.date.today).order_by('name')
    if request.user.is_authenticated and request.user.id != None:
        user = User.objects.get(id=request.user.id)
        if get_user_meta(user) == None:
            initialize_user_meta(user)
        rankings = UserRanking.objects.filter(owner__id=user.id)
        if not rankings.exists():
            initial_ranking = UserRanking()
            initial_ranking.owner = user
            initial_ranking.group = None
            initial_ranking.overall_score = 0
            initial_ranking.rank = None
            initial_ranking.save()
            rankings = UserRanking.objects.filter(owner__id=user.id)
        global_ranking = UserRanking.objects.filter(owner__id=user.id,
                                                    group=None)
        if global_ranking.exists():
            global_ranking = global_ranking[0]
        else:
            global_ranking = None

        admin_groups = Group.objects.filter(
            Q(owners__id__exact=request.user.id)).distinct().order_by('name')
        member_groups = Group.objects.filter(
            Q(members__id__exact=request.user.id)
            | Q(owners__id__exact=request.user.id)).distinct().order_by('name')

        winner_setups = []
        for group in list(admin_groups) + list(member_groups):
            setup = WinnerSetup.objects.filter(group__id=group.id,
                                               group__event__end_date__gte=now)
            if setup.exists():
                setup = setup[0]
                all_steps = setup.setup.all()
                if all_steps.exists():
                    if not group.event.id in winner_setups:
                        winner_setups.append(group.event.id)

        all_matches = Match.objects.filter(when__gte=begin,
                                           when__lte=end).order_by('when')
        all_bets = {}
        for match in all_matches:
            bet = Bet.objects.filter(owner__id=request.user.id,
                                     match__id=match.id)
            if bet.exists():
                bet = bet[0]
            else:
                bet = Bet()
                bet.clean()
                bet.owner = user
                bet.match = match
                bet.when = datetime.datetime.now()
                bet.winner = None
                score = Score()
                score.name = match.name
                score.first = 0
                score.second = 0
                score.save()
                bet.result = score
                bet.save()
            all_bets[bet.match.id] = {
                'id':
                bet.id,
                'score': {
                    'first': bet.result.first,
                    'second': bet.result.second
                },
                'enabled':
                str(bet.match.when +
                    datetime.timedelta(minutes=10) >= now).lower(),
                'amount':
                bet.amount if bet.amount != None else 0
            }
        allow_amount = request.user.groups.filter(name='allow_amount')

        context = {
            'winner_setups': winner_setups,
            'admin_groups': admin_groups,
            'member_groups': member_groups,
            'all_dates': all_dates,
            'all_bets': all_bets,
            'rankings': rankings,
            'global_rank': global_ranking,
            'events': active_events,
            'allow_amount': allow_amount,
            'events': active_events
        }
    else:
        context = {
            'all_dates': all_dates,
            'events': active_events,
            'all_bets': []
        }
    return render(request, 'index.html', context)