django-admin-remote 0.1.0__tar.gz → 0.2.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_admin_remote-0.1.0 → django_admin_remote-0.2.0}/CHANGELOG.md +11 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/PKG-INFO +15 -3
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/README.md +14 -2
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/pyproject.toml +1 -1
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/__init__.py +1 -1
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/client.py +12 -8
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/form.py +42 -2
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/test_client.py +15 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/test_form.py +43 -1
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/.gitignore +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/LICENSE +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/dates.py +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/exceptions.py +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/parse.py +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/py.typed +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/conftest.py +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/fixtures/change_form.html +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/fixtures/changelist.html +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/test_dates.py +0 -0
- {django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/tests/test_parse.py +0 -0
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project are listed here.
|
|
4
4
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/).
|
|
5
5
|
|
|
6
|
+
## 0.2.0 - 2026-11-26
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- File uploads. `AdminForm.file_fields`, `has_file()` and `set_file(name, filename, content, content_type)` attach a file to a file input, and `add_row` takes `(filename, content[, content_type])` for an inline file input. A form with an attached file is submitted as `multipart/form-data`.
|
|
11
|
+
- `AdminClient.post()` and `request()` take `files=` (passed to `requests`); a redirect after the POST is followed with a plain GET.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- A file input with nothing attached is still left out of the POST (keeps the stored file), as before.
|
|
16
|
+
|
|
6
17
|
## 0.1.0 - 2026-11-11
|
|
7
18
|
|
|
8
19
|
First release.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: django-admin-remote
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Read and edit a remote Django admin site over HTTP by parsing its stock HTML. Nothing to install on the server.
|
|
5
5
|
Project-URL: Repository, https://github.com/krushndayshmookh/django-admin-remote
|
|
6
6
|
Project-URL: Issues, https://github.com/krushndayshmookh/django-admin-remote/issues
|
|
@@ -100,13 +100,14 @@ When the session is missing or has expired the client raises `NotLoggedIn`. It n
|
|
|
100
100
|
| `AdminClient(base_url, cookies=None, storage_state=None, delay=0.4, user_agent=None, timeout=60, tz=None, login_path="/admin/login/", session=None)` | One client per site. Waits `delay` seconds between requests. |
|
|
101
101
|
| `client.login(username, password)` | Log in through the admin login form. |
|
|
102
102
|
| `client.get(path, params=None)` | GET a page, returns the HTML. |
|
|
103
|
-
| `client.post(path, data)` | POST form data, returns `(final_url, html)`. Never retried. |
|
|
103
|
+
| `client.post(path, data, files=None)` | POST form data, returns `(final_url, html)`. With `files` the body is `multipart/form-data`. Never retried. |
|
|
104
104
|
| `client.changelist(path, limit=None, params=None, **filters)` | Iterator over the ids of a change list, all pages. |
|
|
105
105
|
| `client.read(path)` | `parse_change_form` of a page: `{"fields", "inlines", "logged_out"}`. |
|
|
106
106
|
| `client.form(path)` | An `AdminForm` for an add or change page. |
|
|
107
107
|
| `client.parse_datetime(text)` | `parse_admin_datetime` with the client's `tz`. |
|
|
108
108
|
| `AdminForm.has / get / set / set_if_present` | Read and change the values that will be posted. |
|
|
109
109
|
| `AdminForm.add_row(prefix, values) / delete_row(prefix, index)` | Inline formset rows. |
|
|
110
|
+
| `AdminForm.has_file(name) / set_file(name, filename, content, content_type)` | Attach a file to a file input; the form is then posted as multipart. |
|
|
110
111
|
| `AdminForm.total(prefix) / initial(prefix) / row_text / row_link_id` | Inspect inline formsets. |
|
|
111
112
|
| `AdminForm.readonly(name) / object_id()` | Displayed read-only text, primary key from the URL. |
|
|
112
113
|
| `AdminForm.submit(button="_continue")` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
@@ -130,6 +131,17 @@ form.submit()
|
|
|
130
131
|
|
|
131
132
|
`add_row` copies the formset's `__prefix__` template row, so hidden fields keep their defaults, and it updates `TOTAL_FORMS`. It refuses to go past `MAX_NUM_FORMS`.
|
|
132
133
|
|
|
134
|
+
### File uploads
|
|
135
|
+
|
|
136
|
+
File inputs are listed in `form.file_fields` and are not posted unless you attach a file, which keeps the stored file, like a browser. Attach one with `set_file`, or pass `(filename, content)` or `(filename, content, content_type)` for a file input in `add_row`. Text content is sent UTF-8 encoded. Any attached file makes the submit a `multipart/form-data` POST.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
form = admin.form("/admin/library/book/3/change/")
|
|
140
|
+
form.set_file("cover", "cover.png", png_bytes, "image/png")
|
|
141
|
+
form.add_row("attachment_set", {"title": "Errata", "document": ("errata.txt", "Page 4: read 12 for 21")})
|
|
142
|
+
form.submit()
|
|
143
|
+
```
|
|
144
|
+
|
|
133
145
|
### Dates
|
|
134
146
|
|
|
135
147
|
The admin shows datetimes in the server's time zone without naming it. Pass the zone to get an unambiguous value:
|
|
@@ -166,7 +178,7 @@ What cannot be recovered: anything the admin formats before display. Dates come
|
|
|
166
178
|
- **Be polite to production sites.** Every call is a real page render on someone's server. Keep the default delay or raise it, use `limit`, and do not run it in parallel against a site you do not operate. Only use it where you are allowed to automate.
|
|
167
179
|
- **POSTs are never retried.** If a POST fails or times out, the client raises and leaves it to you: the save may or may not have happened, and a blind repeat could create a duplicate. A GET that gets a 5xx answer is retried a few times.
|
|
168
180
|
- **A submit sends what the page showed.** Like a browser, the form posts every field back. If someone else edits the same object between your load and your submit, their change is overwritten.
|
|
169
|
-
-
|
|
181
|
+
- A file input with nothing attached is left out of the POST, which keeps the existing file. Clearing a file (the admin's "Clear" checkbox) is an ordinary checkbox field.
|
|
170
182
|
- No JavaScript. Widgets that load their choices by AJAX (`autocomplete_fields`) only list the options present in the HTML; set those fields to the primary key directly. `raw_id_fields` are plain text inputs and work.
|
|
171
183
|
- Change list parsing returns ids, the count and the pages, not the column values. Read each object for its data.
|
|
172
184
|
- The total count and the dates assume an English-language admin with default formats.
|
|
@@ -67,13 +67,14 @@ When the session is missing or has expired the client raises `NotLoggedIn`. It n
|
|
|
67
67
|
| `AdminClient(base_url, cookies=None, storage_state=None, delay=0.4, user_agent=None, timeout=60, tz=None, login_path="/admin/login/", session=None)` | One client per site. Waits `delay` seconds between requests. |
|
|
68
68
|
| `client.login(username, password)` | Log in through the admin login form. |
|
|
69
69
|
| `client.get(path, params=None)` | GET a page, returns the HTML. |
|
|
70
|
-
| `client.post(path, data)` | POST form data, returns `(final_url, html)`. Never retried. |
|
|
70
|
+
| `client.post(path, data, files=None)` | POST form data, returns `(final_url, html)`. With `files` the body is `multipart/form-data`. Never retried. |
|
|
71
71
|
| `client.changelist(path, limit=None, params=None, **filters)` | Iterator over the ids of a change list, all pages. |
|
|
72
72
|
| `client.read(path)` | `parse_change_form` of a page: `{"fields", "inlines", "logged_out"}`. |
|
|
73
73
|
| `client.form(path)` | An `AdminForm` for an add or change page. |
|
|
74
74
|
| `client.parse_datetime(text)` | `parse_admin_datetime` with the client's `tz`. |
|
|
75
75
|
| `AdminForm.has / get / set / set_if_present` | Read and change the values that will be posted. |
|
|
76
76
|
| `AdminForm.add_row(prefix, values) / delete_row(prefix, index)` | Inline formset rows. |
|
|
77
|
+
| `AdminForm.has_file(name) / set_file(name, filename, content, content_type)` | Attach a file to a file input; the form is then posted as multipart. |
|
|
77
78
|
| `AdminForm.total(prefix) / initial(prefix) / row_text / row_link_id` | Inspect inline formsets. |
|
|
78
79
|
| `AdminForm.readonly(name) / object_id()` | Displayed read-only text, primary key from the URL. |
|
|
79
80
|
| `AdminForm.submit(button="_continue")` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
@@ -97,6 +98,17 @@ form.submit()
|
|
|
97
98
|
|
|
98
99
|
`add_row` copies the formset's `__prefix__` template row, so hidden fields keep their defaults, and it updates `TOTAL_FORMS`. It refuses to go past `MAX_NUM_FORMS`.
|
|
99
100
|
|
|
101
|
+
### File uploads
|
|
102
|
+
|
|
103
|
+
File inputs are listed in `form.file_fields` and are not posted unless you attach a file, which keeps the stored file, like a browser. Attach one with `set_file`, or pass `(filename, content)` or `(filename, content, content_type)` for a file input in `add_row`. Text content is sent UTF-8 encoded. Any attached file makes the submit a `multipart/form-data` POST.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
form = admin.form("/admin/library/book/3/change/")
|
|
107
|
+
form.set_file("cover", "cover.png", png_bytes, "image/png")
|
|
108
|
+
form.add_row("attachment_set", {"title": "Errata", "document": ("errata.txt", "Page 4: read 12 for 21")})
|
|
109
|
+
form.submit()
|
|
110
|
+
```
|
|
111
|
+
|
|
100
112
|
### Dates
|
|
101
113
|
|
|
102
114
|
The admin shows datetimes in the server's time zone without naming it. Pass the zone to get an unambiguous value:
|
|
@@ -133,7 +145,7 @@ What cannot be recovered: anything the admin formats before display. Dates come
|
|
|
133
145
|
- **Be polite to production sites.** Every call is a real page render on someone's server. Keep the default delay or raise it, use `limit`, and do not run it in parallel against a site you do not operate. Only use it where you are allowed to automate.
|
|
134
146
|
- **POSTs are never retried.** If a POST fails or times out, the client raises and leaves it to you: the save may or may not have happened, and a blind repeat could create a duplicate. A GET that gets a 5xx answer is retried a few times.
|
|
135
147
|
- **A submit sends what the page showed.** Like a browser, the form posts every field back. If someone else edits the same object between your load and your submit, their change is overwritten.
|
|
136
|
-
-
|
|
148
|
+
- A file input with nothing attached is left out of the POST, which keeps the existing file. Clearing a file (the admin's "Clear" checkbox) is an ordinary checkbox field.
|
|
137
149
|
- No JavaScript. Widgets that load their choices by AJAX (`autocomplete_fields`) only list the options present in the HTML; set those fields to the primary key directly. `raw_id_fields` are plain text inputs and work.
|
|
138
150
|
- Change list parsing returns ids, the count and the pages, not the column values. Read each object for its data.
|
|
139
151
|
- The total count and the dates assume an English-language admin with default formats.
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "django-admin-remote"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Read and edit a remote Django admin site over HTTP by parsing its stock HTML. Nothing to install on the server."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -126,21 +126,22 @@ class AdminClient:
|
|
|
126
126
|
return path.rstrip("/") == self.login_path.rstrip("/") or "login" in path.strip("/").split("/")
|
|
127
127
|
|
|
128
128
|
# ---------- HTTP ----------
|
|
129
|
-
def _send(self, method: str, url: str, params: Any, data: Any, referer: str
|
|
129
|
+
def _send(self, method: str, url: str, params: Any, data: Any, referer: str,
|
|
130
|
+
files: Any = None) -> requests.Response:
|
|
130
131
|
if self._last is not None:
|
|
131
132
|
wait = self.delay - (time.monotonic() - self._last)
|
|
132
133
|
if wait > 0:
|
|
133
134
|
time.sleep(wait)
|
|
134
135
|
headers = {"Referer": referer} if method == "POST" else {}
|
|
135
136
|
try:
|
|
136
|
-
return self.http.request(method, url, params=params, data=data,
|
|
137
|
-
timeout=self.timeout, allow_redirects=False)
|
|
137
|
+
return self.http.request(method, url, params=params, data=data, files=files or None,
|
|
138
|
+
headers=headers, timeout=self.timeout, allow_redirects=False)
|
|
138
139
|
finally:
|
|
139
140
|
self._last = time.monotonic()
|
|
140
141
|
self.requests_made += 1
|
|
141
142
|
|
|
142
143
|
def request(self, method: str, path: str, params: Optional[Mapping[str, Any]] = None,
|
|
143
|
-
data: Optional[FormData] = None) -> Tuple[str, str]:
|
|
144
|
+
data: Optional[FormData] = None, files: Optional[Mapping[str, Any]] = None) -> Tuple[str, str]:
|
|
144
145
|
"""Send one request and follow its redirects. Returns ``(final_url, body)``.
|
|
145
146
|
|
|
146
147
|
Raises NotLoggedIn if the site redirects to a login page or to another host,
|
|
@@ -151,7 +152,7 @@ class AdminClient:
|
|
|
151
152
|
referer = url
|
|
152
153
|
redirects = failures = 0
|
|
153
154
|
while True:
|
|
154
|
-
r = self._send(method, url, params, data, referer)
|
|
155
|
+
r = self._send(method, url, params, data, referer, files)
|
|
155
156
|
if r.status_code in _REDIRECTS:
|
|
156
157
|
target = urljoin(r.url or url, r.headers.get("Location", ""))
|
|
157
158
|
if method == "POST" and r.status_code in (307, 308):
|
|
@@ -162,7 +163,7 @@ class AdminClient:
|
|
|
162
163
|
redirects += 1
|
|
163
164
|
if redirects > self.max_redirects:
|
|
164
165
|
raise AdminHTTPError(f"too many redirects from {url}", r.status_code, url)
|
|
165
|
-
url, method, params, data = target, "GET", None, None
|
|
166
|
+
url, method, params, data, files = target, "GET", None, None, None
|
|
166
167
|
continue
|
|
167
168
|
if r.status_code >= 500 and method == "GET" and failures < self.get_retries:
|
|
168
169
|
failures += 1
|
|
@@ -177,15 +178,18 @@ class AdminClient:
|
|
|
177
178
|
"""GET a page and return its HTML."""
|
|
178
179
|
return self.request("GET", path, params=params)[1]
|
|
179
180
|
|
|
180
|
-
def post(self, path: str, data: FormData) -> Tuple[str, str]:
|
|
181
|
+
def post(self, path: str, data: FormData, files: Optional[Mapping[str, Any]] = None) -> Tuple[str, str]:
|
|
181
182
|
"""POST form data. Returns ``(final_url, body)`` of the page the site lands on.
|
|
182
183
|
|
|
183
184
|
``data`` must contain ``csrfmiddlewaretoken`` (AdminForm takes care of that). The
|
|
184
185
|
Referer header Django's CSRF check wants on HTTPS is added. A POST is sent once
|
|
185
186
|
and never retried: a repeated POST could create a second object.
|
|
187
|
+
|
|
188
|
+
``files`` maps file field names to ``(filename, content, content_type)``; with it the
|
|
189
|
+
body is sent as ``multipart/form-data`` (what a browser does when a file is chosen).
|
|
186
190
|
"""
|
|
187
191
|
self.posts_made += 1
|
|
188
|
-
return self.request("POST", path, data=data)
|
|
192
|
+
return self.request("POST", path, data=data, files=files)
|
|
189
193
|
|
|
190
194
|
# ---------- login ----------
|
|
191
195
|
def login(self, username: str, password: str) -> None:
|
|
@@ -7,7 +7,7 @@ Save in the browser.
|
|
|
7
7
|
from __future__ import annotations
|
|
8
8
|
|
|
9
9
|
import re
|
|
10
|
-
from typing import Any, Dict, List, Optional, Tuple
|
|
10
|
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
11
11
|
|
|
12
12
|
from bs4 import Tag
|
|
13
13
|
|
|
@@ -55,6 +55,13 @@ def _default_values(el: Tag) -> Optional[List[str]]:
|
|
|
55
55
|
return [el.get("value", "")]
|
|
56
56
|
|
|
57
57
|
|
|
58
|
+
def _is_file(el: Tag) -> bool:
|
|
59
|
+
return el.name == "input" and (el.get("type") or "").lower() == "file" and not el.has_attr("disabled")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
FileValue = Union[Tuple[str, Union[str, bytes]], Tuple[str, Union[str, bytes], str]]
|
|
63
|
+
|
|
64
|
+
|
|
58
65
|
class AdminForm:
|
|
59
66
|
"""One admin add or change form.
|
|
60
67
|
|
|
@@ -73,6 +80,8 @@ class AdminForm:
|
|
|
73
80
|
buttons: names of the submit buttons on the form.
|
|
74
81
|
messages: texts of the admin's message list (for example "... was changed successfully.").
|
|
75
82
|
errors: validation errors shown on the page.
|
|
83
|
+
file_fields: names of the file inputs the form would post (see :meth:`set_file`).
|
|
84
|
+
files: ``{name: (filename, content, content_type)}`` attached so far.
|
|
76
85
|
"""
|
|
77
86
|
|
|
78
87
|
def __init__(self, client: Any, path: str, text: Optional[str] = None) -> None:
|
|
@@ -109,6 +118,8 @@ class AdminForm:
|
|
|
109
118
|
self.templates: Dict[str, Dict[str, Tag]] = {}
|
|
110
119
|
self.buttons: List[str] = []
|
|
111
120
|
self._button_values: Dict[str, str] = {}
|
|
121
|
+
self.file_fields: set = set()
|
|
122
|
+
self.files: Dict[str, Tuple[str, bytes, str]] = {}
|
|
112
123
|
self.messages = [li.get_text(" ", strip=True) for li in doc.select("ul.messagelist li")]
|
|
113
124
|
self.errors = [e.get_text(" ", strip=True) for e in doc.select("p.errornote, ul.errorlist li")]
|
|
114
125
|
if form is None:
|
|
@@ -129,6 +140,9 @@ class AdminForm:
|
|
|
129
140
|
if m:
|
|
130
141
|
self.templates.setdefault(m.group(1), {})[m.group(2)] = el
|
|
131
142
|
continue
|
|
143
|
+
if _is_file(el):
|
|
144
|
+
self.file_fields.add(name)
|
|
145
|
+
continue
|
|
132
146
|
vals = _default_values(el)
|
|
133
147
|
if vals is None:
|
|
134
148
|
continue
|
|
@@ -240,6 +254,22 @@ class AdminForm:
|
|
|
240
254
|
else:
|
|
241
255
|
self.fields[name] = [self._resolve(name, value)]
|
|
242
256
|
|
|
257
|
+
def has_file(self, name: str) -> bool:
|
|
258
|
+
"""True if the form has a file input with this name that a browser would post."""
|
|
259
|
+
return name in self.file_fields
|
|
260
|
+
|
|
261
|
+
def set_file(self, name: str, filename: str, content: Union[str, bytes],
|
|
262
|
+
content_type: str = "application/octet-stream") -> None:
|
|
263
|
+
"""Attach a file to a file input, as choosing a file in the browser would.
|
|
264
|
+
|
|
265
|
+
``content`` is bytes, or text that is sent UTF-8 encoded. The form is then posted as
|
|
266
|
+
``multipart/form-data``. Raises FormError if the page has no such file input.
|
|
267
|
+
"""
|
|
268
|
+
if name not in self.file_fields:
|
|
269
|
+
raise FormError(f"file field {name!r} is not on {self.path}")
|
|
270
|
+
data = content.encode("utf-8") if isinstance(content, str) else bytes(content)
|
|
271
|
+
self.files[name] = (filename, data, content_type)
|
|
272
|
+
|
|
243
273
|
def set_if_present(self, name: str, value: Any) -> bool:
|
|
244
274
|
"""Set the field if it is editable here and value is not None. Returns whether it was set."""
|
|
245
275
|
if name in self.fields and value is not None:
|
|
@@ -253,6 +283,8 @@ class AdminForm:
|
|
|
253
283
|
The row is built from the formset's ``__prefix__`` empty-form template, so hidden
|
|
254
284
|
fields such as the parent key keep their defaults, and TOTAL_FORMS is increased.
|
|
255
285
|
``values`` maps the short field names of the inline to values (see :meth:`set`).
|
|
286
|
+
A file input of the inline takes ``(filename, content)`` or
|
|
287
|
+
``(filename, content, content_type)`` (see :meth:`set_file`).
|
|
256
288
|
"""
|
|
257
289
|
tmpl = self.templates.get(prefix)
|
|
258
290
|
if tmpl is None or not self.has_inline(prefix):
|
|
@@ -266,6 +298,11 @@ class AdminForm:
|
|
|
266
298
|
raise FormError(f"inline {prefix!r} has no field(s) {sorted(unknown)}. Has: {sorted(tmpl)}")
|
|
267
299
|
for field, el in tmpl.items():
|
|
268
300
|
name = f"{prefix}-{index}-{field}"
|
|
301
|
+
if _is_file(el):
|
|
302
|
+
self.file_fields.add(name)
|
|
303
|
+
if field in values and values[field] is not None:
|
|
304
|
+
self.set_file(name, *values[field])
|
|
305
|
+
continue
|
|
269
306
|
vals = _default_values(el)
|
|
270
307
|
if vals is None:
|
|
271
308
|
continue
|
|
@@ -298,7 +335,10 @@ class AdminForm:
|
|
|
298
335
|
raise FormError(f"button {button!r} not on {self.path}. Has: {self.buttons}")
|
|
299
336
|
data = [(n, v) for n, vals in self.fields.items() for v in vals]
|
|
300
337
|
data.append((button, self._button_values.get(button, "1")))
|
|
301
|
-
|
|
338
|
+
if self.files:
|
|
339
|
+
url, text = self.client.post(self.path, data, files=dict(self.files))
|
|
340
|
+
else:
|
|
341
|
+
url, text = self.client.post(self.path, data)
|
|
302
342
|
result = AdminForm(self.client, url, text)
|
|
303
343
|
if result.errors:
|
|
304
344
|
raise FormError(f"{self.path} rejected: {result.errors[:6]}")
|
|
@@ -264,3 +264,18 @@ def test_parse_datetime_uses_the_client_zone(make_client):
|
|
|
264
264
|
client, _ = make_client(lambda call: (200, {}, INDEX_PAGE))
|
|
265
265
|
with pytest.raises(ValueError):
|
|
266
266
|
client.parse_datetime("Oct. 30, 2026, noon")
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def test_post_with_files_is_multipart(make_client):
|
|
270
|
+
client, transport = make_client(lambda call: (200, {}, "ok"))
|
|
271
|
+
client.post("/admin/app/thing/5/change/", [("title", "New")], files={"upload": ("a.txt", b"hello", "text/plain")})
|
|
272
|
+
(post,) = transport.calls
|
|
273
|
+
assert post["headers"]["Content-Type"].startswith("multipart/form-data")
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def test_post_without_files_stays_urlencoded(make_client):
|
|
277
|
+
client, transport = make_client(lambda call: (200, {}, "ok"))
|
|
278
|
+
client.post("/admin/app/thing/5/change/", [("title", "New")])
|
|
279
|
+
(post,) = transport.calls
|
|
280
|
+
assert post["headers"]["Content-Type"] == "application/x-www-form-urlencoded"
|
|
281
|
+
assert post["form"] == [("title", "New")]
|
|
@@ -50,8 +50,9 @@ class FakeClient:
|
|
|
50
50
|
def request(self, method, path):
|
|
51
51
|
return self.lands_on or path, PAGE
|
|
52
52
|
|
|
53
|
-
def post(self, path, data):
|
|
53
|
+
def post(self, path, data, **kwargs):
|
|
54
54
|
self.posted = (path, data)
|
|
55
|
+
self.files = kwargs.get("files")
|
|
55
56
|
return self.lands_on or path, self.answer
|
|
56
57
|
|
|
57
58
|
|
|
@@ -158,3 +159,44 @@ def test_base_url_is_only_stripped_as_a_whole_prefix():
|
|
|
158
159
|
assert AdminForm(c, other, PAGE).path == other
|
|
159
160
|
assert AdminForm(c, "https://admin.example.com", PAGE).path == "/"
|
|
160
161
|
assert AdminForm(FakeClient(), "https://x.test/admin/a/b/1/change/", PAGE).path == "https://x.test/admin/a/b/1/change/"
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
INLINE_FILE_PAGE = """
|
|
165
|
+
<html><body><div id="content"><form method="post"><input type="hidden" name="csrfmiddlewaretoken" value="tok">
|
|
166
|
+
<input type="hidden" name="cases-TOTAL_FORMS" value="0"><input type="hidden" name="cases-INITIAL_FORMS" value="0">
|
|
167
|
+
<input type="hidden" name="cases-MIN_NUM_FORMS" value="0"><input type="hidden" name="cases-MAX_NUM_FORMS" value="10">
|
|
168
|
+
<table><tr class="empty-form"><td><textarea name="cases-__prefix__-output"></textarea>
|
|
169
|
+
<input type="file" name="cases-__prefix__-output_file"><input type="file" name="cases-__prefix__-locked_file" disabled></td></tr></table>
|
|
170
|
+
<input type="submit" name="_continue" value="Save and continue editing"></form></div></body></html>
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_file_input_is_known_but_not_posted_as_a_field():
|
|
175
|
+
f = form()
|
|
176
|
+
assert f.has_file("upload") and not f.has("upload")
|
|
177
|
+
c = FakeClient()
|
|
178
|
+
AdminForm(c, "/admin/app/thing/5/change/", PAGE).submit()
|
|
179
|
+
assert c.files is None and "upload" not in dict(c.posted[1])
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def test_set_file_sends_multipart_files():
|
|
183
|
+
c = FakeClient()
|
|
184
|
+
f = AdminForm(c, "/admin/app/thing/5/change/", PAGE)
|
|
185
|
+
f.set_file("upload", "big.txt", "x" * 3000, "text/plain")
|
|
186
|
+
f.submit()
|
|
187
|
+
assert c.files == {"upload": ("big.txt", b"x" * 3000, "text/plain")}
|
|
188
|
+
assert ("title", "Old") in c.posted[1]
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_set_file_on_unknown_field_raises():
|
|
192
|
+
with pytest.raises(FormError):
|
|
193
|
+
form().set_file("nope", "a.txt", b"1")
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def test_add_row_takes_a_file_for_an_inline_file_input():
|
|
197
|
+
c = FakeClient()
|
|
198
|
+
f = AdminForm(c, "/admin/app/question/9/change/", INLINE_FILE_PAGE)
|
|
199
|
+
i = f.add_row("cases", {"output": "", "output_file": ("out.txt", "long output")})
|
|
200
|
+
assert f.has_file(f"cases-{i}-output_file") and not f.has_file(f"cases-{i}-locked_file")
|
|
201
|
+
f.submit()
|
|
202
|
+
assert c.files == {"cases-0-output_file": ("out.txt", b"long output", "application/octet-stream")}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_admin_remote-0.1.0 → django_admin_remote-0.2.0}/src/django_admin_remote/exceptions.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|