django-admin-remote 0.2.0__tar.gz → 0.3.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.2.0 → django_admin_remote-0.3.0}/CHANGELOG.md +11 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/PKG-INFO +22 -3
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/README.md +21 -2
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/pyproject.toml +1 -1
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/__init__.py +3 -2
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/exceptions.py +18 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/form.py +46 -2
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/tests/test_form.py +47 -1
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/.gitignore +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/LICENSE +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/client.py +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/dates.py +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/parse.py +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/py.typed +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/tests/conftest.py +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/tests/fixtures/change_form.html +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/tests/fixtures/changelist.html +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/tests/test_client.py +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/tests/test_dates.py +0 -0
- {django_admin_remote-0.2.0 → django_admin_remote-0.3.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.3.0 - 2026-11-27
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- Error banner check. After a form post the admin may answer 200 with an error or warning banner instead of redirecting. `AdminForm.banners()` returns the message list by level, `AdminForm.success_messages` the success texts, and `AdminForm.check_banners(require_success=False, allow_warnings=False)` raises the new `AdminBannerError` (a `FormError` subclass with `.errors`, `.warnings`, `.success`).
|
|
11
|
+
- `AdminForm.submit(..., check_banners=True, require_success=False)` runs that check on the answer.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- Nothing by default: `submit()` behaves as before unless `check_banners=True` is passed.
|
|
16
|
+
|
|
6
17
|
## 0.2.0 - 2026-11-26
|
|
7
18
|
|
|
8
19
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: django-admin-remote
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.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
|
|
@@ -110,10 +110,11 @@ When the session is missing or has expired the client raises `NotLoggedIn`. It n
|
|
|
110
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. |
|
|
111
111
|
| `AdminForm.total(prefix) / initial(prefix) / row_text / row_link_id` | Inspect inline formsets. |
|
|
112
112
|
| `AdminForm.readonly(name) / object_id()` | Displayed read-only text, primary key from the URL. |
|
|
113
|
-
| `AdminForm.submit(button="_continue")` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
113
|
+
| `AdminForm.submit(button="_continue", check_banners=False, require_success=False)` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
114
|
+
| `AdminForm.banners() / success_messages / check_banners(require_success=False, allow_warnings=False)` | The admin message list by level, and a check that raises `AdminBannerError` on an error or warning banner. |
|
|
114
115
|
| `parse_change_form(html)`, `parse_changelist(html)` | The parsers, usable on HTML you fetched yourself. |
|
|
115
116
|
| `parse_admin_datetime(text, tz)` | "Nov. 11, 2026, 1:43 p.m." to `2026-11-11T13:43:00+01:00`. |
|
|
116
|
-
| `NotLoggedIn`, `FormError`, `AdminHTTPError` | Exceptions, all subclasses of `AdminError`. |
|
|
117
|
+
| `NotLoggedIn`, `FormError`, `AdminBannerError`, `AdminHTTPError` | Exceptions, all subclasses of `AdminError`. `AdminBannerError` is a `FormError`. |
|
|
117
118
|
|
|
118
119
|
Every public function and method has a docstring with the details.
|
|
119
120
|
|
|
@@ -142,6 +143,24 @@ form.add_row("attachment_set", {"title": "Errata", "document": ("errata.txt", "P
|
|
|
142
143
|
form.submit()
|
|
143
144
|
```
|
|
144
145
|
|
|
146
|
+
### Error banners
|
|
147
|
+
|
|
148
|
+
The admin sometimes answers a POST with 200 and an error banner (a message at the error level, `p.errornote`, `ul.errorlist`) instead of redirecting. `submit()` already raises `FormError` for `errornote` and `errorlist`. For the message list too, and to insist on a success message, check the answer:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from django_admin_remote import AdminBannerError
|
|
152
|
+
|
|
153
|
+
saved = form.submit("_continue", check_banners=True, require_success=True)
|
|
154
|
+
# or on any page you hold:
|
|
155
|
+
try:
|
|
156
|
+
saved.check_banners(require_success=True)
|
|
157
|
+
except AdminBannerError as err:
|
|
158
|
+
print(err.errors, err.warnings)
|
|
159
|
+
print(saved.success_messages) # ["The thing "x" was changed successfully."]
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Warning-level messages raise too, unless `allow_warnings=True`. Without `check_banners=True`, `submit()` behaves as in 0.2.
|
|
163
|
+
|
|
145
164
|
### Dates
|
|
146
165
|
|
|
147
166
|
The admin shows datetimes in the server's time zone without naming it. Pass the zone to get an unambiguous value:
|
|
@@ -77,10 +77,11 @@ When the session is missing or has expired the client raises `NotLoggedIn`. It n
|
|
|
77
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. |
|
|
78
78
|
| `AdminForm.total(prefix) / initial(prefix) / row_text / row_link_id` | Inspect inline formsets. |
|
|
79
79
|
| `AdminForm.readonly(name) / object_id()` | Displayed read-only text, primary key from the URL. |
|
|
80
|
-
| `AdminForm.submit(button="_continue")` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
80
|
+
| `AdminForm.submit(button="_continue", check_banners=False, require_success=False)` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
81
|
+
| `AdminForm.banners() / success_messages / check_banners(require_success=False, allow_warnings=False)` | The admin message list by level, and a check that raises `AdminBannerError` on an error or warning banner. |
|
|
81
82
|
| `parse_change_form(html)`, `parse_changelist(html)` | The parsers, usable on HTML you fetched yourself. |
|
|
82
83
|
| `parse_admin_datetime(text, tz)` | "Nov. 11, 2026, 1:43 p.m." to `2026-11-11T13:43:00+01:00`. |
|
|
83
|
-
| `NotLoggedIn`, `FormError`, `AdminHTTPError` | Exceptions, all subclasses of `AdminError`. |
|
|
84
|
+
| `NotLoggedIn`, `FormError`, `AdminBannerError`, `AdminHTTPError` | Exceptions, all subclasses of `AdminError`. `AdminBannerError` is a `FormError`. |
|
|
84
85
|
|
|
85
86
|
Every public function and method has a docstring with the details.
|
|
86
87
|
|
|
@@ -109,6 +110,24 @@ form.add_row("attachment_set", {"title": "Errata", "document": ("errata.txt", "P
|
|
|
109
110
|
form.submit()
|
|
110
111
|
```
|
|
111
112
|
|
|
113
|
+
### Error banners
|
|
114
|
+
|
|
115
|
+
The admin sometimes answers a POST with 200 and an error banner (a message at the error level, `p.errornote`, `ul.errorlist`) instead of redirecting. `submit()` already raises `FormError` for `errornote` and `errorlist`. For the message list too, and to insist on a success message, check the answer:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from django_admin_remote import AdminBannerError
|
|
119
|
+
|
|
120
|
+
saved = form.submit("_continue", check_banners=True, require_success=True)
|
|
121
|
+
# or on any page you hold:
|
|
122
|
+
try:
|
|
123
|
+
saved.check_banners(require_success=True)
|
|
124
|
+
except AdminBannerError as err:
|
|
125
|
+
print(err.errors, err.warnings)
|
|
126
|
+
print(saved.success_messages) # ["The thing "x" was changed successfully."]
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Warning-level messages raise too, unless `allow_warnings=True`. Without `check_banners=True`, `submit()` behaves as in 0.2.
|
|
130
|
+
|
|
112
131
|
### Dates
|
|
113
132
|
|
|
114
133
|
The admin shows datetimes in the server's time zone without naming it. Pass the zone to get an unambiguous value:
|
|
@@ -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.3.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"
|
|
@@ -2,17 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
Nothing is installed on the server. See the README for a quickstart.
|
|
4
4
|
"""
|
|
5
|
-
__version__ = "0.
|
|
5
|
+
__version__ = "0.3.0"
|
|
6
6
|
|
|
7
7
|
from .client import AdminClient
|
|
8
8
|
from .dates import parse_admin_datetime
|
|
9
|
-
from .exceptions import AdminError, AdminHTTPError, FormError, NotLoggedIn
|
|
9
|
+
from .exceptions import AdminBannerError, AdminError, AdminHTTPError, FormError, NotLoggedIn
|
|
10
10
|
from .form import AdminForm
|
|
11
11
|
from .parse import field_value, parse_change_form, parse_changelist
|
|
12
12
|
|
|
13
13
|
__all__ = [
|
|
14
14
|
"AdminClient",
|
|
15
15
|
"AdminForm",
|
|
16
|
+
"AdminBannerError",
|
|
16
17
|
"AdminError",
|
|
17
18
|
"AdminHTTPError",
|
|
18
19
|
"FormError",
|
{django_admin_remote-0.2.0 → django_admin_remote-0.3.0}/src/django_admin_remote/exceptions.py
RENAMED
|
@@ -14,6 +14,24 @@ class FormError(AdminError):
|
|
|
14
14
|
"""A form could not be filled in or the admin rejected it with validation errors."""
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
class AdminBannerError(FormError):
|
|
18
|
+
"""The admin answered 200 with an error or warning banner (or no success message) instead of redirecting.
|
|
19
|
+
|
|
20
|
+
A FormError subclass, so ``except FormError`` still catches it.
|
|
21
|
+
|
|
22
|
+
Attributes:
|
|
23
|
+
errors: texts of the error banners (message list at the error level, errornote, errorlist).
|
|
24
|
+
warnings: texts of the warning banners.
|
|
25
|
+
success: texts of the success messages the page showed.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, message: str, errors=(), warnings=(), success=()) -> None:
|
|
29
|
+
super().__init__(message)
|
|
30
|
+
self.errors = list(errors)
|
|
31
|
+
self.warnings = list(warnings)
|
|
32
|
+
self.success = list(success)
|
|
33
|
+
|
|
34
|
+
|
|
17
35
|
class AdminHTTPError(AdminError):
|
|
18
36
|
"""The site answered with an HTTP error status.
|
|
19
37
|
|
|
@@ -11,7 +11,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
|
|
|
11
11
|
|
|
12
12
|
from bs4 import Tag
|
|
13
13
|
|
|
14
|
-
from .exceptions import FormError, NotLoggedIn
|
|
14
|
+
from .exceptions import AdminBannerError, FormError, NotLoggedIn
|
|
15
15
|
from .parse import is_login_page, soup
|
|
16
16
|
|
|
17
17
|
SKIP_TYPES = {"submit", "button", "file", "image", "reset"}
|
|
@@ -318,7 +318,8 @@ class AdminForm:
|
|
|
318
318
|
"""Tick the DELETE box of a saved inline row. It is deleted when the form is submitted."""
|
|
319
319
|
self.set(f"{prefix}-{index}-DELETE", True)
|
|
320
320
|
|
|
321
|
-
def submit(self, button: str = "_continue"
|
|
321
|
+
def submit(self, button: str = "_continue", check_banners: bool = False,
|
|
322
|
+
require_success: bool = False) -> "AdminForm":
|
|
322
323
|
"""POST the form by "pressing" the named submit button.
|
|
323
324
|
|
|
324
325
|
The default ``_continue`` is "Save and continue editing", which lands back on the
|
|
@@ -328,6 +329,10 @@ class AdminForm:
|
|
|
328
329
|
Returns the page the admin answered with as a new AdminForm. Raises FormError if
|
|
329
330
|
the button does not exist or the admin reports validation errors. The POST is sent
|
|
330
331
|
once and never retried.
|
|
332
|
+
|
|
333
|
+
With ``check_banners=True`` the answer is also run through :meth:`check_banners`
|
|
334
|
+
(``require_success`` is passed on), so an error or warning banner that came with a
|
|
335
|
+
200 page raises :class:`AdminBannerError`. Off by default.
|
|
331
336
|
"""
|
|
332
337
|
if self.form is None:
|
|
333
338
|
raise FormError(f"no form found on {self.path}")
|
|
@@ -342,8 +347,47 @@ class AdminForm:
|
|
|
342
347
|
result = AdminForm(self.client, url, text)
|
|
343
348
|
if result.errors:
|
|
344
349
|
raise FormError(f"{self.path} rejected: {result.errors[:6]}")
|
|
350
|
+
if check_banners:
|
|
351
|
+
result.check_banners(require_success=require_success)
|
|
345
352
|
return result
|
|
346
353
|
|
|
354
|
+
def banners(self) -> Dict[str, List[str]]:
|
|
355
|
+
"""The admin's message list by level: ``{"success": [...], "warning": [...], "error": [...], "info": [...]}``.
|
|
356
|
+
|
|
357
|
+
The level is the CSS class of the message (``success``, ``warning``, ``error``);
|
|
358
|
+
a message with none of them counts as ``info``.
|
|
359
|
+
"""
|
|
360
|
+
out: Dict[str, List[str]] = {"success": [], "warning": [], "error": [], "info": []}
|
|
361
|
+
for li in self.doc.select("ul.messagelist li"):
|
|
362
|
+
level = next((c for c in li.get("class") or [] if c in out), "info")
|
|
363
|
+
out[level].append(li.get_text(" ", strip=True))
|
|
364
|
+
return out
|
|
365
|
+
|
|
366
|
+
@property
|
|
367
|
+
def success_messages(self) -> List[str]:
|
|
368
|
+
"""Texts of the success messages on the page (for example "... was changed successfully.")."""
|
|
369
|
+
return self.banners()["success"]
|
|
370
|
+
|
|
371
|
+
def check_banners(self, require_success: bool = False, allow_warnings: bool = False) -> Dict[str, List[str]]:
|
|
372
|
+
"""Raise :class:`AdminBannerError` if the page carries an error banner.
|
|
373
|
+
|
|
374
|
+
Error banners are error-level messages, ``p.errornote`` and ``ul.errorlist`` items.
|
|
375
|
+
Warning-level messages count too unless ``allow_warnings`` is true. With
|
|
376
|
+
``require_success=True`` a page without any success message raises as well (a save
|
|
377
|
+
that the admin did not confirm). Returns :meth:`banners` when the page is fine.
|
|
378
|
+
"""
|
|
379
|
+
seen = self.banners()
|
|
380
|
+
errors = seen["error"] + [e for e in self.errors if e not in seen["error"]]
|
|
381
|
+
warnings = [] if allow_warnings else seen["warning"]
|
|
382
|
+
bad = errors + warnings
|
|
383
|
+
if bad:
|
|
384
|
+
raise AdminBannerError(f"{self.path}: the admin reported: {'; '.join(bad)}",
|
|
385
|
+
errors, warnings, seen["success"])
|
|
386
|
+
if require_success and not seen["success"]:
|
|
387
|
+
raise AdminBannerError(f"{self.path}: the admin did not confirm the change (no success message)",
|
|
388
|
+
errors, warnings, seen["success"])
|
|
389
|
+
return seen
|
|
390
|
+
|
|
347
391
|
def object_id(self) -> Optional[int]:
|
|
348
392
|
"""Numeric primary key from the page path, None on an add page."""
|
|
349
393
|
m = re.search(r"/(\d+)/change/", self.path)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import pytest
|
|
2
2
|
|
|
3
|
-
from django_admin_remote import AdminForm, FormError, NotLoggedIn
|
|
3
|
+
from django_admin_remote import AdminBannerError, AdminForm, FormError, NotLoggedIn
|
|
4
4
|
|
|
5
5
|
PAGE = """
|
|
6
6
|
<html><body><div id="content">
|
|
@@ -200,3 +200,49 @@ def test_add_row_takes_a_file_for_an_inline_file_input():
|
|
|
200
200
|
assert f.has_file(f"cases-{i}-output_file") and not f.has_file(f"cases-{i}-locked_file")
|
|
201
201
|
f.submit()
|
|
202
202
|
assert c.files == {"cases-0-output_file": ("out.txt", b"long output", "application/octet-stream")}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _with_messages(*items):
|
|
206
|
+
lis = "".join(f'<li class="{c}">{t}</li>' for c, t in items)
|
|
207
|
+
return PAGE.replace('<div id="content">', f'<div id="content"><ul class="messagelist">{lis}</ul>', 1)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def test_banners_by_level_and_success_messages():
|
|
211
|
+
page = _with_messages(("success", "Saved."), ("warning", "Careful"), ("error", "Nope"), ("", "FYI"))
|
|
212
|
+
f = AdminForm(FakeClient(), "/admin/app/thing/5/change/", page)
|
|
213
|
+
assert f.banners() == {"success": ["Saved."], "warning": ["Careful"], "error": ["Nope"], "info": ["FYI"]}
|
|
214
|
+
assert f.success_messages == ["Saved."]
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_check_banners_raises_with_messages():
|
|
218
|
+
page = _with_messages(("error", "Could not run it"), ("warning", "Slow"))
|
|
219
|
+
f = AdminForm(FakeClient(), "/admin/app/thing/5/change/", page)
|
|
220
|
+
with pytest.raises(AdminBannerError) as exc:
|
|
221
|
+
f.check_banners()
|
|
222
|
+
assert exc.value.errors == ["Could not run it"] and exc.value.warnings == ["Slow"]
|
|
223
|
+
assert "Could not run it" in str(exc.value) and isinstance(exc.value, FormError)
|
|
224
|
+
with pytest.raises(AdminBannerError):
|
|
225
|
+
f.check_banners(allow_warnings=True) # the error still counts
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def test_check_banners_warning_and_success_rules():
|
|
229
|
+
f = AdminForm(FakeClient(), "/p/", _with_messages(("warning", "Slow"), ("success", "ok")))
|
|
230
|
+
with pytest.raises(AdminBannerError):
|
|
231
|
+
f.check_banners()
|
|
232
|
+
assert f.check_banners(allow_warnings=True)["success"] == ["ok"]
|
|
233
|
+
bare = AdminForm(FakeClient(), "/p/", PAGE)
|
|
234
|
+
assert bare.check_banners() is not None # no banners and success not required: fine
|
|
235
|
+
with pytest.raises(AdminBannerError, match="no success message"):
|
|
236
|
+
bare.check_banners(require_success=True)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def test_submit_checks_banners_only_when_asked():
|
|
240
|
+
page = _with_messages(("error", "Refused"))
|
|
241
|
+
f = form(FakeClient(answer=page))
|
|
242
|
+
assert f.submit("_save").banners()["error"] == ["Refused"] # default: as in 0.2
|
|
243
|
+
with pytest.raises(AdminBannerError, match="Refused"):
|
|
244
|
+
form(FakeClient(answer=page)).submit("_save", check_banners=True)
|
|
245
|
+
ok = _with_messages(("success", "Saved."))
|
|
246
|
+
assert form(FakeClient(answer=ok)).submit("_save", check_banners=True, require_success=True).success_messages == ["Saved."]
|
|
247
|
+
with pytest.raises(AdminBannerError, match="no success message"):
|
|
248
|
+
form(FakeClient()).submit("_save", check_banners=True, require_success=True)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|