plain 0.37.0__tar.gz → 0.39.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.
- {plain-0.37.0 → plain-0.39.0}/.gitignore +2 -0
- {plain-0.37.0 → plain-0.39.0}/PKG-INFO +1 -1
- {plain-0.37.0 → plain-0.39.0}/plain/assets/README.md +16 -0
- plain-0.39.0/plain/chores/__init__.py +3 -0
- plain-0.39.0/plain/chores/registry.py +79 -0
- plain-0.39.0/plain/cli/chores.py +91 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/core.py +2 -0
- plain-0.39.0/plain/cli/docs.py +253 -0
- {plain-0.37.0 → plain-0.39.0}/plain/csrf/README.md +2 -2
- plain-0.39.0/plain/forms/exceptions.py +13 -0
- {plain-0.37.0 → plain-0.39.0}/plain/forms/fields.py +21 -27
- {plain-0.37.0 → plain-0.39.0}/plain/forms/forms.py +32 -10
- {plain-0.37.0 → plain-0.39.0}/plain/logs/README.md +1 -1
- {plain-0.37.0 → plain-0.39.0}/plain/views/README.md +38 -7
- {plain-0.37.0 → plain-0.39.0}/plain/views/base.py +31 -39
- {plain-0.37.0 → plain-0.39.0}/plain/views/forms.py +3 -11
- {plain-0.37.0 → plain-0.39.0}/pyproject.toml +1 -1
- plain-0.37.0/plain/cli/docs.py +0 -211
- plain-0.37.0/plain/forms/exceptions.py +0 -11
- {plain-0.37.0 → plain-0.39.0}/LICENSE +0 -0
- {plain-0.37.0 → plain-0.39.0}/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/__main__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/assets/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/assets/compile.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/assets/finders.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/assets/fingerprints.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/assets/urls.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/assets/views.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/build.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/formatting.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/preflight.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/print.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/registry.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/scaffold.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/settings.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/shell.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/startup.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/urls.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/cli/utils.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/csrf/middleware.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/csrf/views.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/debug.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/forms/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/forms/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/forms/boundfield.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/http/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/http/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/http/cookie.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/http/multipartparser.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/http/request.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/http/response.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/base.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/locks.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/move.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/temp.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/uploadedfile.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/uploadhandler.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/files/utils.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/handlers/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/handlers/base.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/handlers/exception.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/handlers/wsgi.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/middleware/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/middleware/headers.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/middleware/https.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/internal/middleware/slash.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/json.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/logs/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/logs/configure.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/logs/loggers.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/logs/utils.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/packages/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/packages/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/packages/config.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/packages/registry.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/paginator.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/files.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/messages.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/registry.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/security.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/preflight/urls.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/runtime/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/runtime/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/runtime/global_settings.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/runtime/user_settings.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/signals/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/signals/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/signals/dispatch/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/signals/dispatch/dispatcher.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/signals/dispatch/license.txt +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/signing.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/core.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/jinja/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/jinja/environments.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/jinja/extensions.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/jinja/filters.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/templates/jinja/globals.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/test/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/test/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/test/client.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/test/encoding.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/test/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/converters.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/patterns.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/resolvers.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/routers.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/urls/utils.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/README.md +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/cache.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/connection.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/crypto.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/datastructures.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/dateparse.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/deconstruct.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/decorators.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/duration.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/encoding.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/functional.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/hashable.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/html.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/http.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/inspect.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/ipv6.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/itercompat.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/module_loading.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/regex_helper.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/safestring.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/text.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/timesince.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/timezone.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/utils/tree.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/validators.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/csrf.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/errors.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/objects.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/redirect.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/views/templates.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/plain/wsgi.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/.bolt/assets_collected/assets.json +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/.gitignore +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/app/.gitignore +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/app/settings.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/app/test/__init__.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/app/test/default_settings.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/app/urls.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/conftest.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/test_cli.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/test_runtime.py +0 -0
- {plain-0.37.0 → plain-0.39.0}/tests/test_wsgi.py +0 -0
@@ -44,6 +44,22 @@ By default, this [generates "fingerprinted" and compressed versions of the asset
|
|
44
44
|
|
45
45
|
The purpose of fingerprinting the assets is to allow the browser to cache them indefinitely. When the content of the file changes, the fingerprint will change, and the browser will use the newer file. This cuts down on the number of requests that your app has to handle related to assets.
|
46
46
|
|
47
|
+
## Using `AssetView` directly
|
48
|
+
|
49
|
+
In some situations you may want to use the `AssetView` at a custom URL, for example to serve a `favicon.ico`. You can do this quickly by using the `AssetView.as_view()` class method.
|
50
|
+
|
51
|
+
```python
|
52
|
+
from plain.assets.views import AssetView
|
53
|
+
from plain.urls import path, Router
|
54
|
+
|
55
|
+
|
56
|
+
class AppRouter(Router):
|
57
|
+
namespace = ""
|
58
|
+
urls = [
|
59
|
+
path("favicon.ico", AssetView.as_view(asset_path="favicon.ico")),
|
60
|
+
]
|
61
|
+
```
|
62
|
+
|
47
63
|
## FAQs
|
48
64
|
|
49
65
|
### How do you reference assets in Python code?
|
@@ -0,0 +1,79 @@
|
|
1
|
+
from importlib import import_module
|
2
|
+
from importlib.util import find_spec
|
3
|
+
|
4
|
+
from plain.packages import packages_registry
|
5
|
+
|
6
|
+
|
7
|
+
class Chore:
|
8
|
+
def __init__(self, *, group, func):
|
9
|
+
self.group = group
|
10
|
+
self.func = func
|
11
|
+
self.name = f"{group}.{func.__name__}"
|
12
|
+
self.description = func.__doc__.strip() if func.__doc__ else ""
|
13
|
+
|
14
|
+
def __str__(self):
|
15
|
+
return self.name
|
16
|
+
|
17
|
+
def run(self):
|
18
|
+
"""
|
19
|
+
Run the chore.
|
20
|
+
"""
|
21
|
+
return self.func()
|
22
|
+
|
23
|
+
|
24
|
+
class ChoresRegistry:
|
25
|
+
def __init__(self):
|
26
|
+
self._chores = {}
|
27
|
+
|
28
|
+
def register_chore(self, chore):
|
29
|
+
"""
|
30
|
+
Register a chore with the specified name.
|
31
|
+
"""
|
32
|
+
self._chores[chore.func] = chore
|
33
|
+
|
34
|
+
def import_modules(self):
|
35
|
+
"""
|
36
|
+
Import modules from installed packages and app to trigger registration.
|
37
|
+
"""
|
38
|
+
# Import from installed packages
|
39
|
+
for package_config in packages_registry.get_package_configs():
|
40
|
+
import_name = f"{package_config.name}.chores"
|
41
|
+
try:
|
42
|
+
import_module(import_name)
|
43
|
+
except ModuleNotFoundError:
|
44
|
+
pass
|
45
|
+
|
46
|
+
# Import from app
|
47
|
+
import_name = "app.chores"
|
48
|
+
if find_spec(import_name):
|
49
|
+
try:
|
50
|
+
import_module(import_name)
|
51
|
+
except ModuleNotFoundError:
|
52
|
+
pass
|
53
|
+
|
54
|
+
def get_chores(self):
|
55
|
+
"""
|
56
|
+
Get all registered chores.
|
57
|
+
"""
|
58
|
+
return list(self._chores.values())
|
59
|
+
|
60
|
+
|
61
|
+
chores_registry = ChoresRegistry()
|
62
|
+
|
63
|
+
|
64
|
+
def register_chore(group):
|
65
|
+
"""
|
66
|
+
Register a chore with a given group.
|
67
|
+
|
68
|
+
Usage:
|
69
|
+
@register_chore("clear_expired")
|
70
|
+
def clear_expired():
|
71
|
+
pass
|
72
|
+
"""
|
73
|
+
|
74
|
+
def wrapper(func):
|
75
|
+
chore = Chore(group=group, func=func)
|
76
|
+
chores_registry.register_chore(chore)
|
77
|
+
return func
|
78
|
+
|
79
|
+
return wrapper
|
@@ -0,0 +1,91 @@
|
|
1
|
+
import logging
|
2
|
+
import sys
|
3
|
+
|
4
|
+
import click
|
5
|
+
|
6
|
+
logger = logging.getLogger("plain.chores")
|
7
|
+
|
8
|
+
|
9
|
+
@click.group()
|
10
|
+
def chores():
|
11
|
+
"""Routine maintenance tasks"""
|
12
|
+
pass
|
13
|
+
|
14
|
+
|
15
|
+
@chores.command("list")
|
16
|
+
@click.option("--group", default=None, type=str, help="Group to run", multiple=True)
|
17
|
+
@click.option(
|
18
|
+
"--name", default=None, type=str, help="Name of the chore to run", multiple=True
|
19
|
+
)
|
20
|
+
def list_chores(group, name):
|
21
|
+
"""
|
22
|
+
List all registered chores.
|
23
|
+
"""
|
24
|
+
from plain.chores.registry import chores_registry
|
25
|
+
|
26
|
+
chores_registry.import_modules()
|
27
|
+
|
28
|
+
if group or name:
|
29
|
+
chores = [
|
30
|
+
chore
|
31
|
+
for chore in chores_registry.get_chores()
|
32
|
+
if (chore.group in group or not group) and (chore.name in name or not name)
|
33
|
+
]
|
34
|
+
else:
|
35
|
+
chores = chores_registry.get_chores()
|
36
|
+
|
37
|
+
for chore in chores:
|
38
|
+
click.secho(f"{chore}", bold=True, nl=False)
|
39
|
+
if chore.description:
|
40
|
+
click.echo(f": {chore.description}")
|
41
|
+
else:
|
42
|
+
click.echo("")
|
43
|
+
|
44
|
+
|
45
|
+
@chores.command("run")
|
46
|
+
@click.option("--group", default=None, type=str, help="Group to run", multiple=True)
|
47
|
+
@click.option(
|
48
|
+
"--name", default=None, type=str, help="Name of the chore to run", multiple=True
|
49
|
+
)
|
50
|
+
@click.option(
|
51
|
+
"--dry-run", is_flag=True, help="Show what would be done without executing"
|
52
|
+
)
|
53
|
+
def run_chores(group, name, dry_run):
|
54
|
+
"""
|
55
|
+
Run the specified chores.
|
56
|
+
"""
|
57
|
+
from plain.chores.registry import chores_registry
|
58
|
+
|
59
|
+
chores_registry.import_modules()
|
60
|
+
|
61
|
+
if group or name:
|
62
|
+
chores = [
|
63
|
+
chore
|
64
|
+
for chore in chores_registry.get_chores()
|
65
|
+
if (chore.group in group or not group) and (chore.name in name or not name)
|
66
|
+
]
|
67
|
+
else:
|
68
|
+
chores = chores_registry.get_chores()
|
69
|
+
|
70
|
+
chores_failed = []
|
71
|
+
|
72
|
+
for chore in chores:
|
73
|
+
click.echo(f"{chore.name}:", nl=False)
|
74
|
+
if dry_run:
|
75
|
+
click.echo(" (dry run)", fg="yellow")
|
76
|
+
else:
|
77
|
+
try:
|
78
|
+
result = chore.run()
|
79
|
+
except Exception:
|
80
|
+
click.secho(" Failed", fg="red")
|
81
|
+
chores_failed.append(chore)
|
82
|
+
logger.exception(f"Error running chore {chore.name}")
|
83
|
+
continue
|
84
|
+
|
85
|
+
if result is None:
|
86
|
+
click.secho(" Done", fg="green")
|
87
|
+
else:
|
88
|
+
click.secho(f" {result}", fg="green")
|
89
|
+
|
90
|
+
if chores_failed:
|
91
|
+
sys.exit(1)
|
@@ -7,6 +7,7 @@ import plain.runtime
|
|
7
7
|
from plain.exceptions import ImproperlyConfigured
|
8
8
|
|
9
9
|
from .build import build
|
10
|
+
from .chores import chores
|
10
11
|
from .docs import docs
|
11
12
|
from .formatting import PlainContext
|
12
13
|
from .preflight import preflight_checks
|
@@ -26,6 +27,7 @@ def plain_cli():
|
|
26
27
|
plain_cli.add_command(docs)
|
27
28
|
plain_cli.add_command(preflight_checks)
|
28
29
|
plain_cli.add_command(create)
|
30
|
+
plain_cli.add_command(chores)
|
29
31
|
plain_cli.add_command(build)
|
30
32
|
plain_cli.add_command(utils)
|
31
33
|
plain_cli.add_command(urls)
|
@@ -0,0 +1,253 @@
|
|
1
|
+
import ast
|
2
|
+
import importlib.util
|
3
|
+
import sys
|
4
|
+
from pathlib import Path
|
5
|
+
|
6
|
+
import click
|
7
|
+
|
8
|
+
from plain.packages import packages_registry
|
9
|
+
|
10
|
+
|
11
|
+
@click.command()
|
12
|
+
@click.option("--llm", "llm", is_flag=True)
|
13
|
+
@click.option("--open")
|
14
|
+
@click.argument("module", default="")
|
15
|
+
def docs(module, llm, open):
|
16
|
+
if not module and not llm:
|
17
|
+
click.secho("You must specify a module or use --llm", fg="red")
|
18
|
+
sys.exit(1)
|
19
|
+
|
20
|
+
if llm:
|
21
|
+
paths = [Path(__file__).parent.parent]
|
22
|
+
|
23
|
+
for package_config in packages_registry.get_package_configs():
|
24
|
+
if package_config.name.startswith("app."):
|
25
|
+
# Ignore app packages for now
|
26
|
+
continue
|
27
|
+
|
28
|
+
paths.append(Path(package_config.path))
|
29
|
+
|
30
|
+
source_docs = LLMDocs(paths)
|
31
|
+
source_docs.load()
|
32
|
+
source_docs.print()
|
33
|
+
|
34
|
+
click.secho(
|
35
|
+
"That's everything! Copy this into your AI tool of choice.",
|
36
|
+
err=True,
|
37
|
+
fg="green",
|
38
|
+
)
|
39
|
+
|
40
|
+
return
|
41
|
+
|
42
|
+
if module:
|
43
|
+
# Automatically prefix if we need to
|
44
|
+
if not module.startswith("plain"):
|
45
|
+
module = f"plain.{module}"
|
46
|
+
|
47
|
+
# Get the README.md file for the module
|
48
|
+
spec = importlib.util.find_spec(module)
|
49
|
+
if not spec:
|
50
|
+
click.secho(f"Module {module} not found", fg="red")
|
51
|
+
sys.exit(1)
|
52
|
+
|
53
|
+
module_path = Path(spec.origin).parent
|
54
|
+
readme_path = module_path / "README.md"
|
55
|
+
if not readme_path.exists():
|
56
|
+
click.secho(f"README.md not found for {module}", fg="red")
|
57
|
+
sys.exit(1)
|
58
|
+
|
59
|
+
if open:
|
60
|
+
click.launch(str(readme_path))
|
61
|
+
else:
|
62
|
+
|
63
|
+
def _iterate_markdown(content):
|
64
|
+
"""
|
65
|
+
Iterator that does basic markdown for a Click pager.
|
66
|
+
|
67
|
+
Headings are yellow and bright, code blocks are indented.
|
68
|
+
"""
|
69
|
+
|
70
|
+
in_code_block = False
|
71
|
+
for line in content.splitlines():
|
72
|
+
if line.startswith("```"):
|
73
|
+
in_code_block = not in_code_block
|
74
|
+
|
75
|
+
if in_code_block:
|
76
|
+
yield click.style(line, dim=True)
|
77
|
+
elif line.startswith("# "):
|
78
|
+
yield click.style(line, fg="yellow", bold=True)
|
79
|
+
elif line.startswith("## "):
|
80
|
+
yield click.style(line, fg="yellow", bold=True)
|
81
|
+
elif line.startswith("### "):
|
82
|
+
yield click.style(line, fg="yellow", bold=True)
|
83
|
+
elif line.startswith("#### "):
|
84
|
+
yield click.style(line, fg="yellow", bold=True)
|
85
|
+
elif line.startswith("##### "):
|
86
|
+
yield click.style(line, fg="yellow", bold=True)
|
87
|
+
elif line.startswith("###### "):
|
88
|
+
yield click.style(line, fg="yellow", bold=True)
|
89
|
+
elif line.startswith("**") and line.endswith("**"):
|
90
|
+
yield click.style(line, bold=True)
|
91
|
+
elif line.startswith("> "):
|
92
|
+
yield click.style(line, italic=True)
|
93
|
+
else:
|
94
|
+
yield line
|
95
|
+
|
96
|
+
yield "\n"
|
97
|
+
|
98
|
+
click.echo_via_pager(_iterate_markdown(readme_path.read_text()))
|
99
|
+
|
100
|
+
|
101
|
+
class LLMDocs:
|
102
|
+
preamble = (
|
103
|
+
"Below is all of the documentation and abbreviated source code for the Plain web framework. "
|
104
|
+
"Your job is to read and understand it, and then act as the Plain Framework Assistant and "
|
105
|
+
"help the developer accomplish whatever they want to do next."
|
106
|
+
"\n\n---\n\n"
|
107
|
+
)
|
108
|
+
|
109
|
+
def __init__(self, paths):
|
110
|
+
self.paths = paths
|
111
|
+
|
112
|
+
def load(self):
|
113
|
+
self.docs = set()
|
114
|
+
self.sources = set()
|
115
|
+
|
116
|
+
for path in self.paths:
|
117
|
+
if path.is_dir():
|
118
|
+
self.docs.update(path.glob("**/*.md"))
|
119
|
+
self.sources.update(path.glob("**/*.py"))
|
120
|
+
elif path.suffix == ".py":
|
121
|
+
self.sources.add(path)
|
122
|
+
elif path.suffix == ".md":
|
123
|
+
self.docs.add(path)
|
124
|
+
|
125
|
+
# Exclude "migrations" code from plain apps, except for plain/models/migrations
|
126
|
+
self.docs = {
|
127
|
+
doc
|
128
|
+
for doc in self.docs
|
129
|
+
if not (
|
130
|
+
"/migrations/" in str(doc)
|
131
|
+
and "/plain/models/migrations/" not in str(doc)
|
132
|
+
)
|
133
|
+
}
|
134
|
+
self.sources = {
|
135
|
+
source
|
136
|
+
for source in self.sources
|
137
|
+
if not (
|
138
|
+
"/migrations/" in str(source)
|
139
|
+
and "/plain/models/migrations/" not in str(source)
|
140
|
+
)
|
141
|
+
}
|
142
|
+
|
143
|
+
self.docs = sorted(self.docs)
|
144
|
+
self.sources = sorted(self.sources)
|
145
|
+
|
146
|
+
def display_path(self, path):
|
147
|
+
if "plain" in path.parts:
|
148
|
+
root_index = path.parts.index("plain")
|
149
|
+
elif "plainx" in path.parts:
|
150
|
+
root_index = path.parts.index("plainx")
|
151
|
+
else:
|
152
|
+
raise ValueError("Path does not contain 'plain' or 'plainx'")
|
153
|
+
|
154
|
+
plain_root = Path(*path.parts[: root_index + 1])
|
155
|
+
return path.relative_to(plain_root.parent)
|
156
|
+
|
157
|
+
def print(self, relative_to=None):
|
158
|
+
click.secho(self.preamble, fg="yellow")
|
159
|
+
|
160
|
+
for doc in self.docs:
|
161
|
+
if relative_to:
|
162
|
+
display_path = doc.relative_to(relative_to)
|
163
|
+
else:
|
164
|
+
display_path = self.display_path(doc)
|
165
|
+
click.secho(f"<Docs: {display_path}>", fg="yellow")
|
166
|
+
click.echo(doc.read_text())
|
167
|
+
click.secho(f"</Docs: {display_path}>", fg="yellow")
|
168
|
+
click.echo()
|
169
|
+
|
170
|
+
for source in self.sources:
|
171
|
+
if symbolicated := self.symbolicate(source):
|
172
|
+
if relative_to:
|
173
|
+
display_path = source.relative_to(relative_to)
|
174
|
+
else:
|
175
|
+
display_path = self.display_path(source)
|
176
|
+
click.secho(f"<Source: {display_path}>", fg="yellow")
|
177
|
+
click.echo(symbolicated)
|
178
|
+
click.secho(f"</Source: {display_path}>", fg="yellow")
|
179
|
+
click.echo()
|
180
|
+
|
181
|
+
@staticmethod
|
182
|
+
def symbolicate(file_path: Path):
|
183
|
+
if "internal" in str(file_path).split("/"):
|
184
|
+
return ""
|
185
|
+
|
186
|
+
source = file_path.read_text()
|
187
|
+
|
188
|
+
parsed = ast.parse(source)
|
189
|
+
|
190
|
+
def should_skip(node):
|
191
|
+
if isinstance(node, ast.ClassDef | ast.FunctionDef):
|
192
|
+
if any(
|
193
|
+
isinstance(d, ast.Name) and d.id == "internalcode"
|
194
|
+
for d in node.decorator_list
|
195
|
+
):
|
196
|
+
return True
|
197
|
+
if node.name.startswith("_"): # and not node.name.endswith("__"):
|
198
|
+
return True
|
199
|
+
elif isinstance(node, ast.Assign):
|
200
|
+
for target in node.targets:
|
201
|
+
if (
|
202
|
+
isinstance(target, ast.Name) and target.id.startswith("_")
|
203
|
+
# and not target.id.endswith("__")
|
204
|
+
):
|
205
|
+
return True
|
206
|
+
return False
|
207
|
+
|
208
|
+
def process_node(node, indent=0):
|
209
|
+
lines = []
|
210
|
+
prefix = " " * indent
|
211
|
+
|
212
|
+
if should_skip(node):
|
213
|
+
return []
|
214
|
+
|
215
|
+
if isinstance(node, ast.ClassDef):
|
216
|
+
decorators = [
|
217
|
+
f"{prefix}@{ast.unparse(d)}"
|
218
|
+
for d in node.decorator_list
|
219
|
+
if not (isinstance(d, ast.Name) and d.id == "internalcode")
|
220
|
+
]
|
221
|
+
lines.extend(decorators)
|
222
|
+
bases = [ast.unparse(base) for base in node.bases]
|
223
|
+
lines.append(f"{prefix}class {node.name}({', '.join(bases)})")
|
224
|
+
# if ast.get_docstring(node):
|
225
|
+
# lines.append(f'{prefix} """{ast.get_docstring(node)}"""')
|
226
|
+
for child in node.body:
|
227
|
+
child_lines = process_node(child, indent + 1)
|
228
|
+
if child_lines:
|
229
|
+
lines.extend(child_lines)
|
230
|
+
# if not has_body:
|
231
|
+
# lines.append(f"{prefix} pass")
|
232
|
+
|
233
|
+
elif isinstance(node, ast.FunctionDef):
|
234
|
+
decorators = [f"{prefix}@{ast.unparse(d)}" for d in node.decorator_list]
|
235
|
+
lines.extend(decorators)
|
236
|
+
args = ast.unparse(node.args)
|
237
|
+
lines.append(f"{prefix}def {node.name}({args})")
|
238
|
+
# if ast.get_docstring(node):
|
239
|
+
# lines.append(f'{prefix} """{ast.get_docstring(node)}"""')
|
240
|
+
# lines.append(f"{prefix} pass")
|
241
|
+
|
242
|
+
elif isinstance(node, ast.Assign):
|
243
|
+
for target in node.targets:
|
244
|
+
if isinstance(target, ast.Name):
|
245
|
+
lines.append(f"{prefix}{target.id} = {ast.unparse(node.value)}")
|
246
|
+
|
247
|
+
return lines
|
248
|
+
|
249
|
+
symbolicated_lines = []
|
250
|
+
for node in parsed.body:
|
251
|
+
symbolicated_lines.extend(process_node(node))
|
252
|
+
|
253
|
+
return "\n".join(symbolicated_lines)
|
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
**Cross-Site Request Forgery (CSRF) protection.**
|
4
4
|
|
5
|
-
Plain protects against [CSRF attacks](https://en.wikipedia.org/wiki/Cross-site_request_forgery) through a [middleware](middleware.py) that compares the generated `csrftoken` cookie with the CSRF token from the request (either `_csrftoken` in form data or the `
|
5
|
+
Plain protects against [CSRF attacks](https://en.wikipedia.org/wiki/Cross-site_request_forgery) through a [middleware](middleware.py) that compares the generated `csrftoken` cookie with the CSRF token from the request (either `_csrftoken` in form data or the `CSRF-Token` header).
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
9
|
The `CsrfViewMiddleware` is [automatically installed](../internal/handlers/base.py#BUILTIN_BEFORE_MIDDLEWARE), so you don't need to add it to your `settings.MIDDLEWARE`.
|
10
10
|
|
11
|
-
When you use HTML forms, you should include the CSRF token in the form data:
|
11
|
+
When you use HTML forms, you should include the CSRF token in the form data via a hidden input:
|
12
12
|
|
13
13
|
```html
|
14
14
|
<form method="post">
|
@@ -0,0 +1,13 @@
|
|
1
|
+
from plain.exceptions import ValidationError
|
2
|
+
|
3
|
+
|
4
|
+
class FormFieldMissingError(Exception):
|
5
|
+
def __init__(self, field_name):
|
6
|
+
self.field_name = field_name
|
7
|
+
self.message = f'The "{self.field_name}" field is missing from the form data.'
|
8
|
+
|
9
|
+
|
10
|
+
__all__ = [
|
11
|
+
"ValidationError",
|
12
|
+
"FormFieldMissingError",
|
13
|
+
]
|
@@ -67,23 +67,16 @@ class Field:
|
|
67
67
|
initial=None,
|
68
68
|
error_messages=None,
|
69
69
|
validators=(),
|
70
|
-
disabled=False,
|
71
70
|
):
|
72
71
|
# required -- Boolean that specifies whether the field is required.
|
73
72
|
# True by default.
|
74
|
-
# widget -- A Widget class, or instance of a Widget class, that should
|
75
|
-
# be used for this Field when displaying it. Each Field has a
|
76
|
-
# default Widget that it'll use if you don't specify this. In
|
77
|
-
# most cases, the default widget is TextInput.
|
78
73
|
# initial -- A value to use in this Field's initial display. This value
|
79
74
|
# is *not* used as a fallback if data isn't given.
|
80
75
|
# error_messages -- An optional dictionary to override the default
|
81
76
|
# messages that the field will raise.
|
82
77
|
# validators -- List of additional validators to use
|
83
|
-
|
84
|
-
|
85
|
-
self.required, self.initial = required, initial
|
86
|
-
self.disabled = disabled
|
78
|
+
self.required = required
|
79
|
+
self.initial = initial
|
87
80
|
|
88
81
|
messages = {}
|
89
82
|
for c in reversed(self.__class__.__mro__):
|
@@ -136,16 +129,10 @@ class Field:
|
|
136
129
|
For most fields, this will simply be data; FileFields need to handle it
|
137
130
|
a bit differently.
|
138
131
|
"""
|
139
|
-
if self.disabled:
|
140
|
-
return initial
|
141
132
|
return data
|
142
133
|
|
143
134
|
def has_changed(self, initial, data):
|
144
135
|
"""Return True if data differs from initial."""
|
145
|
-
# Always return False if the field is disabled since self.bound_data
|
146
|
-
# always uses the initial value in this case.
|
147
|
-
if self.disabled:
|
148
|
-
return False
|
149
136
|
try:
|
150
137
|
data = self.to_python(data)
|
151
138
|
if hasattr(self, "_coerce"):
|
@@ -174,12 +161,17 @@ class Field:
|
|
174
161
|
return result
|
175
162
|
|
176
163
|
def value_from_form_data(self, data, files, html_name):
|
164
|
+
# By default, all fields are expected to be present in HTML form data.
|
177
165
|
try:
|
178
166
|
return data[html_name]
|
179
167
|
except KeyError as e:
|
180
|
-
raise FormFieldMissingError(
|
181
|
-
|
182
|
-
|
168
|
+
raise FormFieldMissingError(html_name) from e
|
169
|
+
|
170
|
+
def value_from_json_data(self, data, files, html_name):
|
171
|
+
if self.required and html_name not in data:
|
172
|
+
raise FormFieldMissingError(html_name)
|
173
|
+
|
174
|
+
return data.get(html_name, None)
|
183
175
|
|
184
176
|
|
185
177
|
class CharField(Field):
|
@@ -592,11 +584,14 @@ class FileField(Field):
|
|
592
584
|
return initial
|
593
585
|
|
594
586
|
def has_changed(self, initial, data):
|
595
|
-
return
|
587
|
+
return data is not None
|
596
588
|
|
597
589
|
def value_from_form_data(self, data, files, html_name):
|
598
590
|
return files.get(html_name)
|
599
591
|
|
592
|
+
def value_from_json_data(self, data, files, html_name):
|
593
|
+
return files.get(html_name)
|
594
|
+
|
600
595
|
|
601
596
|
class ImageField(FileField):
|
602
597
|
default_validators = [validators.validate_image_file_extension]
|
@@ -706,8 +701,6 @@ class BooleanField(Field):
|
|
706
701
|
raise ValidationError(self.error_messages["required"], code="required")
|
707
702
|
|
708
703
|
def has_changed(self, initial, data):
|
709
|
-
if self.disabled:
|
710
|
-
return False
|
711
704
|
# Sometimes data or initial may be a string equivalent of a boolean
|
712
705
|
# so we should run it through to_python first to get a boolean value
|
713
706
|
return self.to_python(initial) != self.to_python(data)
|
@@ -729,6 +722,13 @@ class BooleanField(Field):
|
|
729
722
|
"on": True,
|
730
723
|
}.get(value)
|
731
724
|
|
725
|
+
def value_from_json_data(self, data, files, html_name):
|
726
|
+
# Boolean fields must be present in the JSON data
|
727
|
+
try:
|
728
|
+
return data[html_name]
|
729
|
+
except KeyError as e:
|
730
|
+
raise FormFieldMissingError(html_name) from e
|
731
|
+
|
732
732
|
|
733
733
|
class NullBooleanField(BooleanField):
|
734
734
|
"""
|
@@ -883,8 +883,6 @@ class MultipleChoiceField(ChoiceField):
|
|
883
883
|
)
|
884
884
|
|
885
885
|
def has_changed(self, initial, data):
|
886
|
-
if self.disabled:
|
887
|
-
return False
|
888
886
|
if initial is None:
|
889
887
|
initial = []
|
890
888
|
if data is None:
|
@@ -944,8 +942,6 @@ class JSONField(CharField):
|
|
944
942
|
super().__init__(**kwargs)
|
945
943
|
|
946
944
|
def to_python(self, value):
|
947
|
-
if self.disabled:
|
948
|
-
return value
|
949
945
|
if value in self.empty_values:
|
950
946
|
return None
|
951
947
|
elif isinstance(value, list | dict | int | float | JSONString):
|
@@ -964,8 +960,6 @@ class JSONField(CharField):
|
|
964
960
|
return converted
|
965
961
|
|
966
962
|
def bound_data(self, data, initial):
|
967
|
-
if self.disabled:
|
968
|
-
return initial
|
969
963
|
if data is None:
|
970
964
|
return None
|
971
965
|
try:
|