def admin_points(request): if request.method == 'POST': form = PointsForm(request.POST) if form.is_valid(): recalculate_points = form.cleaned_data['recalculate_points'] clear_cache = form.cleaned_data['clear_cache'] repopulate_cache = form.cleaned_data['repopulate_cache'] if recalculate_points: rounds = Round.objects.all() for nmk_round in rounds: recalculate_round_points(nmk_round, recalculate_total=False) recalculate_total_points() if clear_cache or repopulate_cache: groups = Group.objects.all() for group in groups: StandingsCache(group).clear() StandingsCache().clear() for nmk_round in rounds: RoundStandingsCache.clear_round(nmk_round) if repopulate_cache: groups = Group.objects.all() rounds = Round.objects.order_by('id') for group in groups: StandingsCache(group).get(rounds) StandingsCache().get(rounds) for nmk_round in rounds: for group in groups: RoundStandingsCache(nmk_round, group).get() RoundStandingsCache(nmk_round).get() else: form = PointsForm() return render(request, 'admin_points.html', {'form': form})
def activation(request): logger.info("User is on activation page") activation_id = request.GET.get('id', '') success = False player = None if activation_id != '': players = Player.objects.filter(activation_code = activation_id) if len(players) == 1: players[0].user.is_active = True players[0].user.save() success = True player = players[0] StandingsCache().clear() rounds = RoundStandingsCache.clear_group() for nmk_round in rounds: RoundStandingsCache.clear_round(nmk_round) return render(request, "activation.html", {"success": success, "player": player})
def activation(request): logger.info('User is on activation page') activation_id = request.GET.get('id', '') success = False player = None if activation_id != '': players = Player.objects.filter(activation_code=activation_id) if len(players) == 1: players[0].user.is_active = True players[0].user.save() success = True player = players[0] StandingsCache().clear() rounds = RoundStandingsCache.clear_group() for nmk_round in rounds: RoundStandingsCache.clear_round(nmk_round) return render(request, 'activation.html', {'success': success, 'player': player})
def home(request): logger.info('User %s is on betting page', request.user) active_rounds = Round.objects.filter(active=True).order_by('id') if len(active_rounds) == 0: messages.add_message( request, messages.INFO, _('There is no active round currently to place bets, try again later' )) return render(request, 'home.html', {'shots': []}) bets = [] for active_round in active_rounds: user_rounds = UserRound.objects.select_related('round').filter( user=request.user).filter(round=active_round) if len(user_rounds) != 1: messages.add_message( request, messages.INFO, _('There is no active round currently to place bets, try again later' )) return render(request, 'home.html', {'shots': []}) user_round = user_rounds[0] shots = Shot.objects.select_related('match', 'match__home_team', 'match__away_team', 'user_round').\ filter(user_round=user_round).order_by('match__start_time') if len(shots) == 0: matches = Match.objects.select_related('home_team', 'away_team').filter(round=user_round.round).\ order_by('start_time') shots = [] for match in matches: shot = Shot(user_round=user_round, match=match) shots.append(shot) betting_allowed = True for shot in shots: if shot.match.start_time < timezone.now(): betting_allowed = False break if not user_round.shot_allowed: betting_allowed = False form = None if betting_allowed: if request.method == 'POST' and\ ('save_'+str(active_round.id) in request.POST or 'final_save_'+str(active_round.id) in request.POST): logger.info('User %s posted betting', request.user) form = BettingForm(request.POST, shots=shots, player=request.user.player) if form.is_valid(): logger.info('User %s posted valid form %s', request.user, form.cleaned_data) for user_round_match in form.cleaned_data: user_round_id = int(user_round_match.split('_')[0]) match_id = int(user_round_match.split('_')[1]) shot = next(shot for shot in shots if shot.user_round.id == user_round_id and shot.match.id == match_id) if shot is not None: shot.shot = form.cleaned_data[user_round_match] shot.save() if 'final_save_' + str(active_round.id) in request.POST: logger.info('User %s posted final save', request.user) user_round.shot_allowed = False user_round.save() RoundStandingsCache.clear_round(user_round.round) messages.add_message(request, messages.INFO, _('Bets successfully saved')) return HttpResponseRedirect('/') else: form = BettingForm(shots=shots, player=request.user.player) time_left = format_time_left(shots) one_round_to_bet = { 'form': form, 'shots': shots, 'round': active_round, 'time_left': time_left } bets.append(one_round_to_bet) return render(request, 'home.html', {'bets': bets})
def home(request): logger.info("User %s is on betting page", request.user) active_rounds = Round.objects.filter(active=True).order_by("id") if len(active_rounds) == 0: messages.add_message(request, messages.INFO, u"Trenutno nema aktivnog kola za klađenje, pokušaj kasnije") return render(request, "home.html", {"shots": []}) bets = [] for active_round in active_rounds: user_rounds = UserRound.objects.select_related('round').filter(user=request.user).filter(round=active_round) if len(user_rounds) != 1: messages.add_message(request, messages.INFO, u"Trenutno nema aktivnog kola za klađenje, pokušaj kasnije") return render(request, "home.html", {"shots": []}) user_round = user_rounds[0] shots = Shot.objects.select_related('match', 'match__home_team', 'match__away_team', 'user_round').filter(user_round=user_round).order_by("match__start_time") if len(shots) == 0: matches = Match.objects.select_related('home_team', 'away_team').filter(round=user_round.round).order_by("start_time") shots = [] for match in matches: shot = Shot(user_round=user_round, match=match) shots.append(shot) betting_allowed = True for shot in shots: if shot.match.start_time < datetime.now(): betting_allowed = False break if not user_round.shot_allowed: betting_allowed = False form = None if betting_allowed: if request.method == 'POST' and\ ('save_'+str(active_round.id) in request.POST or 'final_save_'+str(active_round.id) in request.POST): logger.info("User %s posted betting", request.user) form = BettingForm(request.POST, shots=shots) if form.is_valid(): logger.info("User %s posted valid form %s", request.user, form.cleaned_data) for user_round_match in form.cleaned_data: user_round_id = int(user_round_match.split("_")[0]) match_id = int(user_round_match.split("_")[1]) shot = next(shot for shot in shots if shot.user_round.id==user_round_id and shot.match.id==match_id) if shot != None: shot.shot=form.cleaned_data[user_round_match] shot.save() if 'final_save_'+str(active_round.id) in request.POST: logger.info("User %s posted final save", request.user) user_round.shot_allowed = False user_round.save() RoundStandingsCache.clear_round(user_round.round) messages.add_message(request, messages.INFO, u"Tipovanje uspešno sačuvano") return HttpResponseRedirect('/') else: form = BettingForm(shots=shots) time_left = format_time_left(shots) one_round_to_bet = {"form": form, "shots": shots, "round": active_round, "time_left": time_left} bets.append(one_round_to_bet) return render(request, "home.html", {"bets": bets})