Skip to content

Commit 6db0e0f

Browse files
committed
Add bootstrap_email Django template tag
1 parent a0f70c9 commit 6db0e0f

16 files changed

+371
-0
lines changed

.github/workflows/publish.yaml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Publish Python Package
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
python-version: ["3.9", "3.10", "3.11", "3.12"]
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
cache: pip
23+
cache-dependency-path: pyproject.toml
24+
- name: Install dependencies
25+
run: |
26+
pip install .
27+
- name: Run tests
28+
run: |
29+
python runtests.py
30+
deploy:
31+
runs-on: ubuntu-latest
32+
needs: [test]
33+
environment: release
34+
permissions:
35+
id-token: write
36+
steps:
37+
- uses: actions/checkout@v4
38+
- name: Set up Python
39+
uses: actions/setup-python@v5
40+
with:
41+
python-version: "3.12"
42+
cache: pip
43+
cache-dependency-path: pyproject.toml
44+
- name: Install dependencies
45+
run: |
46+
pip install setuptools wheel build
47+
- name: Build
48+
run: |
49+
python -m build
50+
- name: Publish
51+
uses: pypa/gh-action-pypi-publish@release/v1

.github/workflows/test.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
python-version: ["3.9", "3.10", "3.11", "3.12"]
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Set up Python ${{ matrix.python-version }}
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: ${{ matrix.python-version }}
20+
cache: pip
21+
cache-dependency-path: pyproject.toml
22+
- name: Install dependencies
23+
run: |
24+
pip install .
25+
- name: Run tests
26+
run: |
27+
python runtests.py

.gitignore

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
# Byte-compiled / optimized / DLL files
3+
__pycache__/
4+
*.py[cod]
5+
*$py.class
6+
7+
# C extensions
8+
*.so
9+
10+
# Distribution / packaging
11+
.Python
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
cover/
54+
55+
# Translations
56+
*.mo
57+
*.pot
58+
59+
# Django stuff:
60+
*.log
61+
local_settings.py
62+
db.sqlite3
63+
db.sqlite3-journal
64+
65+
# Flask stuff:
66+
instance/
67+
.webassets-cache
68+
69+
# Scrapy stuff:
70+
.scrapy
71+
72+
# Sphinx documentation
73+
docs/_build/
74+
75+
# PyBuilder
76+
.pybuilder/
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# pyenv
87+
# For a library or package, you might want to ignore these files since the code is
88+
# intended to run in multiple environments; otherwise, check them in:
89+
# .python-version
90+
91+
# pipenv
92+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
94+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
95+
# install all needed dependencies.
96+
#Pipfile.lock
97+
98+
# poetry
99+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100+
# This is especially recommended for binary packages to ensure reproducibility, and is more
101+
# commonly ignored for libraries.
102+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103+
#poetry.lock
104+
105+
# pdm
106+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107+
#pdm.lock
108+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109+
# in version control.
110+
# https://pdm.fming.dev/#use-with-ide
111+
.pdm.toml
112+
113+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
114+
__pypackages__/
115+
116+
# Celery stuff
117+
celerybeat-schedule
118+
celerybeat.pid
119+
120+
# SageMath parsed files
121+
*.sage.py
122+
123+
# Environments
124+
.env
125+
.venv
126+
env/
127+
venv/
128+
ENV/
129+
env.bak/
130+
venv.bak/
131+
132+
# Spyder project settings
133+
.spyderproject
134+
.spyproject
135+
136+
# Rope project settings
137+
.ropeproject
138+
139+
# mkdocs documentation
140+
/site
141+
142+
# mypy
143+
.mypy_cache/
144+
.dmypy.json
145+
dmypy.json
146+
147+
# Pyre type checker
148+
.pyre/
149+
150+
# pytype static type analyzer
151+
.pytype/
152+
153+
# Cython debug symbols
154+
cython_debug/
155+
156+
# Vscode
157+
.vscode/
158+
159+
# ruff
160+
.ruff_cache/
161+
162+
# PyCharm
163+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
164+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
165+
# and can be added to the global gitignore or merged into this file. For a more nuclear
166+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
167+
#.idea/

