Skip to content

Commit

Permalink
Ensure 303 returned on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Jan 7, 2021
1 parent 507499f commit 0bca414
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.0.16] - 2021-1-7

Ensure all form mixins/views return a 303 on redirect as per Turbo docs.

## [0.0.15] - 2021-1-7

Removed protocol classes and mypy pre-commit requirement
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Third Party Libraries
from setuptools import setup

version = "0.0.15"
version = "0.0.16"

setup(
name="django-turbo-response",
Expand Down
16 changes: 14 additions & 2 deletions src/turbo_response/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Django
from django import forms
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect

# Local
from .renderers import Action
Expand Down Expand Up @@ -93,14 +93,26 @@ def render_turbo_stream_template_response(

class TurboFormMixin:
"""Mixin for handling form validation. Ensures response
has 422 status on invalid"""
has 422 status on invalid and 303 on success"""

def form_invalid(self, form: forms.Form) -> HttpResponse:
return self.render_to_response(
self.get_context_data(form=form),
status=http.HTTPStatus.UNPROCESSABLE_ENTITY,
)

def form_valid(self, form: forms.Form) -> HttpResponse:
return HttpResponseRedirect(
self.get_success_url(), status=http.HTTPStatus.SEE_OTHER
)


class TurboFormModelMixin(TurboFormMixin):
def form_valid(self, form):
"""If the form is valid, save the associated model."""
self.object = form.save()
return super().form_valid(form)


class TurboFrameResponseMixin:
turbo_frame_dom_id = None
Expand Down
3 changes: 3 additions & 0 deletions src/turbo_response/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_post_success(self, rf):
req = rf.post("/", {"description": "ok"})
resp = self.MyView.as_view()(req)
assert resp.url == "/done/"
assert resp.status_code == http.HTTPStatus.SEE_OTHER
assert TodoItem.objects.count() == 1


Expand All @@ -130,6 +131,7 @@ def test_post_success(self, rf, todo):
req = rf.post("/", {"description": "updated!"})
resp = self.MyView.as_view()(req, pk=todo.pk)
assert resp.url == "/done/"
assert resp.status_code == http.HTTPStatus.SEE_OTHER
todo.refresh_from_db()
assert todo.description == "updated!"

Expand Down Expand Up @@ -157,6 +159,7 @@ def test_post_with_validation_errors(self, rf):
def test_post_success(self, rf):
req = rf.post("/", {"description": "ok"})
resp = self.MyView.as_view()(req)
assert resp.status_code == http.HTTPStatus.SEE_OTHER
assert resp.url == "/done/"


Expand Down
5 changes: 3 additions & 2 deletions src/turbo_response/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from . import Action
from .mixins import (
TurboFormMixin,
TurboFormModelMixin,
TurboFrameResponseMixin,
TurboFrameTemplateResponseMixin,
TurboStreamResponseMixin,
Expand Down Expand Up @@ -43,11 +44,11 @@ class TurboFormView(TurboFormMixin, FormView):
...


class TurboCreateView(TurboFormMixin, CreateView):
class TurboCreateView(TurboFormModelMixin, CreateView):
...


class TurboUpdateView(TurboFormMixin, UpdateView):
class TurboUpdateView(TurboFormModelMixin, UpdateView):
...


Expand Down

0 comments on commit 0bca414

Please sign in to comment.