Skip to content

Commit

Permalink
#105 just use forms with post for theme selection -> much better and …
Browse files Browse the repository at this point in the history
…less code as well
  • Loading branch information
ephes committed Sep 3, 2023
1 parent 41abb4c commit d890d8f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
16 changes: 8 additions & 8 deletions cast/templates/cast/bootstrap4/select_theme.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</a>
<div class="dropdown-menu">
{% for theme_slug, theme_name in template_base_dir_choices %}
<a class="dropdown-item"
hx-post="{% url 'cast:select-theme' %}"
hx-vals='{"template_base_dir": "{{ theme_slug }}", "next": "{{ next_url }}"}'
hx-trigger="click"
href="#"
>
{{ theme_name }}
</a>
<form action="{% url 'cast:select-theme' %}" method="post">
{% csrf_token %}
<input type="hidden" name="template_base_dir" value="{{ theme_slug }}">
<input type="hidden" name="next" value="{{ next_url }}">
<button type="submit" class="btn dropdown-item">
{{ theme_name }}
</button>
</form>
{% endfor %}
</div>
</div>
Expand Down
10 changes: 2 additions & 8 deletions cast/views/theme.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing import Union

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django_htmx.http import HttpResponseLocation

from ..forms import SelectThemeForm
from ..models import HtmxHttpRequest, get_template_base_dir
Expand All @@ -13,7 +10,7 @@ def set_template_base_dir(request: HtmxHttpRequest, template_base_dir: str) -> N
request.session["template_base_dir"] = template_base_dir


def select_theme(request: HtmxHttpRequest) -> Union[HttpResponse, HttpResponseLocation]:
def select_theme(request: HtmxHttpRequest) -> HttpResponse:
"""
Store the selected theme in the session. This is used to
determine the template base directory for rendering the
Expand All @@ -28,10 +25,7 @@ def select_theme(request: HtmxHttpRequest) -> Union[HttpResponse, HttpResponseLo
if form.is_valid():
set_template_base_dir(request, form.cleaned_data["template_base_dir"])
success_url = form.cleaned_data["next"]
if request.htmx:
return HttpResponseLocation(success_url)
else:
return HttpResponseRedirect(success_url)
return HttpResponseRedirect(success_url)
else:
form = SelectThemeForm(
initial={
Expand Down
23 changes: 0 additions & 23 deletions tests/theme_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import shutil
from pathlib import Path

Expand Down Expand Up @@ -143,25 +142,3 @@ def test_post_select_theme_view_happy(client):
assert response.status_code == 302
assert next_url == response.url
assert client.session["template_base_dir"] == "plain"


@pytest.mark.django_db
def test_post_select_theme_view_happy_htmx(client):
# Given plain as theme and a next url
url = reverse("cast:select-theme")
theme, next_url = "plain", "/next-url/"
# When we post to the select theme view
headers = {"HTTP_HX-Request": "true"}
response = client.post(
url,
{
"template_base_dir": theme,
"next": next_url,
},
**headers,
)
# Then we are redirected to the next url and the theme is stored in the session
assert response.status_code == 200
actual_next_url = json.loads(response.headers["HX-Location"])["path"]
assert actual_next_url == next_url
assert client.session["template_base_dir"] == "plain"

0 comments on commit d890d8f

Please sign in to comment.