def group_leave(request, group_id): group = get_object_or_404(Group, pk=int(group_id)) # Check if user is in group at all if len( Group.objects.filter(pk=int(group_id)).filter( players__in=[request.user])) == 0: raise Http404() # Check if user is owner of the group error = None if request.user == group.owner: error = _( 'You cannot leave a crew that you created, you can only completely delete that crew.' ) if not error and request.method == 'POST': if '0' in request.POST: return HttpResponseRedirect('/crew') elif '1' in request.POST: RoundStandingsCache.clear_group(group) group.players.remove(request.user) messages.add_message( request, messages.INFO, _('Successfully left a crew "%s"') % group.name) return HttpResponseRedirect('/crew') return render(request, 'group_leave.html', { 'error': error, 'group': group })
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 group_delete(request, group_id): group = get_object_or_404(Group, pk=int(group_id)) # Check if user is in group at all if len( Group.objects.filter(pk=int(group_id)).filter( players__in=[request.user])) == 0: raise Http404() # Check if user is owner of the group error = None if request.user != group.owner: error = _( 'You cannot delete crew that you are not owner of, you can only leave it.' ) if not error and request.method == 'POST': if '0' in request.POST: return HttpResponseRedirect('/crew') elif '1' in request.POST: group.players.clear() RoundStandingsCache.clear_group(group) StandingsCache(group).clear() group.delete() messages.add_message(request, messages.INFO, _('Crew "%s" deleted') % group.name) return HttpResponseRedirect('/crew') return render(request, 'group_delete.html', { 'error': error, 'group': group })
def crew(request): if request.method == 'POST' and 'new_group' in request.POST: form_new_group = NewGroupForm(request.POST, group={}) if form_new_group.is_valid(): cleaned_data = form_new_group.cleaned_data new_group = Group(name=cleaned_data['name'], group_key=id_generator(size=8, chars=string.digits), owner=request.user) new_group.save() new_group.players.add(request.user) new_group.save() messages.add_message(request, messages.INFO, _('Crew created successfully. Now you can send invite code "%s" to your friends ' 'using chat/mail/SMS, so they too can join your new crew') % new_group.group_key) else: form_new_group = NewGroupForm(group={}) if request.method == 'POST' and 'add_to_group' in request.POST: form_add_group = AddToGroupForm(request.user, request.POST, group_key={}) if form_add_group.is_valid(): cleaned_data = form_add_group.cleaned_data groups = Group.objects.filter(group_key=cleaned_data['key']) group = groups[0] group.players.add(request.user) group.save() RoundStandingsCache.clear_group(group) messages.add_message(request, messages.INFO, _('Joined crew successfully')) else: form_add_group = AddToGroupForm(request.user.player, group_key={}) groups = Group.objects.filter(players__in=[request.user]) return render(request, 'crew.html', {'form_new_group': form_new_group, 'form_add_group': form_add_group, 'groups': groups, 'current_user': request.user} )
def admin_results_change(request, match_id): match = get_object_or_404(Match, pk=int(match_id)) if request.method == 'POST': form = ResultsForm(request.POST, instance=match) if form.is_valid(): match = form.save(commit=False) scores = match.score.split(':') score_home = int(scores[0]) score_away = int(scores[1]) if score_home > score_away: match.result = 1 elif score_home == score_away: match.result = 0 else: match.result = 2 match.save() logger.info('User %s set result for match %s', request.user, match) recalculate_round_points(match.round) messages.add_message(request, messages.INFO, _('Result added successfully')) # invalidate cache of shots for all user in this round RoundStandingsCache.repopulate_round(match.round) groups = Group.objects.all() for group in groups: StandingsCache(group).clear() StandingsCache().clear() # repopulate cache rounds = Round.objects.all() for group in groups: StandingsCache(group).get(rounds) StandingsCache().get(rounds) # send mail if this is the last match from round if settings.SEND_MAIL: count_matches_without_result = Match.objects.all().\ filter(round=match.round).filter(result__isnull=True).count() if count_matches_without_result == 0: all_players = Player.objects.\ exclude(user__email='').filter(user__is_active=True).filter(send_mail_results_available=True) logger.info('Sending mail that round %s have all results to %d', match.round, len(all_players)) for player in all_players: with translation.override(player.language): subject = _('[sharkz.bet] All results from round "%s" received') % match.round.name template = loader.get_template('mail/result_added.html') message_text = template.render({'round': match.round}) msg = EmailMessage(subject, message_text, '*****@*****.**', to=[player.user.email, ]) msg.content_subtype = 'html' msg.send(fail_silently=False) return HttpResponseRedirect('/admin/results') else: form = ResultsForm(instance=match) return render(request, 'admin_results_change.html', {'form': form, 'match': match})
def admin_results_change(request, match_id): match = get_object_or_404(Match, pk=int(match_id)) if request.method == 'POST': form = ResultsForm(request.POST, instance=match) if form.is_valid(): match = form.save(commit=False) scores = match.score.split(":") score_home = int(scores[0]) score_away = int(scores[1]) if score_home > score_away: match.result = 1 elif score_home == score_away: match.result = 0 else: match.result = 2 match.save() logger.info("User %s set result for match %s", request.user, match) recalculate_round_points(match.round) messages.add_message(request, messages.INFO, u"Rezultat uspešno unesen") # invalidate cache of shots for all user in this round RoundStandingsCache.repopulate_round(match.round) groups = Group.objects.all() for group in groups: StandingsCache(group).clear() StandingsCache().clear() # repopulate cache rounds = Round.objects.all() for group in groups: StandingsCache(group).get(rounds) StandingsCache().get(rounds) # send mail if this is the last match from round if settings.SEND_MAIL: count_matches_without_result = Match.objects.all().filter(round=match.round).filter(result__isnull=True).count() if count_matches_without_result == 0: all_players = Player.objects.all() all_user_mail = [player.user.email for player in all_players if player.send_mail==True and player.user.email != ""] logger.info("Sending mail that round %s have all results to %s", match.round, all_user_mail) template = loader.get_template("mail/result_added.html") message_text = template.render(Context({"round": match.round})) for user_mail in all_user_mail: msg = EmailMessage(u"[nmk] Uneti svi rezultati mečeva iz kola \"%s\"" % (match.round.name), message_text, "*****@*****.**", to=[user_mail,]) msg.content_subtype = "html" msg.send(fail_silently = False) return HttpResponseRedirect('/admin/results') else: form = ResultsForm(instance=match) return render(request, "admin_results_change.html", {"form": form, "match": match})
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 group_leave(request, group_id): group = get_object_or_404(Group, pk=int(group_id)) # Check if user is in group at all if len(Group.objects.filter(pk = int(group_id)).filter(player__in=[request.user.player])) == 0: raise Http404() # Check if user is owner of the group error = None if request.user.player == group.owner: error = "Ne možeš da izađeš iz ekipe koju si ti napravio, možeš samo da je izbrišeš skroz." if not error and request.method == 'POST': if '0' in request.POST: return HttpResponseRedirect('/profile') elif '1' in request.POST: RoundStandingsCache.clear_group(group) group.players.remove(request.user.player) messages.add_message(request, messages.INFO, u"Izašao si iz ekipe '%s'" % group.name) return HttpResponseRedirect('/profile') return render(request, "group_leave.html", {"error": error, "group": group})
def paypal(request): username = "******" success = False if request.user.is_authenticated(): username = request.user.username request.user.player.in_money = True request.user.player.save() groups = Group.objects.filter(id = 1) group = groups[0] group.players.add(request.user.player) group.save() RoundStandingsCache.clear_group(group) success = True msg = EmailMessage(u"[nmk] Igrac uplatio paypal", "Igrac %s" % username, "*****@*****.**", to=["*****@*****.**",]) msg.content_subtype = "html" msg.send(fail_silently = True) return render(request, "paypal.html", {"success": success})
def profile(request): if request.method == 'POST' and 'profile_change' in request.POST: form = PlayerForm(request.POST, instance=request.user.player) if form.is_valid(): form.save() messages.add_message(request, messages.INFO, u"Podešavanja uspešno sačuvana") else: form = PlayerForm(instance=request.user.player) if request.method == 'POST' and 'new_group' in request.POST: form_new_group = NewGroupForm(request.POST, group={}) if form_new_group.is_valid(): cleaned_data = form_new_group.cleaned_data new_group = Group(name = cleaned_data['name'], group_key = id_generator(size=8, chars=string.digits), owner = request.user.player) new_group.save() new_group.players.add(request.user.player) new_group.save() messages.add_message(request, messages.INFO, u"Uspešno si napravio novu ekipu. Sad pošalji chat-om/mail-om/SMS-om prijateljima šifru-pozivnicu '%s' \ da bi mogli i oni da uđu u tvoju ekipu" % new_group.group_key) else: form_new_group = NewGroupForm(group={}) if request.method == 'POST' and 'add_to_group' in request.POST: form_add_group = AddToGroupForm(request.user.player, request.POST, group_key={}) if form_add_group.is_valid(): cleaned_data = form_add_group.cleaned_data groups = Group.objects.filter(group_key = cleaned_data['key']) group = groups[0] group.players.add(request.user.player) group.save() RoundStandingsCache.clear_group(group) messages.add_message(request, messages.INFO, u"Uspešno si se dodao u ekipu") else: form_add_group = AddToGroupForm(request.user.player, group_key={}) groups = Group.objects.filter(player__in=[request.user.player]) return render(request, "profile.html", {"form": form, "form_new_group": form_new_group, "form_add_group": form_add_group, "groups": groups, "current_user": request.user.player} )
def paypal(request): email = 'not logged' success = False if request.user.is_authenticated: email = request.user.email request.user.player.in_money = True request.user.player.save() groups = Group.objects.filter(id=1) group = groups[0] group.players.add(request.user) group.save() RoundStandingsCache.clear_group(group) success = True msg = EmailMessage(_('[sharkz.bet] Player payed paypal'), _('Player %s') % email, '*****@*****.**', to=['*****@*****.**', ]) msg.content_subtype = 'html' msg.send(fail_silently=True) return render(request, 'paypal.html', {'success': success})
def round_standings(request, round_id): this_round = get_object_or_404(Round, pk=int(round_id)) matches = Match.objects.select_related('round', 'home_team', 'away_team').\ filter(round=this_round).order_by('start_time', 'id') selected_group = request.GET.get('group', '') logger.info('User %s is on round standings page for round %s for group %s', request.user, this_round.name, selected_group) all_groups = Group.objects.filter(players__in=[request.user]) group = Group.objects.filter(players__in=[request.user]).filter( name=selected_group) if len(group) != 1: selected_group = '' group = None else: group = group[0] can_see_standings = False is_any_match_started = False for match in matches: if match.start_time < timezone.now(): is_any_match_started = True break if is_any_match_started: can_see_standings = True else: logged_user_rounds = UserRound.objects.filter(user=request.user, round=this_round) if len(logged_user_rounds ) == 1 and not logged_user_rounds[0].shot_allowed: can_see_standings = True round_standings_list = [] if can_see_standings: round_standings_list = RoundStandingsCache(this_round, group).get() return render( request, 'roundstandings.html', { 'can_see_standings': can_see_standings, 'matches': matches, 'round_standings': round_standings_list, 'round': this_round, 'groups': all_groups, 'selected_group': selected_group })
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})