def judgement_brawl(request, contest_slug):
    """Renders the decision for the brawl

    Args:
        request ([request]): HTML request
        contest_id (int, optional): The particular type of contest"""
    brawl = get_object_or_404(Contest, slug=contest_slug)
    entries = Entry.objects.filter(
        contest_id=brawl.id).select_related("contest")
    submitted = False
    preview = ""
    form = FinalWordsBrawlForm(request.POST or None,
                               instance=brawl,
                               entries=entries)
    if request.method == "POST" and form.is_valid():
        #check if this is the final judgement
        if not form.cleaned_data.get("final"):
            submitted = True
            preview = form.cleaned_data.get("final_words")
        else:
            form.save()
            brawl.get_brawl()
            brawl.judge(form.cleaned_data.get("winner").pk)
            messages.success(request, "Brawl judgement has been handed down.")
            return redirect("view contest judgement",
                            contest_slug=contest_slug)
    context = {
        "form": form,
        "entries_context": entries,
        "brawl_context": brawl,
        "submitted": submitted,
        "preview": preview,
    }
    return render(request, "promptarena/judgement-brawl.html", context)
 def test_finalwords_brawl(self):
     """test finalwords form is valid"""
     self.create_brawl()
     self.create_brawl(challenger = self.challenger, defender = self.defender, judge =  self.brawl_judge, title="Brawl test")
     # copy the original qs for entries
     entries = Entry.objects.filter(contest=self.contest).select_related("contest")
     for entry in entries:
         winner = entry
     self.form = FinalWordsBrawlForm(
         data={
             "final_words": "these are the final words on the matter",
             "final": True,
             "winner": winner,
         },
         entries=entries,
     )
     self.assertTrue(self.form.is_valid())
 def test_start_brawl(self):
     """Test challenge form is valid"""
     self.form = BrawlChallengeForm(
         data={
             "challenger": self.challenger,
             "defender": self.defender,
             "challenge": "Fight me!",
         }
     )
     self.assertTrue(self.form.is_valid())
 def test_create_brawl_form_invalid(self):
     """test create brawl form with invalid data"""
     self.form = CreateBrawlForm(
         data={
             "title": "BRAWL NEEDS JUDGE:",
             "content": "Content",
             "max_wordcount": 1000,
             "start_date": timezone.now(),
             "expiry_date": timezone.now() + timezone.timedelta(7),
             "mode": Contest.BRAWL,
         }
     )
     self.assertFalse(self.form.is_valid())
 def test_create_brawl_form(self):
     """test create brawl form is valid"""
     self.form = CreateBrawlForm(
         data={
             "title": "Brawl",
             "content": "Content",
             "max_wordcount": 1000,
             "start_date": timezone.now(),
             "expiry_date": timezone.now() + timezone.timedelta(7),
         }
     )
     #self.form["mode"].value = Contest.BRAWL
     self.assertTrue(self.form.is_valid())
 def test_respond_to_brawl_invalid(self):
     """test response form with invalid data"""
     self.form = BrawlResponseForm(
         data={"accepted": BrawlChallenge.NOT_YET, "response": "I will fight you "}
     )
     self.assertFalse(self.form.is_valid())
 def test_respond_to_brawl(self):
     """test response form is valid"""
     self.form = BrawlResponseForm(
         data={"accepted": BrawlChallenge.YES, "response": "I will fight you "}
     )
     self.assertTrue(self.form.is_valid())
class BrawlFormsTest(UniversalAppTestCase):
    """Tetss the brawl form"""
    def setUp(self):
        """sets up brawl peeples"""
        self.create_brawl_participants()

    def test_start_brawl(self):
        """Test challenge form is valid"""
        self.form = BrawlChallengeForm(
            data={
                "challenger": self.challenger,
                "defender": self.defender,
                "challenge": "Fight me!",
            }
        )
        self.assertTrue(self.form.is_valid())

    def test_respond_to_brawl(self):
        """test response form is valid"""
        self.form = BrawlResponseForm(
            data={"accepted": BrawlChallenge.YES, "response": "I will fight you "}
        )
        self.assertTrue(self.form.is_valid())

    def test_respond_to_brawl_invalid(self):
        """test response form with invalid data"""
        self.form = BrawlResponseForm(
            data={"accepted": BrawlChallenge.NOT_YET, "response": "I will fight you "}
        )
        self.assertFalse(self.form.is_valid())

    def test_create_brawl_form(self):
        """test create brawl form is valid"""
        self.form = CreateBrawlForm(
            data={
                "title": "Brawl",
                "content": "Content",
                "max_wordcount": 1000,
                "start_date": timezone.now(),
                "expiry_date": timezone.now() + timezone.timedelta(7),
            }
        )
        #self.form["mode"].value = Contest.BRAWL
        self.assertTrue(self.form.is_valid())

    def test_create_brawl_form_invalid(self):
        """test create brawl form with invalid data"""
        self.form = CreateBrawlForm(
            data={
                "title": "BRAWL NEEDS JUDGE:",
                "content": "Content",
                "max_wordcount": 1000,
                "start_date": timezone.now(),
                "expiry_date": timezone.now() + timezone.timedelta(7),
                "mode": Contest.BRAWL,
            }
        )
        self.assertFalse(self.form.is_valid())

    def test_finalwords_brawl(self):
        """test finalwords form is valid"""
        self.create_brawl()
        self.create_brawl(challenger = self.challenger, defender = self.defender, judge =  self.brawl_judge, title="Brawl test")
        # copy the original qs for entries
        entries = Entry.objects.filter(contest=self.contest).select_related("contest")
        for entry in entries:
            winner = entry
        self.form = FinalWordsBrawlForm(
            data={
                "final_words": "these are the final words on the matter",
                "final": True,
                "winner": winner,
            },
            entries=entries,
        )
        self.assertTrue(self.form.is_valid())