Exemplo n.º 1
0
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})
Exemplo n.º 2
0
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})
Exemplo n.º 3
0
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})
Exemplo n.º 4
0
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
            user_rounds = UserRound.objects.filter(round=match.round)
            for user_round in user_rounds:
                cache.delete("shots_by_user_round/%d" % user_round.id)
            groups = Group.objects.all()
            for group in groups:
                cache.delete("standings/%s" % hashlib.sha256(group.name.encode('utf-8')).hexdigest())
            cache.delete("standings/")

            # 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_to_response("admin_results_change.html", {"form": form, "match": match}, context_instance=RequestContext(request))
    def setUp(self):
        super().setUp()
        self.client = Client()
        self.assertTrue(self.client.login(username='******', password='******'))

        # Recalculate all points in DB (we don't trust fixtures to contain properly calculated points)
        logic.recalculate_round_points(1)
        logic.recalculate_round_points(2)
        logic.recalculate_round_points(3)
        logic.recalculate_total_points()

        # Clear cache
        groups = models.Group.objects.all()
        rounds = models.Round.objects.all()
        for group in groups:
            cache.StandingsCache(group).clear()
        cache.StandingsCache().clear()
        for nmk_round in rounds:
            cache.RoundStandingsCache.clear_round(nmk_round)
    def setUp(self):
        super().setUp()
        self.client = Client()
        self.assertTrue(
            self.client.login(username='******', password='******'))

        # Recalculate all points in DB (we don't trust fixtures to contain properly calculated points)
        logic.recalculate_round_points(1)
        logic.recalculate_round_points(2)
        logic.recalculate_round_points(3)
        logic.recalculate_total_points()

        # Clear cache
        groups = models.Group.objects.all()
        rounds = models.Round.objects.all()
        for group in groups:
            cache.StandingsCache(group).clear()
        cache.StandingsCache().clear()
        for nmk_round in rounds:
            cache.RoundStandingsCache.clear_round(nmk_round)