django_bootstrap_email/__init__.py

Whitespace-only changes.

django_bootstrap_email/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

django_bootstrap_email/apps.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BootstrapemailConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "django_bootstrap_email"
7+
label = "bootstrap_email"

django_bootstrap_email/migrations/__init__.py

Whitespace-only changes.

django_bootstrap_email/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

django_bootstrap_email/templatetags/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from bootstrap_email.compiler import compile
2+
from django import template
3+
4+
register = template.Library()
5+
6+
7+
@register.tag
8+
def bootstrap_email(parser, token):
9+
nodelist = parser.parse(("end_bootstrap_email",))
10+
parser.delete_first_token()
11+
return BootstrapEmailNode(nodelist)
12+
13+
14+
class BootstrapEmailNode(template.Node):
15+
def __init__(self, children):
16+
self.children = children
17+
18+
def render(self, context):
19+
html = "".join([node.render(context) for node in self.children])
20+
result = compile(html)
21+
return result

django_bootstrap_email/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

pyproject.toml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[project]
2+
name = "django-bs-email"
3+
version = "0.1.0"
4+
description = "Django application with Bootstrap email template tag."
5+
readme = "README.md"
6+
requires-python = ">=3.9"
7+
authors = [{name = "Jeremy Thompson"}]
8+
license = {file = "LICENSE"}
9+
dependencies = [
10+
"bootstrap-email",
11+
"Django"
12+
]
13+
14+
[build-system]
15+
requires = ["setuptools"]
16+
build-backend = "setuptools.build_meta"
17+
18+
[project.urls]
19+
Homepage = "https://github.com/jhthompson/django-bootstrap-email"
20+
Changelog = "https://github.com/jhthompson/django-bootstrap-email/releases"
21+
Issues = "https://github.com/jhthompson/django-bootstrap-email/issues"
22+
CI = "https://github.com/jhthompson/django-bootstrap-email/actions"
23+
24+
[tool.ruff.lint]
25+
select = [
26+
"E", # pycodestyle
27+
"F", # Pyflakes
28+
"UP", # pyupgrade
29+
"B", # flake8-bugbear
30+
"SIM", # flake8-simplify
31+
"I", # isort
32+
]

runtests.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
import django
6+
from django.conf import settings
7+
from django.test.utils import get_runner
8+
9+
if __name__ == "__main__":
10+
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.test_settings"
11+
django.setup()
12+
TestRunner = get_runner(settings)
13+
test_runner = TestRunner()
14+
failures = test_runner.run_tests(["tests"])
15+
sys.exit(bool(failures))

tests/__init__.py

Whitespace-only changes.

tests/test_settings.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
DATABASES = {
2+
"default": {
3+
"ENGINE": "django.db.backends.sqlite3",
4+
"NAME": "bootstrap_email.db",
5+
}
6+
}
7+
8+
TEMPLATES = [
9+
{
10+
"BACKEND": "django.template.backends.django.DjangoTemplates",
11+
"DIRS": [],
12+
"APP_DIRS": True,
13+
},
14+
]
15+
16+
INSTALLED_APPS = [
17+
"django_bootstrap_email",
18+
]

tests/tests.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.template import Context, Template
2+
from django.test import TestCase
3+
4+
5+
class CustomTemplateTagTests(TestCase):
6+
def test_custom_template_tag(self):
7+
# Define the custom template tag usage
8+
template_code = """
9+
{% load bootstrap_email %}
10+
{% bootstrap_email %}
11+
<body class="bg-light">
12+
<p>Hello, world!</p>
13+
</body>
14+
{% end_bootstrap_email %}
15+
"""
16+
17+
# Render the template with the given context
18+
rendered_template = Template(template_code).render(Context({}))
19+
20+
# Assert that it actually compiles
21+
self.assertIn(
22+
"<!-- Compiled with Python Bootstrap Email version:",
23+
rendered_template,
24+
)

0 commit comments

Comments
 (0)