-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#105 select theme view + tests and some refactoring
- Loading branch information
Showing
10 changed files
with
182 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<form action='{% url "cast:select-theme" %}' method="post"> | ||
{% csrf_token %} | ||
{{ theme_form.as_p }} | ||
<input role="button" type="submit" /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from django.contrib.auth.models import User | ||
from django.http import HttpRequest | ||
from django_htmx.middleware import HtmxDetails | ||
|
||
|
||
class AuthenticatedHttpRequest(HttpRequest): | ||
user: User | ||
|
||
|
||
class HtmxHttpRequest(HttpRequest): | ||
htmx: HtmxDetails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 | ||
|
||
|
||
def select_theme(request: HtmxHttpRequest) -> Union[HttpResponse, HttpResponseLocation]: | ||
""" | ||
Store the selected theme in the session. This is used to | ||
determine the template base directory for rendering the | ||
blog and episode pages. | ||
""" | ||
template_base_dir = get_template_base_dir(request, None) | ||
# use the referer as the next url if it exists because request.path is | ||
# the url of the select theme view, and we want to redirect to the previous page | ||
next_url = request.headers.get("referer", request.path) | ||
if request.method == "POST": | ||
form = SelectThemeForm(request.POST) | ||
if form.is_valid(): | ||
request.session["template_base_dir"] = form.cleaned_data["template_base_dir"] | ||
success_url = form.cleaned_data["next"] | ||
if request.htmx: | ||
return HttpResponseLocation(success_url) | ||
else: | ||
return HttpResponseRedirect(success_url) | ||
else: | ||
form = SelectThemeForm( | ||
initial={ | ||
"template_base_dir": template_base_dir, | ||
"next": next_url, | ||
} | ||
) | ||
context = { | ||
"theme_form": form, | ||
"template_base_dir": template_base_dir, | ||
"template_base_dir_choices": form.fields["template_base_dir"].choices, | ||
"next_url": next_url, | ||
} | ||
return render(request, f"cast/{template_base_dir}/select_theme.html", context=context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters