def matchs_edit(request): begin = dt.combine(dates.AddDay(datetime.date.today(), -31), dt.min.time()) end = dt.combine(dates.AddDay(datetime.date.today(), 14), dt.max.time()) active_events = BettableEvent.objects.filter( end_date__gte=begin).order_by('name') 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] context = {'all_dates': all_dates, 'events': active_events} return render(request, 'match_edit.html', context)
def matchs_schedule(request): if request.user.id != None and request.user.is_authenticated and request.user.is_superuser: begin = dt.combine(dates.AddDay(datetime.date.today(), -31), dt.min.time()) end = dt.combine(dates.AddDay(datetime.date.today(), 31), dt.max.time()) all_matchs = Match.objects.filter(when__gte=begin, when__lte=end).order_by('when') context = {'all_matchs': all_matchs} return render(request, 'match_schedule.html', context) else: raise PermissionDenied()
def compute_fifa_wc_8th(event): if (datetime.date.today() - event.start_date).days < 15: for match in event.matchs.filter(type__identifier='MATCH_EIGHTH'): event.matchs.remove(match) match.delete() event.save() event_meta = get_event_meta(event) for index in range(0, 4): effective_match = Match() first = event_meta['groups'][event_meta['groups_list'][index * 2]][0] second = event_meta['groups'][event_meta['groups_list'][(index * 2) + 1]][1] effective_match.name = first['name'] + ' vs ' + second[ 'name'] + ' [1' + event_meta['groups_list'][ index * 2] + ' - 2' + event_meta['groups_list'][ (index * 2) + 1] + ']' effective_match.type = Attributes.objects.get( identifier='MATCH_EIGHTH', type='match_type') effective_match.when = dt.combine( dates.AddDay(event.start_date, 16 + index), dt.min.time()) effective_match.first = Participant.objects.get(id=first['id']) effective_match.second = Participant.objects.get(id=second['id']) effective_match.save() event.matchs.add(effective_match) event.save() effective_match = Match() first = event_meta['groups'][event_meta['groups_list'][(index * 2) + 1]][0] second = event_meta['groups'][event_meta['groups_list'][index * 2]][1] effective_match.name = first['name'] + ' vs ' + second[ 'name'] + ' [1' + event_meta['groups_list'][ (index * 2) + 1] + ' - 2' + event_meta['groups_list'][index * 2] + ']' effective_match.type = Attributes.objects.get( identifier='MATCH_EIGHTH', type='match_type') effective_match.when = dt.combine( dates.AddDay(event.start_date, 16 + index), dt.min.time()) effective_match.first = Participant.objects.get(id=first['id']) effective_match.second = Participant.objects.get(id=second['id']) effective_match.save() event.matchs.add(effective_match) event.save() complete_meta_for_type(event, 'MATCH_EIGHTH')
def handle(self, *args, **options): begin = dt.combine(dates.AddDay(datetime.date.today(), -7), dt.min.time()) active_events = BettableEvent.objects.filter( end_date__gte=begin).order_by('name') for event in active_events: providers = Provider.objects.filter(event__id=event.id) event_meta = get_event_meta(event) for provider in providers: provider.get_all_matches_info() for m_type in event_meta['final_phases'][1:]: complete_meta_for_type(event, m_type) compute_event_ranking() compute_group_ranking() compute_overall_ranking() generate_matchs()
def generate_matchs(match_ids=None): start_date = dates.AddDay(datetime.date.today(), -14) start_date_time = dt.combine(start_date, dt.min.time()) all_dates = Match.objects.filter( when__gte=start_date_time).order_by('when').dates('when', 'day') template = loader.get_template('rendition/matchs.html') for a_date in all_dates: events = BettableEvent.objects.filter(end_date__gte=start_date) for event in events: matchs = event.matchs.filter( when__gte=dt.combine(a_date, dt.min.time()), when__lte=dt.combine(a_date, dt.max.time())).order_by('when') context = Context({"events": matchs}) rendition = template.render(context) try: os.makedirs(os.path.join(STATICS_PATH, 'matchs', str(event.id))) except: None # Folder already exists outfile = os.path.join(STATICS_PATH, 'matchs', str(event.id), a_date.strftime('%Y-%m-%d') + '_en.html') with open(outfile, 'w') as o: o.write(rendition.encode('utf-8'))
def compute_event_ranking(): users = User.objects.filter(is_active=True) today = dates.AddDay(datetime.date.today(), -1) events = BettableEvent.objects.filter(end_date__gte=today) events_ranking = {} for event in events: LOGGER.info("Working on event " + unicode(event.name)) EventRanking.objects.filter(event__id=event.id).delete() for user in users: LOGGER.info("\tWorking on user " + unicode(user.username)) for match in event.matchs.filter(result__isnull=False): score = 0 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] score = bet.get_score() if not events_ranking.has_key(event.id): events_ranking[event.id] = {} if not events_ranking[event.id].has_key(user.id): rank = EventRanking() rank.event = event rank.owner = user rank.overall_score = 0 rank.rank = None events_ranking[event.id][user.id] = rank LOGGER.info("\t\tResult: FINAL CREATION = " + str( events_ranking[event.id][user.id].overall_score)) events_ranking[event.id][user.id].overall_score += score LOGGER.info( "\t\tResult: FINAL score = " + str(events_ranking[event.id][user.id].overall_score)) for event_id in events_ranking.keys(): rank_list(events_ranking[event_id].values())
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)