django-freeletter 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_freeletter-0.1.0/.gitignore +9 -0
- django_freeletter-0.1.0/PKG-INFO +138 -0
- django_freeletter-0.1.0/README.md +114 -0
- django_freeletter-0.1.0/pyproject.toml +73 -0
- django_freeletter-0.1.0/src/freeletter/__init__.py +1 -0
- django_freeletter-0.1.0/src/freeletter/admin.py +37 -0
- django_freeletter-0.1.0/src/freeletter/apps.py +21 -0
- django_freeletter-0.1.0/src/freeletter/blocks.py +62 -0
- django_freeletter-0.1.0/src/freeletter/emails.py +55 -0
- django_freeletter-0.1.0/src/freeletter/forms.py +7 -0
- django_freeletter-0.1.0/src/freeletter/management/__init__.py +0 -0
- django_freeletter-0.1.0/src/freeletter/management/commands/__init__.py +0 -0
- django_freeletter-0.1.0/src/freeletter/management/commands/send_freeletter_issues.py +58 -0
- django_freeletter-0.1.0/src/freeletter/migrations/0001_initial.py +158 -0
- django_freeletter-0.1.0/src/freeletter/migrations/__init__.py +0 -0
- django_freeletter-0.1.0/src/freeletter/models.py +135 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/blocks/html.html +2 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/confirmed.html +6 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/email/confirm.txt +5 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/email/confirm_subject.txt +1 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/email/issue.html +13 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/email/issue.txt +6 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/issue_detail.html +9 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/issue_list.html +13 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/subscribe.html +11 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/subscribe_pending.html +6 -0
- django_freeletter-0.1.0/src/freeletter/templates/freeletter/unsubscribed.html +6 -0
- django_freeletter-0.1.0/src/freeletter/urls.py +18 -0
- django_freeletter-0.1.0/src/freeletter/urls_helpers.py +23 -0
- django_freeletter-0.1.0/src/freeletter/views.py +93 -0
- django_freeletter-0.1.0/tests/__init__.py +0 -0
- django_freeletter-0.1.0/tests/settings.py +56 -0
- django_freeletter-0.1.0/tests/templates/freeletter_test/echo_block.html +1 -0
- django_freeletter-0.1.0/tests/test_models.py +59 -0
- django_freeletter-0.1.0/tests/test_send_command.py +64 -0
- django_freeletter-0.1.0/tests/test_views.py +90 -0
- django_freeletter-0.1.0/tests/urls.py +7 -0
- django_freeletter-0.1.0/uv.lock +729 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-freeletter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small Django newsletter engine: subscribers, double opt-in, composable issues, a web archive, and a send queue.
|
|
5
|
+
Project-URL: Homepage, https://github.com/hisie/django-freeletter
|
|
6
|
+
Project-URL: Repository, https://github.com/hisie/django-freeletter
|
|
7
|
+
Project-URL: Issues, https://github.com/hisie/django-freeletter/issues
|
|
8
|
+
Author-email: Diego Cebrian <diego@cebrian-it.fr>
|
|
9
|
+
License: BSD-3-Clause
|
|
10
|
+
Keywords: django,email,mailing-list,newsletter
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Framework :: Django
|
|
13
|
+
Classifier: Framework :: Django :: 5.2
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Communications :: Email
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Requires-Dist: django<5.3,>=5.2
|
|
22
|
+
Requires-Dist: pillow>=12.3.0
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# django-freeletter
|
|
26
|
+
|
|
27
|
+
A small Django newsletter engine: subscribers with double opt-in, issues
|
|
28
|
+
made of ordered content blocks, a web-viewable archive, and a plain
|
|
29
|
+
management-command send queue. No e-commerce framework, no CMS, no
|
|
30
|
+
particular project's assumptions baked in — just Django.
|
|
31
|
+
|
|
32
|
+
## What this package does, and doesn't, do
|
|
33
|
+
|
|
34
|
+
- `Subscriber`: email + optional name, double opt-in (confirm via a
|
|
35
|
+
tokenized email link before ever receiving a send), a separate
|
|
36
|
+
unsubscribe token.
|
|
37
|
+
- `Issue`: a draft → queued → sent newsletter, made of ordered
|
|
38
|
+
`IssueBlock`s.
|
|
39
|
+
- **One built-in block type**: `"html"` — free-form content (rich text +
|
|
40
|
+
an optional image). That's the only content type this package itself
|
|
41
|
+
knows about.
|
|
42
|
+
- A **pluggable block-type registry** (`freeletter.blocks`) — anything
|
|
43
|
+
else (a blog post teaser, a product card, whatever a host project
|
|
44
|
+
wants to feature in an issue) is registered by a *different* package or
|
|
45
|
+
the host project itself, pointing at its own template and its own
|
|
46
|
+
model, via a generic `(content_type, object_id)` pointer. freeletter
|
|
47
|
+
never imports or knows about that other model — see "Registering a
|
|
48
|
+
block type" below.
|
|
49
|
+
- A public archive (`freeletter/`) of sent issues, subscribe/confirm/
|
|
50
|
+
unsubscribe views, and a `send_freeletter_issues` management command
|
|
51
|
+
meant to run off a plain cron/systemd timer, not a task queue.
|
|
52
|
+
- It does **not** ship any dashboard/admin UI beyond Django's own
|
|
53
|
+
`admin.py` registration, any e-commerce integration, or any CTA
|
|
54
|
+
placement — all host-project scope.
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
uv add django-freeletter
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
INSTALLED_APPS = [
|
|
64
|
+
...,
|
|
65
|
+
"django.contrib.contenttypes", # required by IssueBlock's generic FK
|
|
66
|
+
"freeletter",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
FREELETTER_FROM_EMAIL = "newsletter@example.com" # defaults to DEFAULT_FROM_EMAIL
|
|
70
|
+
FREELETTER_BASE_URL = "https://example.com" # required for links in emails sent outside a request (the send command)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
# urls.py
|
|
75
|
+
urlpatterns = [
|
|
76
|
+
path("newsletter/", include("freeletter.urls")),
|
|
77
|
+
]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Run `manage.py migrate`.
|
|
81
|
+
|
|
82
|
+
## Registering a block type
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
# yourapp/apps.py
|
|
86
|
+
from django.apps import AppConfig
|
|
87
|
+
|
|
88
|
+
class YourAppConfig(AppConfig):
|
|
89
|
+
def ready(self):
|
|
90
|
+
from freeletter.blocks import BlockType, register_block_type
|
|
91
|
+
|
|
92
|
+
register_block_type(
|
|
93
|
+
BlockType(
|
|
94
|
+
slug="product",
|
|
95
|
+
label="Product",
|
|
96
|
+
template_name="yourapp/freeletter_blocks/product.html",
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```html
|
|
102
|
+
{# yourapp/freeletter_blocks/product.html — `object` is the resolved content_object #}
|
|
103
|
+
<a href="{{ object.get_absolute_url }}">{{ object.title }} — {{ object.price }}</a>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
An editor then creates an `IssueBlock` with `block_type="product"` and
|
|
107
|
+
`content_object=<some product instance>` (e.g. via Django admin, or a
|
|
108
|
+
host project's own dashboard) — freeletter resolves and renders it
|
|
109
|
+
without ever importing the product model.
|
|
110
|
+
|
|
111
|
+
## Sending
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
manage.py send_freeletter_issues
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Sends every `Issue` with `status="queued"` to every active (`is_confirmed
|
|
118
|
+
and not unsubscribed_at`) subscriber, then marks it `sent`. A single
|
|
119
|
+
recipient's send failure is logged and skipped, not fatal to the run.
|
|
120
|
+
Point a systemd timer or cron entry at this command — there's no daemon
|
|
121
|
+
or task queue involved by design, matching the "prepare for sending in an
|
|
122
|
+
agnostic way" requirement this package was built for: swapping the mail
|
|
123
|
+
transport later (a different SMTP relay, an ESP used as an SMTP backend)
|
|
124
|
+
only ever means changing `EMAIL_BACKEND`, nothing here.
|
|
125
|
+
|
|
126
|
+
Optional batching, if a mail relay needs it:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
FREELETTER_BATCH_SIZE = 50
|
|
130
|
+
FREELETTER_BATCH_DELAY = 2 # seconds, after every FREELETTER_BATCH_SIZE sends
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Development
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
uv sync
|
|
137
|
+
uv run pytest
|
|
138
|
+
```
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# django-freeletter
|
|
2
|
+
|
|
3
|
+
A small Django newsletter engine: subscribers with double opt-in, issues
|
|
4
|
+
made of ordered content blocks, a web-viewable archive, and a plain
|
|
5
|
+
management-command send queue. No e-commerce framework, no CMS, no
|
|
6
|
+
particular project's assumptions baked in — just Django.
|
|
7
|
+
|
|
8
|
+
## What this package does, and doesn't, do
|
|
9
|
+
|
|
10
|
+
- `Subscriber`: email + optional name, double opt-in (confirm via a
|
|
11
|
+
tokenized email link before ever receiving a send), a separate
|
|
12
|
+
unsubscribe token.
|
|
13
|
+
- `Issue`: a draft → queued → sent newsletter, made of ordered
|
|
14
|
+
`IssueBlock`s.
|
|
15
|
+
- **One built-in block type**: `"html"` — free-form content (rich text +
|
|
16
|
+
an optional image). That's the only content type this package itself
|
|
17
|
+
knows about.
|
|
18
|
+
- A **pluggable block-type registry** (`freeletter.blocks`) — anything
|
|
19
|
+
else (a blog post teaser, a product card, whatever a host project
|
|
20
|
+
wants to feature in an issue) is registered by a *different* package or
|
|
21
|
+
the host project itself, pointing at its own template and its own
|
|
22
|
+
model, via a generic `(content_type, object_id)` pointer. freeletter
|
|
23
|
+
never imports or knows about that other model — see "Registering a
|
|
24
|
+
block type" below.
|
|
25
|
+
- A public archive (`freeletter/`) of sent issues, subscribe/confirm/
|
|
26
|
+
unsubscribe views, and a `send_freeletter_issues` management command
|
|
27
|
+
meant to run off a plain cron/systemd timer, not a task queue.
|
|
28
|
+
- It does **not** ship any dashboard/admin UI beyond Django's own
|
|
29
|
+
`admin.py` registration, any e-commerce integration, or any CTA
|
|
30
|
+
placement — all host-project scope.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
uv add django-freeletter
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
INSTALLED_APPS = [
|
|
40
|
+
...,
|
|
41
|
+
"django.contrib.contenttypes", # required by IssueBlock's generic FK
|
|
42
|
+
"freeletter",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
FREELETTER_FROM_EMAIL = "newsletter@example.com" # defaults to DEFAULT_FROM_EMAIL
|
|
46
|
+
FREELETTER_BASE_URL = "https://example.com" # required for links in emails sent outside a request (the send command)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
# urls.py
|
|
51
|
+
urlpatterns = [
|
|
52
|
+
path("newsletter/", include("freeletter.urls")),
|
|
53
|
+
]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Run `manage.py migrate`.
|
|
57
|
+
|
|
58
|
+
## Registering a block type
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
# yourapp/apps.py
|
|
62
|
+
from django.apps import AppConfig
|
|
63
|
+
|
|
64
|
+
class YourAppConfig(AppConfig):
|
|
65
|
+
def ready(self):
|
|
66
|
+
from freeletter.blocks import BlockType, register_block_type
|
|
67
|
+
|
|
68
|
+
register_block_type(
|
|
69
|
+
BlockType(
|
|
70
|
+
slug="product",
|
|
71
|
+
label="Product",
|
|
72
|
+
template_name="yourapp/freeletter_blocks/product.html",
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```html
|
|
78
|
+
{# yourapp/freeletter_blocks/product.html — `object` is the resolved content_object #}
|
|
79
|
+
<a href="{{ object.get_absolute_url }}">{{ object.title }} — {{ object.price }}</a>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
An editor then creates an `IssueBlock` with `block_type="product"` and
|
|
83
|
+
`content_object=<some product instance>` (e.g. via Django admin, or a
|
|
84
|
+
host project's own dashboard) — freeletter resolves and renders it
|
|
85
|
+
without ever importing the product model.
|
|
86
|
+
|
|
87
|
+
## Sending
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
manage.py send_freeletter_issues
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Sends every `Issue` with `status="queued"` to every active (`is_confirmed
|
|
94
|
+
and not unsubscribed_at`) subscriber, then marks it `sent`. A single
|
|
95
|
+
recipient's send failure is logged and skipped, not fatal to the run.
|
|
96
|
+
Point a systemd timer or cron entry at this command — there's no daemon
|
|
97
|
+
or task queue involved by design, matching the "prepare for sending in an
|
|
98
|
+
agnostic way" requirement this package was built for: swapping the mail
|
|
99
|
+
transport later (a different SMTP relay, an ESP used as an SMTP backend)
|
|
100
|
+
only ever means changing `EMAIL_BACKEND`, nothing here.
|
|
101
|
+
|
|
102
|
+
Optional batching, if a mail relay needs it:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
FREELETTER_BATCH_SIZE = 50
|
|
106
|
+
FREELETTER_BATCH_DELAY = 2 # seconds, after every FREELETTER_BATCH_SIZE sends
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Development
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
uv sync
|
|
113
|
+
uv run pytest
|
|
114
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-freeletter"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A small Django newsletter engine: subscribers, double opt-in, composable issues, a web archive, and a send queue."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = { text = "BSD-3-Clause" }
|
|
8
|
+
authors = [{ name = "Diego Cebrian", email = "diego@cebrian-it.fr" }]
|
|
9
|
+
keywords = ["django", "newsletter", "mailing-list", "email"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Framework :: Django",
|
|
12
|
+
"Framework :: Django :: 5.2",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.12",
|
|
15
|
+
"License :: OSI Approved :: BSD License",
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Topic :: Communications :: Email",
|
|
19
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"django>=5.2,<5.3",
|
|
23
|
+
"pillow>=12.3.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://github.com/hisie/django-freeletter"
|
|
28
|
+
Repository = "https://github.com/hisie/django-freeletter"
|
|
29
|
+
Issues = "https://github.com/hisie/django-freeletter/issues"
|
|
30
|
+
|
|
31
|
+
[dependency-groups]
|
|
32
|
+
dev = [
|
|
33
|
+
"black",
|
|
34
|
+
"coverage",
|
|
35
|
+
"isort",
|
|
36
|
+
"mypy",
|
|
37
|
+
"pytest",
|
|
38
|
+
"pytest-django",
|
|
39
|
+
"django-stubs",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[build-system]
|
|
43
|
+
requires = ["hatchling"]
|
|
44
|
+
build-backend = "hatchling.build"
|
|
45
|
+
|
|
46
|
+
[tool.hatch.build.targets.wheel]
|
|
47
|
+
packages = ["src/freeletter"]
|
|
48
|
+
|
|
49
|
+
[tool.black]
|
|
50
|
+
line-length = 100
|
|
51
|
+
target-version = ["py312"]
|
|
52
|
+
|
|
53
|
+
[tool.isort]
|
|
54
|
+
profile = "black"
|
|
55
|
+
line_length = 100
|
|
56
|
+
|
|
57
|
+
[tool.mypy]
|
|
58
|
+
python_version = "3.12"
|
|
59
|
+
strict = true
|
|
60
|
+
plugins = ["mypy_django_plugin.main"]
|
|
61
|
+
mypy_path = "src"
|
|
62
|
+
|
|
63
|
+
[tool.django-stubs]
|
|
64
|
+
django_settings_module = "tests.settings"
|
|
65
|
+
|
|
66
|
+
[tool.pytest.ini_options]
|
|
67
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
68
|
+
python_files = ["test_*.py"]
|
|
69
|
+
testpaths = ["tests"]
|
|
70
|
+
pythonpath = ["."]
|
|
71
|
+
|
|
72
|
+
[tool.coverage.run]
|
|
73
|
+
source = ["src/freeletter"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
|
|
3
|
+
from freeletter.models import Issue, IssueBlock, Subscriber
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@admin.register(Subscriber)
|
|
7
|
+
class SubscriberAdmin(admin.ModelAdmin):
|
|
8
|
+
list_display = ("email", "name", "is_confirmed", "subscribed_at", "unsubscribed_at")
|
|
9
|
+
list_filter = ("is_confirmed",)
|
|
10
|
+
search_fields = ("email", "name")
|
|
11
|
+
readonly_fields = (
|
|
12
|
+
"confirmation_token",
|
|
13
|
+
"unsubscribe_token",
|
|
14
|
+
"subscribed_at",
|
|
15
|
+
"confirmed_at",
|
|
16
|
+
"unsubscribed_at",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class IssueBlockInline(admin.TabularInline):
|
|
21
|
+
model = IssueBlock
|
|
22
|
+
extra = 1
|
|
23
|
+
fields = ("sortorder", "block_type", "html", "image", "content_type", "object_id")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@admin.register(Issue)
|
|
27
|
+
class IssueAdmin(admin.ModelAdmin):
|
|
28
|
+
list_display = ("title", "status", "queued_at", "sent_at")
|
|
29
|
+
list_filter = ("status",)
|
|
30
|
+
prepopulated_fields = {"slug": ("title",)}
|
|
31
|
+
inlines = [IssueBlockInline]
|
|
32
|
+
actions = ["queue_for_sending"]
|
|
33
|
+
|
|
34
|
+
@admin.action(description="Queue selected issues for sending")
|
|
35
|
+
def queue_for_sending(self, request, queryset):
|
|
36
|
+
for issue in queryset.filter(status=Issue.Status.DRAFT):
|
|
37
|
+
issue.queue()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
from django.utils.translation import gettext_lazy as _
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class FreeletterConfig(AppConfig):
|
|
6
|
+
name = "freeletter"
|
|
7
|
+
label = "freeletter"
|
|
8
|
+
verbose_name = "Freeletter"
|
|
9
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
10
|
+
|
|
11
|
+
def ready(self) -> None:
|
|
12
|
+
from freeletter.blocks import BlockType, register_block_type
|
|
13
|
+
|
|
14
|
+
register_block_type(
|
|
15
|
+
BlockType(
|
|
16
|
+
slug="html",
|
|
17
|
+
label=str(_("Free content")),
|
|
18
|
+
template_name="freeletter/blocks/html.html",
|
|
19
|
+
requires_object=False,
|
|
20
|
+
)
|
|
21
|
+
)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Pluggable issue-block types.
|
|
2
|
+
|
|
3
|
+
freeletter itself only knows about one block type ("html", free-form
|
|
4
|
+
content) — it has no idea what any other kind of content might be. A
|
|
5
|
+
host project (or a separate integration package) can register further
|
|
6
|
+
block types here, each pointing at its own template and knowing how to
|
|
7
|
+
resolve its own `content_object`; freeletter's IssueBlock model just
|
|
8
|
+
stores a block_type slug plus a generic (content_type, object_id) pointer
|
|
9
|
+
and defers rendering to whatever's registered. This is what lets an issue
|
|
10
|
+
mix free content with, say, a blog post or a product card, without
|
|
11
|
+
freeletter needing to know what either of those is.
|
|
12
|
+
|
|
13
|
+
Registration happens in an AppConfig.ready() (see apps.py), the same
|
|
14
|
+
"import side effects happen at app-loading time" shape Django itself uses
|
|
15
|
+
for signal receivers — not at import time of this module, so app-loading
|
|
16
|
+
order doesn't matter.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from dataclasses import dataclass
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from django.template.loader import render_to_string
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class BlockType:
|
|
29
|
+
slug: str
|
|
30
|
+
label: str
|
|
31
|
+
template_name: str
|
|
32
|
+
# Whether this block type needs a content_object resolved via the
|
|
33
|
+
# generic (content_type, object_id) pair — False for "html", which
|
|
34
|
+
# carries its own content directly on IssueBlock.
|
|
35
|
+
requires_object: bool = True
|
|
36
|
+
|
|
37
|
+
def render(self, block: Any, context: dict) -> str:
|
|
38
|
+
ctx = {
|
|
39
|
+
**context,
|
|
40
|
+
"block": block,
|
|
41
|
+
"object": block.content_object if self.requires_object else None,
|
|
42
|
+
}
|
|
43
|
+
return render_to_string(self.template_name, ctx)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
_registry: dict[str, BlockType] = {}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def register_block_type(block_type: BlockType) -> None:
|
|
50
|
+
_registry[block_type.slug] = block_type
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def get_block_type(slug: str) -> BlockType:
|
|
54
|
+
return _registry[slug]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_registered_block_types() -> list[BlockType]:
|
|
58
|
+
return list(_registry.values())
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def is_registered(slug: str) -> bool:
|
|
62
|
+
return slug in _registry
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
from django.core.mail import EmailMultiAlternatives
|
|
5
|
+
from django.http import HttpRequest
|
|
6
|
+
from django.template.loader import render_to_string
|
|
7
|
+
from django.urls import reverse
|
|
8
|
+
|
|
9
|
+
from freeletter.models import Issue, Subscriber
|
|
10
|
+
from freeletter.urls_helpers import absolute_url
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _from_email() -> str:
|
|
14
|
+
return getattr(settings, "FREELETTER_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def send_confirmation_email(subscriber: Subscriber, request: HttpRequest | None = None) -> None:
|
|
18
|
+
confirm_url = absolute_url(
|
|
19
|
+
reverse("freeletter:confirm", kwargs={"token": subscriber.confirmation_token}), request
|
|
20
|
+
)
|
|
21
|
+
context = {"subscriber": subscriber, "confirm_url": confirm_url}
|
|
22
|
+
|
|
23
|
+
subject = render_to_string("freeletter/email/confirm_subject.txt", context).strip()
|
|
24
|
+
text_body = render_to_string("freeletter/email/confirm.txt", context)
|
|
25
|
+
|
|
26
|
+
message = EmailMultiAlternatives(
|
|
27
|
+
subject, text_body, from_email=_from_email(), to=[subscriber.email]
|
|
28
|
+
)
|
|
29
|
+
message.send()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def send_issue_email(
|
|
33
|
+
issue: Issue, subscriber: Subscriber, request: HttpRequest | None = None
|
|
34
|
+
) -> None:
|
|
35
|
+
unsubscribe_url = absolute_url(
|
|
36
|
+
reverse("freeletter:unsubscribe", kwargs={"token": subscriber.unsubscribe_token}), request
|
|
37
|
+
)
|
|
38
|
+
archive_url = absolute_url(issue.get_absolute_url(), request)
|
|
39
|
+
context = {
|
|
40
|
+
"issue": issue,
|
|
41
|
+
"subscriber": subscriber,
|
|
42
|
+
"unsubscribe_url": unsubscribe_url,
|
|
43
|
+
"archive_url": archive_url,
|
|
44
|
+
"blocks": [block.render({"request": request}) for block in issue.blocks.all()],
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
subject = issue.effective_subject()
|
|
48
|
+
text_body = render_to_string("freeletter/email/issue.txt", context)
|
|
49
|
+
html_body = render_to_string("freeletter/email/issue.html", context)
|
|
50
|
+
|
|
51
|
+
message = EmailMultiAlternatives(
|
|
52
|
+
subject, text_body, from_email=_from_email(), to=[subscriber.email]
|
|
53
|
+
)
|
|
54
|
+
message.attach_alternative(html_body, "text/html")
|
|
55
|
+
message.send()
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Sends every queued Issue to every active Subscriber.
|
|
2
|
+
|
|
3
|
+
Meant to run on a plain schedule (cron/systemd timer), not a long-lived
|
|
4
|
+
daemon — mirrors this project's general "a timer tick is enough" stance
|
|
5
|
+
rather than assuming a task queue is available. Safe to run again if
|
|
6
|
+
interrupted: only Subscribers who haven't already gotten *this* issue are
|
|
7
|
+
sent to (Issue.status flips to SENT only once the whole run for that
|
|
8
|
+
issue completes), and a single recipient's send failure is logged and
|
|
9
|
+
skipped rather than aborting the rest of the run.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
import time
|
|
16
|
+
|
|
17
|
+
from django.conf import settings
|
|
18
|
+
from django.core.management.base import BaseCommand
|
|
19
|
+
from django.utils import timezone
|
|
20
|
+
|
|
21
|
+
from freeletter.emails import send_issue_email
|
|
22
|
+
from freeletter.models import Issue, Subscriber
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger("freeletter")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Command(BaseCommand):
|
|
28
|
+
help = "Send every queued newsletter issue to every active subscriber."
|
|
29
|
+
|
|
30
|
+
def handle(self, *args, **options):
|
|
31
|
+
batch_size = getattr(settings, "FREELETTER_BATCH_SIZE", 0)
|
|
32
|
+
batch_delay = getattr(settings, "FREELETTER_BATCH_DELAY", 0)
|
|
33
|
+
|
|
34
|
+
for issue in Issue.objects.filter(status=Issue.Status.QUEUED):
|
|
35
|
+
self._send_issue(issue, batch_size, batch_delay)
|
|
36
|
+
|
|
37
|
+
def _send_issue(self, issue: Issue, batch_size: int, batch_delay: float) -> None:
|
|
38
|
+
subscribers = Subscriber.objects.filter(is_confirmed=True, unsubscribed_at__isnull=True)
|
|
39
|
+
logger.info("Sending issue %r to %d subscribers", issue.title, subscribers.count())
|
|
40
|
+
|
|
41
|
+
sent = 0
|
|
42
|
+
failed = 0
|
|
43
|
+
for index, subscriber in enumerate(subscribers, start=1):
|
|
44
|
+
try:
|
|
45
|
+
send_issue_email(issue, subscriber)
|
|
46
|
+
sent += 1
|
|
47
|
+
except Exception:
|
|
48
|
+
failed += 1
|
|
49
|
+
logger.exception("Failed to send issue %r to %s", issue.title, subscriber.email)
|
|
50
|
+
|
|
51
|
+
if batch_size and batch_delay and index % batch_size == 0:
|
|
52
|
+
time.sleep(batch_delay)
|
|
53
|
+
|
|
54
|
+
issue.status = Issue.Status.SENT
|
|
55
|
+
issue.sent_at = timezone.now()
|
|
56
|
+
issue.save(update_fields=["status", "sent_at"])
|
|
57
|
+
|
|
58
|
+
logger.info("Issue %r sent: %d ok, %d failed", issue.title, sent, failed)
|