def _post_new_invitation(request): invitation = Invitation(from_user=request.user) form = InvitationForm(instance=invitation, data=request.POST) if form.is_valid(): form.save() return redirect('player_home') else: return render(request, "player/new_invitation_form.html", {"form": form})
def new_invitation(request): if request.method == "POST": invitation = Invitation(from_user=request.user) form = InvitationForm(instance=invitation, data=request.POST) if form.is_valid(): form.save() return redirect('player_home') else: form = InvitationForm() return render(request, "player/new_invitation_form.html", {'form': form})
def new_invitation(request: HttpRequest): """ Displays an InvitationForm for filling out on GET, redirects to Home() on POST success, redirects back to the form with validation errors on POST if validation failed :param request: :return: Form() in the Context() on validation error, redirect to Home() on success """ if request.method == 'POST': # dont forget - form does not display "from" in the invitation, lets fill it: invitation = Invitation(from_user=request.user) form = InvitationForm(instance=invitation, data=request.POST) # now form shall be fine - all fields present. Lets check if input is indeed ok: if form.is_valid(): form.save() return redirect('player_home') else: form = InvitationForm() return render(request, 'player/new_invitation_form.html', {'form': form})
def test_accept_invitation_request_guard(self): user1 = User.objects.get_by_natural_key(self.user1["username"]) user2 = User.objects.get_by_natural_key(self.user2["username"]) invitation = Invitation() invitation.from_user = user1 invitation.to_user = user2 invitation.save() url = reverse("player_accept_invitation", kwargs={"id": 1}) self.client.login(username=self.user1["username"], password=self.user1["password"]) response = self.client.get(url) self.assertEqual(response.status_code, 403)
def test_deny_invitation_post(self): user1 = User.objects.get_by_natural_key(self.user1["username"]) user2 = User.objects.get_by_natural_key(self.user2["username"]) invitation = Invitation() invitation.from_user = user1 invitation.to_user = user2 invitation.save() url = reverse("player_accept_invitation", kwargs={"id": 1}) self.client.login(username=self.user2["username"], password=self.user2["password"]) data = {"deny": "no"} response = self.client.post(url, data) self.assertRedirects(response, reverse("player_home")) self.assertEqual(Invitation.objects.count(), 0)