plain 0.37.0__tar.gz → 0.38.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.38.0}/.gitignore +2 -0
- {plain-0.37.0 → plain-0.38.0}/PKG-INFO +1 -1
- {plain-0.37.0 → plain-0.38.0}/plain/assets/README.md +16 -0
- plain-0.38.0/plain/cli/docs.py +253 -0
- {plain-0.37.0 → plain-0.38.0}/plain/csrf/README.md +2 -2
- plain-0.38.0/plain/forms/exceptions.py +13 -0
- {plain-0.37.0 → plain-0.38.0}/plain/forms/fields.py +21 -27
- {plain-0.37.0 → plain-0.38.0}/plain/forms/forms.py +32 -10
- {plain-0.37.0 → plain-0.38.0}/plain/logs/README.md +1 -1
- {plain-0.37.0 → plain-0.38.0}/plain/views/README.md +38 -7
- {plain-0.37.0 → plain-0.38.0}/plain/views/base.py +40 -39
- {plain-0.37.0 → plain-0.38.0}/plain/views/forms.py +3 -11
- {plain-0.37.0 → plain-0.38.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.38.0}/LICENSE +0 -0
- {plain-0.37.0 → plain-0.38.0}/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/__main__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/assets/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/assets/compile.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/assets/finders.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/assets/fingerprints.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/assets/urls.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/assets/views.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/build.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/core.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/formatting.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/preflight.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/print.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/registry.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/scaffold.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/settings.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/shell.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/startup.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/urls.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/cli/utils.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/csrf/middleware.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/csrf/views.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/debug.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/forms/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/forms/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/forms/boundfield.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/http/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/http/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/http/cookie.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/http/multipartparser.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/http/request.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/http/response.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/base.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/locks.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/move.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/temp.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/uploadedfile.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/uploadhandler.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/files/utils.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/handlers/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/handlers/base.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/handlers/exception.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/handlers/wsgi.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/middleware/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/middleware/headers.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/middleware/https.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/internal/middleware/slash.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/json.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/logs/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/logs/configure.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/logs/loggers.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/logs/utils.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/packages/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/packages/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/packages/config.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/packages/registry.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/paginator.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/files.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/messages.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/registry.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/security.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/preflight/urls.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/runtime/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/runtime/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/runtime/global_settings.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/runtime/user_settings.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/signals/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/signals/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/signals/dispatch/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/signals/dispatch/dispatcher.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/signals/dispatch/license.txt +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/signing.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/core.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/jinja/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/jinja/environments.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/jinja/extensions.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/jinja/filters.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/templates/jinja/globals.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/test/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/test/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/test/client.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/test/encoding.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/test/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/converters.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/patterns.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/resolvers.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/routers.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/urls/utils.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/README.md +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/cache.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/connection.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/crypto.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/datastructures.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/dateparse.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/deconstruct.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/decorators.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/duration.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/encoding.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/functional.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/hashable.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/html.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/http.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/inspect.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/ipv6.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/itercompat.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/module_loading.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/regex_helper.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/safestring.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/text.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/timesince.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/timezone.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/utils/tree.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/validators.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/csrf.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/errors.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/exceptions.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/objects.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/redirect.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/views/templates.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/plain/wsgi.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/.bolt/assets_collected/assets.json +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/.gitignore +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/app/.gitignore +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/app/settings.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/app/test/__init__.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/app/test/default_settings.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/app/urls.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/conftest.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/test_cli.py +0 -0
- {plain-0.37.0 → plain-0.38.0}/tests/test_runtime.py +0 -0
- {plain-0.37.0 → plain-0.38.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,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:
|
@@ -3,6 +3,7 @@ Form classes
|
|
3
3
|
"""
|
4
4
|
|
5
5
|
import copy
|
6
|
+
import json
|
6
7
|
|
7
8
|
from plain.exceptions import NON_FIELD_ERRORS
|
8
9
|
from plain.utils.datastructures import MultiValueDict
|
@@ -57,15 +58,36 @@ class BaseForm:
|
|
57
58
|
|
58
59
|
def __init__(
|
59
60
|
self,
|
60
|
-
|
61
|
-
|
61
|
+
*,
|
62
|
+
request,
|
62
63
|
auto_id="id_%s",
|
63
64
|
prefix=None,
|
64
65
|
initial=None,
|
65
66
|
):
|
66
|
-
|
67
|
-
|
68
|
-
|
67
|
+
if request.method in ("POST", "PUT", "PATCH"):
|
68
|
+
if request.headers.get("Content-Type") == "application/json":
|
69
|
+
self.data = json.loads(request.body)
|
70
|
+
self.is_json_request = True
|
71
|
+
elif request.headers.get("Content-Type") in [
|
72
|
+
"application/x-www-form-urlencoded",
|
73
|
+
"multipart/form-data",
|
74
|
+
]:
|
75
|
+
self.data = request.POST
|
76
|
+
self.is_json_request = False
|
77
|
+
else:
|
78
|
+
raise ValueError(
|
79
|
+
"Unsupported Content-Type. Supported types are "
|
80
|
+
"'application/json', 'application/x-www-form-urlencoded', "
|
81
|
+
"and 'multipart/form-data'."
|
82
|
+
)
|
83
|
+
else:
|
84
|
+
self.data = MultiValueDict()
|
85
|
+
self.is_json_request = False
|
86
|
+
|
87
|
+
self.files = request.FILES
|
88
|
+
|
89
|
+
self.is_bound = bool(self.data or self.files)
|
90
|
+
|
69
91
|
self._auto_id = auto_id
|
70
92
|
if prefix is not None:
|
71
93
|
self.prefix = prefix
|
@@ -224,16 +246,16 @@ class BaseForm:
|
|
224
246
|
# Allow custom parsing from form data/files at the form level
|
225
247
|
return getattr(self, f"parse_{html_name}")()
|
226
248
|
|
227
|
-
|
249
|
+
if self.is_json_request:
|
250
|
+
return field.value_from_json_data(self.data, self.files, html_name)
|
251
|
+
else:
|
252
|
+
return field.value_from_form_data(self.data, self.files, html_name)
|
228
253
|
|
229
254
|
def _clean_fields(self):
|
230
255
|
for name, bf in self._bound_items():
|
231
256
|
field = bf.field
|
232
257
|
|
233
|
-
|
234
|
-
value = bf.initial
|
235
|
-
else:
|
236
|
-
value = self._field_data_value(bf.field, bf.html_name)
|
258
|
+
value = self._field_data_value(bf.field, bf.html_name)
|
237
259
|
|
238
260
|
try:
|
239
261
|
if isinstance(field, FileField):
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
**Logging configuration and utilities.**
|
4
4
|
|
5
|
-
In Python, configuring logging can be surprisingly complex. For most use cases, Plain provides a [default configuration](configure.py) that "just works".
|
5
|
+
In Python, configuring logging can be surprisingly complex. For most use cases, Plain provides a [default configuration](./configure.py) that "just works".
|
6
6
|
|
7
7
|
By default, both the `plain` and `app` loggers are set to the `INFO` level. You can quickly change this by using the `PLAIN_LOG_LEVEL` and `APP_LOG_LEVEL` environment variables.
|
8
8
|
|
@@ -12,7 +12,7 @@ from plain.views import View
|
|
12
12
|
|
13
13
|
class ExampleView(View):
|
14
14
|
def get(self):
|
15
|
-
return "Hello, world
|
15
|
+
return "<html><body>Hello, world!</body></html>"
|
16
16
|
```
|
17
17
|
|
18
18
|
## HTTP methods -> class methods
|
@@ -51,18 +51,23 @@ but you can override these too.
|
|
51
51
|
|
52
52
|
## Return types
|
53
53
|
|
54
|
-
For simple
|
54
|
+
For simple JSON responses, HTML, or status code responses,
|
55
55
|
you don't need to instantiate a `Response` object.
|
56
56
|
|
57
57
|
```python
|
58
|
-
class
|
58
|
+
class JsonView(View):
|
59
59
|
def get(self):
|
60
|
-
return "Hello, world!"
|
60
|
+
return {"message": "Hello, world!"}
|
61
61
|
|
62
62
|
|
63
|
-
class
|
63
|
+
class HtmlView(View):
|
64
64
|
def get(self):
|
65
|
-
return
|
65
|
+
return "<html><body>Hello, world!</body></html>"
|
66
|
+
|
67
|
+
|
68
|
+
class StatusCodeView(View):
|
69
|
+
def get(self):
|
70
|
+
return 204 # No content
|
66
71
|
```
|
67
72
|
|
68
73
|
## Template views
|
@@ -84,6 +89,19 @@ class ExampleView(TemplateView):
|
|
84
89
|
|
85
90
|
The `TemplateView` is also the base class for _most_ of the other built-in view classes.
|
86
91
|
|
92
|
+
Template views that don't need any custom context can use `TemplateView.as_view()` direcly in the URL route.
|
93
|
+
|
94
|
+
```python
|
95
|
+
from plain.views import TemplateView
|
96
|
+
from plain.urls import path, Router
|
97
|
+
|
98
|
+
|
99
|
+
class AppRouter(Router):
|
100
|
+
routes = [
|
101
|
+
path("/example/", TemplateView.as_view(template_name="example.html")),
|
102
|
+
]
|
103
|
+
```
|
104
|
+
|
87
105
|
## Form views
|
88
106
|
|
89
107
|
Standard [forms](../forms) can be rendered and processed by a `FormView`.
|
@@ -255,7 +273,20 @@ class ExampleRedirectView(RedirectView):
|
|
255
273
|
permanent = True
|
256
274
|
```
|
257
275
|
|
258
|
-
|
276
|
+
Redirect views can also be used in the URL router.
|
277
|
+
|
278
|
+
```python
|
279
|
+
from plain.views import RedirectView
|
280
|
+
from plain.urls import path, Router
|
281
|
+
|
282
|
+
|
283
|
+
class AppRouter(Router):
|
284
|
+
routes = [
|
285
|
+
path("/old-location/", RedirectView.as_view(url="/new-location/", permanent=True)),
|
286
|
+
]
|
287
|
+
```
|
288
|
+
|
289
|
+
## CSRF exempt views
|
259
290
|
|
260
291
|
```python
|
261
292
|
from plain.views import View
|