Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jinja #16

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ Temporary Items
.directory


### VS Code ###
.vscode/

### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
name: djlint
entry: poetry run djlint --reformat
language: system
types_or: [html]
types_or: [html,jinja]

## system
- repo: https://github.com/asottile/pyupgrade
Expand Down
687 changes: 281 additions & 406 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
"serve": "vite preview"
},
"devDependencies": {
"@tailwindcss/forms": "^0.4.0",
"@tailwindcss/forms": "^0.5.2",
"@types/alpinejs": "^3.7.0",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"autoprefixer": "^10.4.1",
"eslint": "^8.6.0",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"autoprefixer": "^10.4.7",
"eslint": "^8.15.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.25.4",
"fast-glob": "^3.2.7",
"postcss": "^8.4.5",
"stylelint": "^14.2.0",
"eslint-plugin-import": "^2.26.0",
"fast-glob": "^3.2.11",
"postcss": "^8.4.14",
"stylelint": "^14.8.2",
"stylelint-config-standard-scss": "^3.0.0",
"tailwindcss": "^3.0.21",
"typescript": "^4.5.4",
"vite": "^2.8.6"
"tailwindcss": "^3.0.24",
"typescript": "^4.6.4",
"vite": "^2.9.9"
},
"dependencies": {
"@alpinejs/morph": "^3.9.1",
"alpinejs": "^3.8.1",
"@alpinejs/morph": "^3.10.2",
"alpinejs": "^3.10.2",
"htmx.org": "^1.7.0"
}
}
114 changes: 68 additions & 46 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ flake8-builtins = "^1.5.3"
flake8-eradicate = "^1.2.0"
flake8-mutable = "^1.2.0"
# flake8-django = "^1.1.2"
black = "^21.12b0"
black = "^22.3.0"
isort = "^5.10.1"
djlint = "^0.7.4"
djlint = "1.0.3"
pudb = "^2022.1"
poetryup = "^0.5.1"
pylint-celery = "^0.3"
Expand Down Expand Up @@ -144,6 +144,7 @@ pyflakes = ["+*", "-E5110"]
[tool.djlint]
profile="django"
ignore="H023"
custom_blocks="trans"


[build-system]
Expand Down
53 changes: 53 additions & 0 deletions sampleapp/config/jinja2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Example options from the docs
options = {
"tests": {
"mytest": "path.to.tests.mytestfn",
},
"filters": {
"myfilter": "path.to.filters.myfilterfn",
},
"constants": {
"hello": "hello world",
},
"globals": {
"somefn": "path.to.functions.somefn",
},
}
"""
import datetime
import random
from math import ceil, floor


def random_chart(
num_times: int = 10, num_vars: int = 2, starting_val: int = 100
) -> list[list[int]]:
results = [[starting_val for i in range(num_vars)]]
for _time in range(num_times - 1):
previous_mean = sum(results[-1]) / num_vars
results.append([])
for var in range(num_vars):
previous_val = (results[-2][var] + previous_mean) / 2
results[-1].append(
random.randrange( # noqa
floor(0.5 * previous_val), ceil(2 * previous_val) # noqa
) # noqa
)
return results


options = {
"constants": {"csrf_cookie_name": "sampleapp"},
"filters": {
"template_localtime": "django.utils.timezone.template_localtime",
},
"globals": {
"vite_asset": "django_vite.templatetags.django_vite.vite_asset",
"vite_hmr_client": "django_vite.templatetags.django_vite.vite_hmr_client",
"django_htmx_script": "django_htmx.templatetags.django_htmx.django_htmx_script",
"now": datetime.datetime.utcnow,
"template_localtime": "django.utils.timezone.template_localtime",
"random_chart": random_chart,
},
}
14 changes: 13 additions & 1 deletion sampleapp/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.core.exceptions import ImproperlyConfigured
from environ import Env, Path

from ..jinja2 import options

root = Path(__file__) - 3

env = Env()
Expand Down Expand Up @@ -71,6 +73,7 @@
"cachalot",
"django_extensions",
"django_htmx",
"django_jinja",
"django_vite",
"django_components",
"model_utils",
Expand Down Expand Up @@ -189,7 +192,15 @@


# https://docs.djangoproject.com/en/dev/ref/settings/#templates

TEMPLATES = [
{
# https://niwi.nz/django-jinja/latest/
"BACKEND": "django_jinja.backend.Jinja2",
"DIRS": [root("templates")],
"APP_DIRS": True,
"OPTIONS": options,
},
{
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
"BACKEND": "django.template.backends.django.DjangoTemplates",
Expand All @@ -210,7 +221,7 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
"context_processors": CONTEXT_PROCESSORS,
},
}
},
]

# components settings
Expand Down Expand Up @@ -300,6 +311,7 @@ def prefixed_cookie(name):
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "none"
ACCOUNT_SIGNUP_EMAIL_ENTER_TWICE = True
ACCOUNT_TEMPLATE_EXTENSION = "jinja"

# https://django-allauth.readthedocs.io/en/latest/forms.html#account-forms
ACCOUNT_FORMS = {
Expand Down
4 changes: 3 additions & 1 deletion sampleapp/config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#internal-ips
INTERNAL_IPS = ["127.0.0.1", "10.0.2.2"]


"""
Only use in some dev conditions
class InvalidVariable(str):
def __bool__(self):
return False


TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = InvalidVariable("BAD TEMPLATE VARIABLE: %s")
"""
import sys

TESTING = sys.argv[1:2] == ["test"]
Expand Down
7 changes: 6 additions & 1 deletion sampleapp/home/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

urlpatterns = [
path(r"current-time/", current_time, name="current_time"),
path(
"random-chart/",
TemplateView.as_view(template_name="random_chart.jinja"),
name="random_chart",
),
path(r"error/", error, name="error"),
path(r"", TemplateView.as_view(template_name="index.html"), name="home"),
path(r"", TemplateView.as_view(template_name="index.jinja"), name="home"),
]
2 changes: 1 addition & 1 deletion sampleapp/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def error(request):
def current_time(request):
"""Generate the current time. Useful for testing htmx"""
messages.info(request, "updated the current time")
return TemplateResponse(request, "current_time.html")
return TemplateResponse(request, "current_time.jinja")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "base.jinja" %}
{% block title %}
Forbidden (403)
{% endblock title %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "base.jinja" %}
{% block title %}
Page not found
{% endblock title %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "base.jinja" %}
{% block title %}
Server Error
{% endblock title %}
Expand Down
17 changes: 17 additions & 0 deletions sampleapp/templates/account/account_base.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% from 'account/components/account_box.jinja' import account_box %}
{% extends "base.jinja" %}
{% set account_back %}
{% block account_back %}
{% endblock account_back %}
{% endset %}
{% set account_title %}
{% block account_title %}
{% endblock account_title %}
{% endset %}
{% set account_content %}
{% block account_content %}
{% endblock account_content %}
{% endset %}
{% block content %}
{{ account_box(account_title, account_content, back=account_back) }}
{% endblock content %}
11 changes: 0 additions & 11 deletions sampleapp/templates/account/account_inactive.html

This file was deleted.

13 changes: 13 additions & 0 deletions sampleapp/templates/account/account_inactive.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "account/account_base.jinja" %}
{% block account_title %}
{% trans %}
Account Inactive
{% endtrans %}
{% endblock account_title %}
{% block account_content %}
<p>
{% trans %}
This account is inactive.
{% endtrans %}
</p>
{% endblock account_content %}
1 change: 1 addition & 0 deletions sampleapp/templates/account/already_logged_in.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3>You are logged in as {{ request.user.email }}</h3>
28 changes: 0 additions & 28 deletions sampleapp/templates/account/components/account_box.html

This file was deleted.

43 changes: 43 additions & 0 deletions sampleapp/templates/account/components/account_box.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% set default_title %}
Did YOU ever hear the Tragedy of Darth Plagueis the Wise?
{% endset %}
{% set default_content %}
<h2>I thought not. It's not a story the Jedi would tell you.</h2>
{% endset %}
{% set default_back %}
<a class="absolute top-6 left-6"
hx-get="{{ url('user:account_welcome') }}"
hx-select="#account-box"
hx-target="#account-box"
hx-swap="outerHTML">
<svg xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2">
<path stroke-linecap="round"
stroke-linejoin="round"
d="M11 17l-5-5m0 0l5-5m-5 5h12"/>
</svg>
</a>
{% endset %}
{% macro account_box(title=default_title, content=default_content, back=default_back ) %}
<div class="mt-20 sm:mx-auto sm:w-full sm:max-w-md" id="account-box">
{% if back %}
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 flex flex-col justify-center relative">
{{ back }}
<img class="mx-auto h-12 w-auto"
src="{{ static("img/logo.svg") }}"
alt="logo">
</div>
{% else %}
<img class="mx-auto h-12 w-auto"
src="{{ static("img/logo.svg") }}"
alt="logo">
{% endif %}
<h2 class="mt-2 mb-4 text-center text-xl font-bold text-gray-900"
slot="title">{{ title }}</h2>
{{ content }}
</div>
{% endmacro %}
Loading