django-gemini 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_gemini-0.1.0/LICENSE +21 -0
- django_gemini-0.1.0/PKG-INFO +236 -0
- django_gemini-0.1.0/README.md +201 -0
- django_gemini-0.1.0/pyproject.toml +69 -0
- django_gemini-0.1.0/pyproject.toml.orig +63 -0
- django_gemini-0.1.0/src/django_gemini/__init__.py +35 -0
- django_gemini-0.1.0/src/django_gemini/admin.py +16 -0
- django_gemini-0.1.0/src/django_gemini/apps.py +10 -0
- django_gemini-0.1.0/src/django_gemini/asgi.py +140 -0
- django_gemini-0.1.0/src/django_gemini/auth.py +143 -0
- django_gemini-0.1.0/src/django_gemini/checks.py +63 -0
- django_gemini-0.1.0/src/django_gemini/conf.py +24 -0
- django_gemini-0.1.0/src/django_gemini/convert.py +329 -0
- django_gemini-0.1.0/src/django_gemini/decorators.py +61 -0
- django_gemini-0.1.0/src/django_gemini/gemtext.py +56 -0
- django_gemini-0.1.0/src/django_gemini/html.py +159 -0
- django_gemini-0.1.0/src/django_gemini/management/__init__.py +0 -0
- django_gemini-0.1.0/src/django_gemini/management/commands/__init__.py +0 -0
- django_gemini-0.1.0/src/django_gemini/management/commands/rungemini.py +134 -0
- django_gemini-0.1.0/src/django_gemini/middleware.py +121 -0
- django_gemini-0.1.0/src/django_gemini/migrations/0001_initial.py +59 -0
- django_gemini-0.1.0/src/django_gemini/migrations/__init__.py +0 -0
- django_gemini-0.1.0/src/django_gemini/models.py +42 -0
- django_gemini-0.1.0/src/django_gemini/py.typed +0 -0
- django_gemini-0.1.0/src/django_gemini/request.py +61 -0
- django_gemini-0.1.0/src/django_gemini/responses.py +84 -0
- django_gemini-0.1.0/src/django_gemini/status.py +91 -0
- django_gemini-0.1.0/src/django_gemini/template.py +41 -0
- django_gemini-0.1.0/src/django_gemini/templates/django_gemini/base.html +23 -0
- django_gemini-0.1.0/src/django_gemini/templates/django_gemini/certificate.html +4 -0
- django_gemini-0.1.0/src/django_gemini/templates/django_gemini/error.html +2 -0
- django_gemini-0.1.0/src/django_gemini/templates/django_gemini/input.html +6 -0
- django_gemini-0.1.0/src/django_gemini/templates/django_gemini/page.html +2 -0
- django_gemini-0.1.0/src/django_gemini/templatetags/__init__.py +0 -0
- django_gemini-0.1.0/src/django_gemini/templatetags/gemtext.py +38 -0
- django_gemini-0.1.0/src/django_gemini/urls.py +14 -0
- django_gemini-0.1.0/src/django_gemini/views.py +175 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Humberto Alves
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-gemini
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Serve an existing Django site over the Gemini protocol, next to HTTP
|
|
5
|
+
Keywords: gemini,django,asgi,gemtext
|
|
6
|
+
Author: Humberto Alves
|
|
7
|
+
Author-email: Humberto Alves <hjalves@live.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Environment :: Web Environment
|
|
12
|
+
Classifier: Framework :: Django
|
|
13
|
+
Classifier: Framework :: Django :: 4.2
|
|
14
|
+
Classifier: Framework :: Django :: 5.2
|
|
15
|
+
Classifier: Framework :: Django :: 6.0
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Topic :: Internet
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Dist: asgi-gemini>=0.1.0
|
|
28
|
+
Requires-Dist: django>=4.2
|
|
29
|
+
Requires-Python: >=3.10
|
|
30
|
+
Project-URL: Homepage, https://github.com/hjalves/django-gemini
|
|
31
|
+
Project-URL: Repository, https://github.com/hjalves/django-gemini
|
|
32
|
+
Project-URL: Issues, https://github.com/hjalves/django-gemini/issues
|
|
33
|
+
Project-URL: Changelog, https://github.com/hjalves/django-gemini/blob/main/CHANGELOG.md
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# django-gemini
|
|
37
|
+
|
|
38
|
+
Serve an existing Django site over the [Gemini protocol](https://geminiprotocol.net/),
|
|
39
|
+
next to HTTP, from the same project. It runs on
|
|
40
|
+
[asgi-gemini](https://github.com/hjalves/asgi-gemini).
|
|
41
|
+
|
|
42
|
+
- **Existing HTML views work over Gemini**: their responses are converted to
|
|
43
|
+
gemtext (headings, paragraphs, lists, quotes, preformatted text, tables and
|
|
44
|
+
links). Parts that make no sense there, like the admin, can be hidden.
|
|
45
|
+
- **Views written for Gemini work in browsers**: gemtext becomes HTML pages
|
|
46
|
+
in your site's look, input prompts become forms, and certificate requests a
|
|
47
|
+
page pointing to the gemini:// URL.
|
|
48
|
+
- **Client certificates are logins**: a certificate linked to a user makes
|
|
49
|
+
`request.user` that user over Gemini. Django's `@login_required` sends
|
|
50
|
+
Gemini clients to a login that links the certificate with their username
|
|
51
|
+
and password, then back to the page.
|
|
52
|
+
- Gemini statuses for Django responses (404 → 51, redirects → 30/31, …),
|
|
53
|
+
`GeminiResponse` for explicit ones, a gemtext template engine, and a
|
|
54
|
+
`rungemini` development server.
|
|
55
|
+
|
|
56
|
+
Django 4.2 or later, Python 3.10 or later.
|
|
57
|
+
|
|
58
|
+
## Setup
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
pip install django-gemini
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
In `settings.py`:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
INSTALLED_APPS = [
|
|
68
|
+
...,
|
|
69
|
+
"django_gemini",
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
MIDDLEWARE = [
|
|
73
|
+
"django_gemini.middleware.GeminiMiddleware", # before Django's middleware
|
|
74
|
+
...,
|
|
75
|
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
76
|
+
"django_gemini.auth.CertificateAuthenticationMiddleware", # after auth
|
|
77
|
+
...,
|
|
78
|
+
"django_gemini.middleware.GemtextToHtmlMiddleware", # last
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
TEMPLATES = [
|
|
82
|
+
{...}, # your HTML engine, unchanged
|
|
83
|
+
{
|
|
84
|
+
"NAME": "gemtext",
|
|
85
|
+
"BACKEND": "django_gemini.template.GemtextTemplates",
|
|
86
|
+
"APP_DIRS": True,
|
|
87
|
+
},
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
GEMINI_HIDDEN_PATHS = ["/admin/"]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
In `urls.py`, the Gemini login (optional):
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
urlpatterns = [
|
|
97
|
+
...,
|
|
98
|
+
path("account/", include("django_gemini.urls")),
|
|
99
|
+
]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
In `asgi.py`:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from django_gemini.asgi import get_asgi_application
|
|
106
|
+
|
|
107
|
+
application = get_asgi_application()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Then `python manage.py migrate` and, while developing:
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
python manage.py rungemini # gemini://localhost/ on 127.0.0.1:1965
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
In production, run the same application with asgi-gemini, with your
|
|
117
|
+
certificate:
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
asgi-gemini mysite.asgi:application --host 0.0.0.0 --hostname example.org \
|
|
121
|
+
--tls-certfile cert.pem --tls-keyfile key.pem
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
HTTP keeps working as before, from your WSGI app or from this ASGI
|
|
125
|
+
application with any ASGI server. Gemini requests must be for a host in
|
|
126
|
+
`ALLOWED_HOSTS`. The [example](example/) project is an ordinary site with
|
|
127
|
+
these lines added.
|
|
128
|
+
|
|
129
|
+
## Writing views for Gemini
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from django_gemini import GeminiResponse, get_input, input_response, render_gemtext
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def hello(request):
|
|
136
|
+
name = get_input(request) # the answer to a prompt, or None
|
|
137
|
+
if name is None:
|
|
138
|
+
return input_response("What's your name?") # 10; sensitive=True for 11
|
|
139
|
+
return render_gemtext(request, "hello.gmi", {"name": name})
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
# Hello, {{ name|oneline:50 }}!
|
|
144
|
+
=> / Home
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
- `render_gemtext` renders `.gmi` templates with the gemtext engine:
|
|
148
|
+
Django's template language without HTML escaping. Gemtext has no escaping
|
|
149
|
+
at all, so keep user text on one line with the built-in filters:
|
|
150
|
+
`|oneline` (whitespace collapsed; for after a line marker like `#` or `=>`),
|
|
151
|
+
`|textline` (also can't start a link, heading, list item or quote),
|
|
152
|
+
`|gemurl` (for link URLs) and `|pre` (inside a preformatted block).
|
|
153
|
+
`django_gemini.gemtext` has the same as functions, for building gemtext in
|
|
154
|
+
Python (`gemtext_response(lines)` returns it).
|
|
155
|
+
- `GeminiResponse(status, meta, content)` sends any Gemini status;
|
|
156
|
+
`certificate_response()` is a 60.
|
|
157
|
+
- `is_gemini(request)`, `get_client_cert(request)` (DER bytes) and
|
|
158
|
+
`get_fingerprint(request)` (SHA-256, hex) tell views about the request.
|
|
159
|
+
Over Gemini, `request.scheme` is `"gemini"`, so `build_absolute_uri()`
|
|
160
|
+
gives gemini:// URLs, and `request.is_secure()` is True.
|
|
161
|
+
- Decorators in `django_gemini.decorators`: `gemini_only`, `http_only`
|
|
162
|
+
(404 over the other protocol) and `certificate_required`.
|
|
163
|
+
|
|
164
|
+
Over HTTP, `GemtextToHtmlMiddleware` turns gemtext into HTML with the
|
|
165
|
+
templates in `django_gemini/` (`base.html`, `page.html`, `input.html`,
|
|
166
|
+
`certificate.html`, `error.html`). Override them to use your site's layout,
|
|
167
|
+
for example `templates/django_gemini/page.html`:
|
|
168
|
+
|
|
169
|
+
```django
|
|
170
|
+
{% extends "base.html" %}
|
|
171
|
+
{% block content %}{{ content }}{% endblock %}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
A client sending `Accept: text/gemini` gets the gemtext itself.
|
|
175
|
+
|
|
176
|
+
## A separate Gemini site
|
|
177
|
+
|
|
178
|
+
To serve Gemini clients different URLs from the web site, give them their own
|
|
179
|
+
URLconf:
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
GEMINI_URLCONF = "mysite.gemini_urls"
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Logins
|
|
186
|
+
|
|
187
|
+
`ClientCertificate` links a certificate's fingerprint to a user. Gemini has no
|
|
188
|
+
cookies, so it plays the part of the session: with
|
|
189
|
+
`CertificateAuthenticationMiddleware`, requests with a linked certificate have
|
|
190
|
+
`request.user` set to its (active) user, and `login_required`,
|
|
191
|
+
`permission_required` and `user.has_perm` work as usual.
|
|
192
|
+
|
|
193
|
+
Certificates get linked:
|
|
194
|
+
|
|
195
|
+
- over Gemini, at `account/login/` (from `django_gemini.urls`): a username
|
|
196
|
+
prompt, a password prompt, and the certificate is linked, so the password
|
|
197
|
+
is needed once per certificate. Wrong passwords are limited to
|
|
198
|
+
`GEMINI_LOGIN_ATTEMPTS` per certificate per `GEMINI_LOGIN_WINDOW` seconds.
|
|
199
|
+
`account/` shows the certificate and user, and `account/logout/` unlinks it;
|
|
200
|
+
- in the admin, by pasting the fingerprint (`account/` shows it).
|
|
201
|
+
|
|
202
|
+
Redirects to `settings.LOGIN_URL`, which is what `@login_required` does, are
|
|
203
|
+
sent to `GEMINI_LOGIN_URL` instead (60 if the client has no certificate), and
|
|
204
|
+
the client comes back to the page after logging in.
|
|
205
|
+
|
|
206
|
+
Gemini has no request body: the password travels in the URL's query, like
|
|
207
|
+
all input, so it ends up in access logs, asgi-gemini's included (it prints
|
|
208
|
+
each request's URL to stdout, so to the journal under systemd). Set
|
|
209
|
+
`GEMINI_PASSWORD_LOGIN = False` to link certificates only in the admin.
|
|
210
|
+
|
|
211
|
+
## Settings
|
|
212
|
+
|
|
213
|
+
| Setting | Default | |
|
|
214
|
+
|---|---|---|
|
|
215
|
+
| `GEMINI_URLCONF` | `None` | URLconf for Gemini requests (`None`: `ROOT_URLCONF`) |
|
|
216
|
+
| `GEMINI_HIDDEN_PATHS` | `[]` | Path prefixes that are 51 (not found) over Gemini |
|
|
217
|
+
| `GEMINI_CONVERT_HTML` | `True` | Convert `text/html` responses to gemtext for Gemini |
|
|
218
|
+
| `GEMINI_LOGIN_URL` | `"gemini:login"` | Where Gemini clients log in (URL name or path) |
|
|
219
|
+
| `GEMINI_PASSWORD_LOGIN` | `True` | Allow linking a certificate by username and password |
|
|
220
|
+
| `GEMINI_LOGIN_ATTEMPTS` | `5` | Wrong passwords allowed per certificate... |
|
|
221
|
+
| `GEMINI_LOGIN_WINDOW` | `900` | ...in this many seconds |
|
|
222
|
+
|
|
223
|
+
## How it works
|
|
224
|
+
|
|
225
|
+
asgi-gemini hands the application `gemini` scopes. `GeminiBridge` turns each
|
|
226
|
+
one into an HTTP GET scope for Django, with the original in
|
|
227
|
+
`request.scope["gemini"]`, and turns Django's response back into a Gemini
|
|
228
|
+
status and body: an explicit status from `GeminiResponse` (sent in
|
|
229
|
+
`X-Gemini-*` headers), otherwise one mapped from the HTTP status. HTTP scopes
|
|
230
|
+
are passed through unchanged.
|
|
231
|
+
|
|
232
|
+
## Tests
|
|
233
|
+
|
|
234
|
+
```sh
|
|
235
|
+
uv run pytest
|
|
236
|
+
```
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# django-gemini
|
|
2
|
+
|
|
3
|
+
Serve an existing Django site over the [Gemini protocol](https://geminiprotocol.net/),
|
|
4
|
+
next to HTTP, from the same project. It runs on
|
|
5
|
+
[asgi-gemini](https://github.com/hjalves/asgi-gemini).
|
|
6
|
+
|
|
7
|
+
- **Existing HTML views work over Gemini**: their responses are converted to
|
|
8
|
+
gemtext (headings, paragraphs, lists, quotes, preformatted text, tables and
|
|
9
|
+
links). Parts that make no sense there, like the admin, can be hidden.
|
|
10
|
+
- **Views written for Gemini work in browsers**: gemtext becomes HTML pages
|
|
11
|
+
in your site's look, input prompts become forms, and certificate requests a
|
|
12
|
+
page pointing to the gemini:// URL.
|
|
13
|
+
- **Client certificates are logins**: a certificate linked to a user makes
|
|
14
|
+
`request.user` that user over Gemini. Django's `@login_required` sends
|
|
15
|
+
Gemini clients to a login that links the certificate with their username
|
|
16
|
+
and password, then back to the page.
|
|
17
|
+
- Gemini statuses for Django responses (404 → 51, redirects → 30/31, …),
|
|
18
|
+
`GeminiResponse` for explicit ones, a gemtext template engine, and a
|
|
19
|
+
`rungemini` development server.
|
|
20
|
+
|
|
21
|
+
Django 4.2 or later, Python 3.10 or later.
|
|
22
|
+
|
|
23
|
+
## Setup
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
pip install django-gemini
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
In `settings.py`:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
INSTALLED_APPS = [
|
|
33
|
+
...,
|
|
34
|
+
"django_gemini",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
MIDDLEWARE = [
|
|
38
|
+
"django_gemini.middleware.GeminiMiddleware", # before Django's middleware
|
|
39
|
+
...,
|
|
40
|
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
41
|
+
"django_gemini.auth.CertificateAuthenticationMiddleware", # after auth
|
|
42
|
+
...,
|
|
43
|
+
"django_gemini.middleware.GemtextToHtmlMiddleware", # last
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
TEMPLATES = [
|
|
47
|
+
{...}, # your HTML engine, unchanged
|
|
48
|
+
{
|
|
49
|
+
"NAME": "gemtext",
|
|
50
|
+
"BACKEND": "django_gemini.template.GemtextTemplates",
|
|
51
|
+
"APP_DIRS": True,
|
|
52
|
+
},
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
GEMINI_HIDDEN_PATHS = ["/admin/"]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
In `urls.py`, the Gemini login (optional):
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
urlpatterns = [
|
|
62
|
+
...,
|
|
63
|
+
path("account/", include("django_gemini.urls")),
|
|
64
|
+
]
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
In `asgi.py`:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from django_gemini.asgi import get_asgi_application
|
|
71
|
+
|
|
72
|
+
application = get_asgi_application()
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Then `python manage.py migrate` and, while developing:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
python manage.py rungemini # gemini://localhost/ on 127.0.0.1:1965
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
In production, run the same application with asgi-gemini, with your
|
|
82
|
+
certificate:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
asgi-gemini mysite.asgi:application --host 0.0.0.0 --hostname example.org \
|
|
86
|
+
--tls-certfile cert.pem --tls-keyfile key.pem
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
HTTP keeps working as before, from your WSGI app or from this ASGI
|
|
90
|
+
application with any ASGI server. Gemini requests must be for a host in
|
|
91
|
+
`ALLOWED_HOSTS`. The [example](example/) project is an ordinary site with
|
|
92
|
+
these lines added.
|
|
93
|
+
|
|
94
|
+
## Writing views for Gemini
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from django_gemini import GeminiResponse, get_input, input_response, render_gemtext
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def hello(request):
|
|
101
|
+
name = get_input(request) # the answer to a prompt, or None
|
|
102
|
+
if name is None:
|
|
103
|
+
return input_response("What's your name?") # 10; sensitive=True for 11
|
|
104
|
+
return render_gemtext(request, "hello.gmi", {"name": name})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
# Hello, {{ name|oneline:50 }}!
|
|
109
|
+
=> / Home
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- `render_gemtext` renders `.gmi` templates with the gemtext engine:
|
|
113
|
+
Django's template language without HTML escaping. Gemtext has no escaping
|
|
114
|
+
at all, so keep user text on one line with the built-in filters:
|
|
115
|
+
`|oneline` (whitespace collapsed; for after a line marker like `#` or `=>`),
|
|
116
|
+
`|textline` (also can't start a link, heading, list item or quote),
|
|
117
|
+
`|gemurl` (for link URLs) and `|pre` (inside a preformatted block).
|
|
118
|
+
`django_gemini.gemtext` has the same as functions, for building gemtext in
|
|
119
|
+
Python (`gemtext_response(lines)` returns it).
|
|
120
|
+
- `GeminiResponse(status, meta, content)` sends any Gemini status;
|
|
121
|
+
`certificate_response()` is a 60.
|
|
122
|
+
- `is_gemini(request)`, `get_client_cert(request)` (DER bytes) and
|
|
123
|
+
`get_fingerprint(request)` (SHA-256, hex) tell views about the request.
|
|
124
|
+
Over Gemini, `request.scheme` is `"gemini"`, so `build_absolute_uri()`
|
|
125
|
+
gives gemini:// URLs, and `request.is_secure()` is True.
|
|
126
|
+
- Decorators in `django_gemini.decorators`: `gemini_only`, `http_only`
|
|
127
|
+
(404 over the other protocol) and `certificate_required`.
|
|
128
|
+
|
|
129
|
+
Over HTTP, `GemtextToHtmlMiddleware` turns gemtext into HTML with the
|
|
130
|
+
templates in `django_gemini/` (`base.html`, `page.html`, `input.html`,
|
|
131
|
+
`certificate.html`, `error.html`). Override them to use your site's layout,
|
|
132
|
+
for example `templates/django_gemini/page.html`:
|
|
133
|
+
|
|
134
|
+
```django
|
|
135
|
+
{% extends "base.html" %}
|
|
136
|
+
{% block content %}{{ content }}{% endblock %}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
A client sending `Accept: text/gemini` gets the gemtext itself.
|
|
140
|
+
|
|
141
|
+
## A separate Gemini site
|
|
142
|
+
|
|
143
|
+
To serve Gemini clients different URLs from the web site, give them their own
|
|
144
|
+
URLconf:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
GEMINI_URLCONF = "mysite.gemini_urls"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Logins
|
|
151
|
+
|
|
152
|
+
`ClientCertificate` links a certificate's fingerprint to a user. Gemini has no
|
|
153
|
+
cookies, so it plays the part of the session: with
|
|
154
|
+
`CertificateAuthenticationMiddleware`, requests with a linked certificate have
|
|
155
|
+
`request.user` set to its (active) user, and `login_required`,
|
|
156
|
+
`permission_required` and `user.has_perm` work as usual.
|
|
157
|
+
|
|
158
|
+
Certificates get linked:
|
|
159
|
+
|
|
160
|
+
- over Gemini, at `account/login/` (from `django_gemini.urls`): a username
|
|
161
|
+
prompt, a password prompt, and the certificate is linked, so the password
|
|
162
|
+
is needed once per certificate. Wrong passwords are limited to
|
|
163
|
+
`GEMINI_LOGIN_ATTEMPTS` per certificate per `GEMINI_LOGIN_WINDOW` seconds.
|
|
164
|
+
`account/` shows the certificate and user, and `account/logout/` unlinks it;
|
|
165
|
+
- in the admin, by pasting the fingerprint (`account/` shows it).
|
|
166
|
+
|
|
167
|
+
Redirects to `settings.LOGIN_URL`, which is what `@login_required` does, are
|
|
168
|
+
sent to `GEMINI_LOGIN_URL` instead (60 if the client has no certificate), and
|
|
169
|
+
the client comes back to the page after logging in.
|
|
170
|
+
|
|
171
|
+
Gemini has no request body: the password travels in the URL's query, like
|
|
172
|
+
all input, so it ends up in access logs, asgi-gemini's included (it prints
|
|
173
|
+
each request's URL to stdout, so to the journal under systemd). Set
|
|
174
|
+
`GEMINI_PASSWORD_LOGIN = False` to link certificates only in the admin.
|
|
175
|
+
|
|
176
|
+
## Settings
|
|
177
|
+
|
|
178
|
+
| Setting | Default | |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| `GEMINI_URLCONF` | `None` | URLconf for Gemini requests (`None`: `ROOT_URLCONF`) |
|
|
181
|
+
| `GEMINI_HIDDEN_PATHS` | `[]` | Path prefixes that are 51 (not found) over Gemini |
|
|
182
|
+
| `GEMINI_CONVERT_HTML` | `True` | Convert `text/html` responses to gemtext for Gemini |
|
|
183
|
+
| `GEMINI_LOGIN_URL` | `"gemini:login"` | Where Gemini clients log in (URL name or path) |
|
|
184
|
+
| `GEMINI_PASSWORD_LOGIN` | `True` | Allow linking a certificate by username and password |
|
|
185
|
+
| `GEMINI_LOGIN_ATTEMPTS` | `5` | Wrong passwords allowed per certificate... |
|
|
186
|
+
| `GEMINI_LOGIN_WINDOW` | `900` | ...in this many seconds |
|
|
187
|
+
|
|
188
|
+
## How it works
|
|
189
|
+
|
|
190
|
+
asgi-gemini hands the application `gemini` scopes. `GeminiBridge` turns each
|
|
191
|
+
one into an HTTP GET scope for Django, with the original in
|
|
192
|
+
`request.scope["gemini"]`, and turns Django's response back into a Gemini
|
|
193
|
+
status and body: an explicit status from `GeminiResponse` (sent in
|
|
194
|
+
`X-Gemini-*` headers), otherwise one mapped from the HTTP status. HTTP scopes
|
|
195
|
+
are passed through unchanged.
|
|
196
|
+
|
|
197
|
+
## Tests
|
|
198
|
+
|
|
199
|
+
```sh
|
|
200
|
+
uv run pytest
|
|
201
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-gemini"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Serve an existing Django site over the Gemini protocol, next to HTTP"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"asgi-gemini>=0.1.0",
|
|
11
|
+
"django>=4.2",
|
|
12
|
+
]
|
|
13
|
+
keywords = [
|
|
14
|
+
"gemini",
|
|
15
|
+
"django",
|
|
16
|
+
"asgi",
|
|
17
|
+
"gemtext",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 3 - Alpha",
|
|
21
|
+
"Environment :: Web Environment",
|
|
22
|
+
"Framework :: Django",
|
|
23
|
+
"Framework :: Django :: 4.2",
|
|
24
|
+
"Framework :: Django :: 5.2",
|
|
25
|
+
"Framework :: Django :: 6.0",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
30
|
+
"Programming Language :: Python :: 3.10",
|
|
31
|
+
"Programming Language :: Python :: 3.11",
|
|
32
|
+
"Programming Language :: Python :: 3.12",
|
|
33
|
+
"Programming Language :: Python :: 3.13",
|
|
34
|
+
"Programming Language :: Python :: 3.14",
|
|
35
|
+
"Topic :: Internet",
|
|
36
|
+
"Typing :: Typed",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[[project.authors]]
|
|
40
|
+
name = "Humberto Alves"
|
|
41
|
+
email = "hjalves@live.com"
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Homepage = "https://github.com/hjalves/django-gemini"
|
|
45
|
+
Repository = "https://github.com/hjalves/django-gemini"
|
|
46
|
+
Issues = "https://github.com/hjalves/django-gemini/issues"
|
|
47
|
+
Changelog = "https://github.com/hjalves/django-gemini/blob/main/CHANGELOG.md"
|
|
48
|
+
|
|
49
|
+
[build-system]
|
|
50
|
+
requires = ["uv_build>=0.13.10,<0.14.0"]
|
|
51
|
+
build-backend = "uv_build"
|
|
52
|
+
|
|
53
|
+
[dependency-groups]
|
|
54
|
+
dev = [
|
|
55
|
+
"cryptography>=46",
|
|
56
|
+
"pytest>=9.2.0",
|
|
57
|
+
"pytest-asyncio>=1.5.0",
|
|
58
|
+
"pytest-django>=4.14.0",
|
|
59
|
+
"ruff>=0.17.6",
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
[tool.pytest.ini_options]
|
|
63
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
64
|
+
pythonpath = ["."]
|
|
65
|
+
asyncio_mode = "auto"
|
|
66
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
67
|
+
|
|
68
|
+
[tool.ruff]
|
|
69
|
+
line-length = 88
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-gemini"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Serve an existing Django site over the Gemini protocol, next to HTTP"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Humberto Alves", email = "hjalves@live.com" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"asgi-gemini>=0.1.0",
|
|
14
|
+
"django>=4.2",
|
|
15
|
+
]
|
|
16
|
+
keywords = ["gemini", "django", "asgi", "gemtext"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Environment :: Web Environment",
|
|
20
|
+
"Framework :: Django",
|
|
21
|
+
"Framework :: Django :: 4.2",
|
|
22
|
+
"Framework :: Django :: 5.2",
|
|
23
|
+
"Framework :: Django :: 6.0",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
28
|
+
"Programming Language :: Python :: 3.10",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Programming Language :: Python :: 3.13",
|
|
32
|
+
"Programming Language :: Python :: 3.14",
|
|
33
|
+
"Topic :: Internet",
|
|
34
|
+
"Typing :: Typed",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/hjalves/django-gemini"
|
|
39
|
+
Repository = "https://github.com/hjalves/django-gemini"
|
|
40
|
+
Issues = "https://github.com/hjalves/django-gemini/issues"
|
|
41
|
+
Changelog = "https://github.com/hjalves/django-gemini/blob/main/CHANGELOG.md"
|
|
42
|
+
|
|
43
|
+
[build-system]
|
|
44
|
+
requires = ["uv_build>=0.13.10,<0.14.0"]
|
|
45
|
+
build-backend = "uv_build"
|
|
46
|
+
|
|
47
|
+
[dependency-groups]
|
|
48
|
+
dev = [
|
|
49
|
+
"cryptography>=46",
|
|
50
|
+
"pytest>=9.2.0",
|
|
51
|
+
"pytest-asyncio>=1.5.0",
|
|
52
|
+
"pytest-django>=4.14.0",
|
|
53
|
+
"ruff>=0.17.6",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[tool.pytest.ini_options]
|
|
57
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
58
|
+
pythonpath = ["."]
|
|
59
|
+
asyncio_mode = "auto"
|
|
60
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
line-length = 88
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Serve an existing Django site over the Gemini protocol, next to HTTP."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from .request import (
|
|
6
|
+
cert_fingerprint,
|
|
7
|
+
get_client_cert,
|
|
8
|
+
get_fingerprint,
|
|
9
|
+
get_input,
|
|
10
|
+
is_gemini,
|
|
11
|
+
)
|
|
12
|
+
from .responses import (
|
|
13
|
+
GeminiResponse,
|
|
14
|
+
certificate_response,
|
|
15
|
+
gemtext_response,
|
|
16
|
+
input_response,
|
|
17
|
+
render_gemtext,
|
|
18
|
+
)
|
|
19
|
+
from .status import GEMTEXT
|
|
20
|
+
|
|
21
|
+
__version__ = version("django-gemini")
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"GEMTEXT",
|
|
25
|
+
"GeminiResponse",
|
|
26
|
+
"cert_fingerprint",
|
|
27
|
+
"certificate_response",
|
|
28
|
+
"gemtext_response",
|
|
29
|
+
"get_client_cert",
|
|
30
|
+
"get_fingerprint",
|
|
31
|
+
"get_input",
|
|
32
|
+
"input_response",
|
|
33
|
+
"is_gemini",
|
|
34
|
+
"render_gemtext",
|
|
35
|
+
]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
|
|
3
|
+
from .models import ClientCertificate
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@admin.register(ClientCertificate)
|
|
7
|
+
class ClientCertificateAdmin(admin.ModelAdmin):
|
|
8
|
+
list_display = ["user", "label", "short_fingerprint", "created", "last_used"]
|
|
9
|
+
list_select_related = ["user"]
|
|
10
|
+
search_fields = ["fingerprint", "label", "user__username"]
|
|
11
|
+
readonly_fields = ["created", "last_used"]
|
|
12
|
+
raw_id_fields = ["user"]
|
|
13
|
+
|
|
14
|
+
@admin.display(description="fingerprint", ordering="fingerprint")
|
|
15
|
+
def short_fingerprint(self, obj):
|
|
16
|
+
return obj.fingerprint[:16] + "…"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class DjangoGeminiConfig(AppConfig):
|
|
5
|
+
name = "django_gemini"
|
|
6
|
+
verbose_name = "Gemini"
|
|
7
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
8
|
+
|
|
9
|
+
def ready(self):
|
|
10
|
+
from . import checks as _checks # noqa: F401 (registers the checks)
|