def group_edit(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) now = dt.now() try: group = Group.objects.get(id=group_id) if group.owners.filter(id=user.id).exists(): all_members = list(group.members.all()) + list(group.owners.all()) for member in all_members: rank = UserRanking.objects.filter(owner__id=member.id, group__id=group.id) if not rank.exists(): rank = UserRanking() rank.owner = member rank.group = group rank.overall_score = 0 rank.rank = None rank.save() else: rank = rank[0] all_users = list(group.members.values_list('id', flat=True)) + list(group.owners.values_list('id', flat=True)) all_matchs = group.event.matchs.all().values_list('id', flat=True) betted_amounts = {} for user_id in all_users: betted_amounts[user_id] = Bet.objects.filter(owner__id=user_id, match__id__in=all_matchs).aggregate(Sum('amount'))['amount__sum'] locked_amounts = {} for user_id in all_users: locked_amounts[user_id] = Bet.objects.filter(owner__id=user_id, match__id__in=all_matchs, match__when__lte=now).aggregate(Sum('amount'))['amount__sum'] ranking = UserRanking.objects.filter(owner__id__in=all_users, group__id=group.id) your_rank = UserRanking.objects.get(owner__id=user.id, group__id=group.id) allow_amount = request.user.groups.filter(name='allow_amount') winner_setup = WinnerSetup.objects.filter(group__id=group.id) if winner_setup.exists(): winner_setup = winner_setup[0] else: winner_setup = None context = {'group': group, 'ranking':ranking, 'yours': your_rank, 'betted_amounts':betted_amounts, 'allow_amount':allow_amount,'locked_amounts':locked_amounts, 'winner_setup': winner_setup} return render(request,'group_edit.html', context) else: raise PermissionDenied() except: traceback.print_exc() return HttpResponseBadRequest() else: raise PermissionDenied()
def compute_group_ranking(group_id=None): today = dates.AddDay(datetime.date.today(),-1) if group_id==None: all_groups = Group.objects.filter(event__end_date__gte=today) else: all_groups = Group.objects.filter(id=group_id) for group in all_groups: if not group.owners.all()[0].groups.filter(name='allow_amount').exists(): LOGGER.info("Working on group " + str(group.name)) for user in list(group.members.all()) + list(group.owners.all()): LOGGER.info("\tWorking on user " + str(user.username)) ranking = UserRanking.objects.filter(owner__id=user.id, group__id=group.id) if not ranking.exists(): ranking = UserRanking() ranking.owner = user ranking.group = group ranking.overall_score = 0 ranking.rank = None ranking.save() else: ranking = ranking[0] event_rank = EventRanking.objects.filter(event__id=group.event.id, owner__id=user.id) if event_rank.exists(): event_rank = event_rank[0] ranking.overall_score = event_rank.overall_score ranking.save() else: LOGGER.warn("User [" + str(user.id) +"] has no rank in event:" + str(group.event.name)) else: LOGGER.info("Working on group with event and amount: " + unicode(group.event.name)) events_ranking = {} for user in list(group.members.all()) + list(group.owners.all()): LOGGER.info("\tWorking on user " + unicode(user.username)) for match in group.event.matchs.filter(result__isnull=False): LOGGER.info("\t\tWorking on match " + unicode(match.name)) bet = Bet.objects.filter(match__id=match.id, owner__id=user.id) winner = match.get_winner() LOGGER.info("\t\tWinner is " + unicode(winner)) if bet.exists(): bet = bet[0] if bet.amount!=None and bet.amount!=0: score = bet.get_score() if not events_ranking.has_key(group.event.id): events_ranking[group.event.id] = {} if not events_ranking[group.event.id].has_key(user.id): rank = EventRanking() rank.event = group.event rank.owner = user rank.overall_score = 0 rank.rank = None events_ranking[group.event.id][user.id] = rank LOGGER.info("\t\tResult: FINAL CREATION = " + str(events_ranking[group.event.id][user.id].overall_score)) events_ranking[group.event.id][user.id].overall_score += score LOGGER.info("\t\tResult: FINAL score = " + str(events_ranking[group.event.id][user.id].overall_score)) ranking = UserRanking.objects.filter(owner__id=user.id, group__id=group.id) if not ranking.exists(): ranking = UserRanking() ranking.owner = user ranking.group = group ranking.overall_score = 0 ranking.rank = None ranking.save() else: ranking = ranking[0] if events_ranking[group.event.id].has_key(user.id): ranking.overall_score = events_ranking[group.event.id][user.id].overall_score else: ranking.overall_score = 0 ranking.save() winner_setups = WinnerSetup.objects.filter(group__id=group.id) event_meta = get_event_meta(group.event) for a_setup in winner_setups: for per_type in a_setup.setup.all(): LOGGER.info("Adding winner bets results for " + per_type.category.identifier) if per_type.category.identifier!='MATCH_WINNER': data = [team['id'] for team in event_meta[per_type.category.identifier]['teams']] else: last_match = group.event.matchs.filter(result__isnull=False, type__identifier='MATCH_FINAL') if last_match.exists(): data = [last_match.first.id if last_match.result.first>last_match.result.second else last_match.second.id] else: data = [] print data for user in list(group.members.all()) + list(group.owners.all()): ranking = UserRanking.objects.get(owner__id=user.id, group__id=group.id) winner = Winner.objects.filter(owner__id=user.id, event__id=group.event.id, category__identifier=per_type.category.identifier) if winner.exists(): winner = winner[0] found = len(winner.participants.filter(id__in=data)) if per_type.use_quotes: LOGGER.info("User " + user.username + " found " + str(found) + " teams, adding " + str(found * per_type.points) + " points") ranking.overall_score = ranking.overall_score + (found * per_type.points) else: LOGGER.info("User " + user.username + " found " + str(found) + " teams, adding " + str(POINTS_PER_TYPE[per_type.category.identifier][found]) + " points") ranking.overall_score = ranking.overall_score + POINTS_PER_TYPE[per_type.category.identifier][found] ranking.save() ranks = [UserRanking.objects.get(owner__id=user.id, group__id=group.id) for user in list(group.members.all()) + list(group.owners.all())] rank_list(ranks)
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)
def group_view(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) now = dt.now() try: group = Group.objects.get(id=group_id) if group.members.filter( id=user.id).exists() or group.owners.filter( id=user.id).exists(): all_members = list(group.members.all()) + list( group.owners.all()) for member in all_members: rank = UserRanking.objects.filter(owner__id=member.id, group__id=group.id) if not rank.exists(): rank = UserRanking() rank.owner = member rank.group = group rank.overall_score = 0 rank.rank = None rank.save() else: rank = rank[0] all_users = list(group.members.values_list( 'id', flat=True)) + list( group.owners.values_list('id', flat=True)) all_matchs = group.event.matchs.all().values_list('id', flat=True) betted_amounts = {} for user_id in all_users: betted_amounts[user_id] = Bet.objects.filter( owner__id=user_id, match__id__in=all_matchs).aggregate( Sum('amount'))['amount__sum'] locked_amounts = {} for user_id in all_users: locked_amounts[user_id] = Bet.objects.filter( owner__id=user_id, match__id__in=all_matchs, match__when__lte=now).aggregate( Sum('amount'))['amount__sum'] ranking = UserRanking.objects.filter(owner__id__in=all_users, group__id=group.id) your_rank = UserRanking.objects.filter(owner__id=user.id, group__id=group.id) if your_rank.exists(): your_rank = your_rank[0] allow_amount = request.user.groups.filter(name='allow_amount') context = { 'group': group, 'ranking': ranking, 'yours': your_rank, 'betted_amounts': betted_amounts, 'allow_amount': allow_amount, 'locked_amounts': locked_amounts } return render(request, 'group_view.html', context) else: raise PermissionDenied() except: traceback.print_exc() return HttpResponseBadRequest() else: raise PermissionDenied()
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)
def compute_group_ranking(group_id=None): today = dates.AddDay(datetime.date.today(), -1) if group_id == None: all_groups = Group.objects.filter(event__end_date__gte=today) else: all_groups = Group.objects.filter(id=group_id) for group in all_groups: if not group.owners.all()[0].groups.filter( name='allow_amount').exists(): LOGGER.info("Working on group " + str(group.name)) for user in list(group.members.all()) + list(group.owners.all()): LOGGER.info("\tWorking on user " + str(user.username)) ranking = UserRanking.objects.filter(owner__id=user.id, group__id=group.id) if not ranking.exists(): ranking = UserRanking() ranking.owner = user ranking.group = group ranking.overall_score = 0 ranking.rank = None ranking.save() else: ranking = ranking[0] event_rank = EventRanking.objects.filter( event__id=group.event.id, owner__id=user.id) if event_rank.exists(): event_rank = event_rank[0] ranking.overall_score = event_rank.overall_score ranking.save() else: LOGGER.warn("User [" + str(user.id) + "] has no rank in event:" + str(group.event.name)) else: LOGGER.info("Working on group with event and amount: " + unicode(group.event.name)) events_ranking = {} for user in list(group.members.all()) + list(group.owners.all()): LOGGER.info("\tWorking on user " + unicode(user.username)) for match in group.event.matchs.filter(result__isnull=False): LOGGER.info("\t\tWorking on match " + unicode(match.name)) bet = Bet.objects.filter(match__id=match.id, owner__id=user.id) winner = match.get_winner() LOGGER.info("\t\tWinner is " + unicode(winner)) if bet.exists(): bet = bet[0] if bet.amount != None and bet.amount != 0: score = bet.get_score() if not events_ranking.has_key(group.event.id): events_ranking[group.event.id] = {} if not events_ranking[group.event.id].has_key( user.id): rank = EventRanking() rank.event = group.event rank.owner = user rank.overall_score = 0 rank.rank = None events_ranking[group.event.id][user.id] = rank LOGGER.info("\t\tResult: FINAL CREATION = " + str(events_ranking[group.event.id][ user.id].overall_score)) events_ranking[group.event.id][ user.id].overall_score += score LOGGER.info("\t\tResult: FINAL score = " + str(events_ranking[group.event.id][ user.id].overall_score)) ranking = UserRanking.objects.filter(owner__id=user.id, group__id=group.id) if not ranking.exists(): ranking = UserRanking() ranking.owner = user ranking.group = group ranking.overall_score = 0 ranking.rank = None ranking.save() else: ranking = ranking[0] if events_ranking[group.event.id].has_key(user.id): ranking.overall_score = events_ranking[group.event.id][ user.id].overall_score else: ranking.overall_score = 0 ranking.save() winner_setups = WinnerSetup.objects.filter(group__id=group.id) event_meta = get_event_meta(group.event) for a_setup in winner_setups: for per_type in a_setup.setup.all(): LOGGER.info("Adding winner bets results for " + per_type.category.identifier) if per_type.category.identifier != 'MATCH_WINNER': data = [ team['id'] for team in event_meta[ per_type.category.identifier]['teams'] ] else: last_match = group.event.matchs.filter( result__isnull=False, type__identifier='MATCH_FINAL') if last_match.exists(): data = [ last_match.first.id if last_match.result.first > last_match.result.second else last_match.second.id ] else: data = [] print data for user in list(group.members.all()) + list( group.owners.all()): ranking = UserRanking.objects.get(owner__id=user.id, group__id=group.id) winner = Winner.objects.filter( owner__id=user.id, event__id=group.event.id, category__identifier=per_type.category.identifier) if winner.exists(): winner = winner[0] found = len(winner.participants.filter(id__in=data)) if per_type.use_quotes: LOGGER.info("User " + user.username + " found " + str(found) + " teams, adding " + str(found * per_type.points) + " points") ranking.overall_score = ranking.overall_score + ( found * per_type.points) else: LOGGER.info( "User " + user.username + " found " + str(found) + " teams, adding " + str(POINTS_PER_TYPE[ per_type.category.identifier][found]) + " points") ranking.overall_score = ranking.overall_score + POINTS_PER_TYPE[ per_type.category.identifier][found] ranking.save() ranks = [ UserRanking.objects.get(owner__id=user.id, group__id=group.id) for user in list(group.members.all()) + list(group.owners.all()) ] rank_list(ranks)