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 admin_rounds_edit(request): if request.method == 'POST': form = RoundForm(request.POST) if form.is_valid(): new_round = form.save() users = User.objects.all() for user in users: user_round = UserRound(user=user, round=new_round, shot_allowed=True, points=0) user_round.save() 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) messages.add_message( request, messages.INFO, _('New round "%s" created successfully') % new_round.name) return HttpResponseRedirect('/admin/rounds') else: form = RoundForm() return render(request, 'admin_rounds_edit.html', { 'form': form, })
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 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 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 standings(request): selected_group = request.GET.get('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] logger.info('User %s is on standings page for group %s', request.user, selected_group) rounds = Round.objects.order_by('id') nmk_standings = StandingsCache(group).get(rounds) return render(request, 'standings.html', {'rounds': rounds, 'standings': nmk_standings, 'groups': all_groups, 'selected_group': selected_group} )