django-admin-remote 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_admin_remote-0.1.0/.gitignore +7 -0
- django_admin_remote-0.1.0/CHANGELOG.md +18 -0
- django_admin_remote-0.1.0/LICENSE +21 -0
- django_admin_remote-0.1.0/PKG-INFO +201 -0
- django_admin_remote-0.1.0/README.md +168 -0
- django_admin_remote-0.1.0/pyproject.toml +53 -0
- django_admin_remote-0.1.0/src/django_admin_remote/__init__.py +25 -0
- django_admin_remote-0.1.0/src/django_admin_remote/client.py +278 -0
- django_admin_remote-0.1.0/src/django_admin_remote/dates.py +57 -0
- django_admin_remote-0.1.0/src/django_admin_remote/exceptions.py +28 -0
- django_admin_remote-0.1.0/src/django_admin_remote/form.py +310 -0
- django_admin_remote-0.1.0/src/django_admin_remote/parse.py +233 -0
- django_admin_remote-0.1.0/src/django_admin_remote/py.typed +0 -0
- django_admin_remote-0.1.0/tests/conftest.py +59 -0
- django_admin_remote-0.1.0/tests/fixtures/change_form.html +87 -0
- django_admin_remote-0.1.0/tests/fixtures/changelist.html +7 -0
- django_admin_remote-0.1.0/tests/test_client.py +266 -0
- django_admin_remote-0.1.0/tests/test_dates.py +29 -0
- django_admin_remote-0.1.0/tests/test_form.py +160 -0
- django_admin_remote-0.1.0/tests/test_parse.py +145 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are listed here.
|
|
4
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/).
|
|
5
|
+
|
|
6
|
+
## 0.1.0 - 2026-11-11
|
|
7
|
+
|
|
8
|
+
First release.
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `AdminClient`: polite `requests` based client with a delay between requests, manual redirect following, GET retries on 5xx, no POST retries, and a same-host rule.
|
|
13
|
+
- Authentication from existing cookies (cookie dict, cookie jar or a Playwright `storage_state`) and `AdminClient.login()` for the standard admin login form.
|
|
14
|
+
- `AdminClient.changelist()`: iterate over the ids of a change list across all pages, with filters and a limit. Works with both 0-based and 1-based `?p=` page numbers.
|
|
15
|
+
- `AdminClient.read()` and `parse_change_form()`: editable fields, read-only fields with the stored value recovered, boolean icons, links, split date and time widgets, inline formset rows.
|
|
16
|
+
- `AdminForm`: load an add or change page and submit it like a browser, including select values by label, inline row add and delete, and custom submit buttons.
|
|
17
|
+
- `parse_changelist()` and `parse_admin_datetime()`.
|
|
18
|
+
- Exceptions `NotLoggedIn`, `FormError`, `AdminHTTPError` with the common base `AdminError`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Krushn Dayshmookh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-admin-remote
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Read and edit a remote Django admin site over HTTP by parsing its stock HTML. Nothing to install on the server.
|
|
5
|
+
Project-URL: Repository, https://github.com/krushndayshmookh/django-admin-remote
|
|
6
|
+
Project-URL: Issues, https://github.com/krushndayshmookh/django-admin-remote/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/krushndayshmookh/django-admin-remote/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Krushn Dayshmookh
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: admin,automation,client,django,formset,inlines,scraping
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: Django
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Requires-Dist: beautifulsoup4>=4.9
|
|
28
|
+
Requires-Dist: requests>=2.25
|
|
29
|
+
Requires-Dist: tzdata; sys_platform == 'win32'
|
|
30
|
+
Provides-Extra: test
|
|
31
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# django-admin-remote
|
|
35
|
+
|
|
36
|
+
Read and edit a remote Django admin site from Python, over plain HTTP, by parsing the HTML the stock admin already serves. Nothing is installed on the server: if you can use the admin in a browser, you can script it.
|
|
37
|
+
|
|
38
|
+
It is meant for the case where you have admin access to a Django site but no API and no way to deploy code to it.
|
|
39
|
+
|
|
40
|
+
- Reads change forms: editable fields, read-only fields (with the stored value recovered, not just the displayed text), yes/no icons, links, and inline formset rows.
|
|
41
|
+
- Reads change lists: ids, total count, all pages, with the same filters the admin puts in the URL.
|
|
42
|
+
- Submits add and change forms the way a browser does: every field is sent back, selects can be set by label, inline rows can be added and deleted, custom submit buttons can be pressed, validation errors are raised.
|
|
43
|
+
- Logs in with a username and password, or reuses the cookies of a browser session for sites behind SSO.
|
|
44
|
+
|
|
45
|
+
## Install
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install django-admin-remote
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Python 3.10 or newer. Depends on `requests` and `beautifulsoup4`.
|
|
52
|
+
|
|
53
|
+
## Quickstart
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from django_admin_remote import AdminClient, FormError
|
|
57
|
+
|
|
58
|
+
admin = AdminClient("https://admin.example.com", delay=0.5)
|
|
59
|
+
admin.login("alice", "correct horse battery staple")
|
|
60
|
+
|
|
61
|
+
# ids of every matching row, across all pages of the change list
|
|
62
|
+
ids = list(admin.changelist("/admin/blog/post/", status__exact="draft", limit=50))
|
|
63
|
+
|
|
64
|
+
# read one object, including read-only fields and inline rows
|
|
65
|
+
post = admin.read(f"/admin/blog/post/{ids[0]}/change/")
|
|
66
|
+
print(post["fields"]["title"]["value"], post["inlines"]["comment_set"])
|
|
67
|
+
# change it like a browser would: untouched fields are sent back as they are
|
|
68
|
+
form = admin.form(f"/admin/blog/post/{ids[0]}/change/")
|
|
69
|
+
form.set("title", "A better title")
|
|
70
|
+
form.set("status", "Published") # a select: option value or visible label
|
|
71
|
+
form.add_row("comment_set", {"text": "First!"})
|
|
72
|
+
try:
|
|
73
|
+
saved = form.submit("_continue") # "Save and continue editing"
|
|
74
|
+
except FormError as err:
|
|
75
|
+
print("the admin rejected the form:", err)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
To create an object, load the add page (`admin.form("/admin/blog/post/add/")`), set the fields, submit, and read `saved.object_id()`.
|
|
79
|
+
|
|
80
|
+
## Authentication
|
|
81
|
+
|
|
82
|
+
**Username and password.** `admin.login(username, password)` uses the standard admin login form at `/admin/login/` (change it with `login_path=`). The password is posted once and not kept.
|
|
83
|
+
|
|
84
|
+
**Existing cookies.** Sites that log in through SSO (Google, Okta and so on) have no password form to post to. Log in with a real browser once and hand the session cookies over:
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
admin = AdminClient("https://admin.example.com", cookies={"sessionid": "..."})
|
|
88
|
+
# or a Playwright storage_state file, as saved by context.storage_state(path="state.json")
|
|
89
|
+
admin = AdminClient("https://admin.example.com", storage_state="state.json")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Playwright is not a dependency. Only the JSON file it writes is read, and only cookies whose domain matches the base URL are used.
|
|
93
|
+
|
|
94
|
+
When the session is missing or has expired the client raises `NotLoggedIn`. It notices this from a redirect to a login page or to another host, and from the login form showing up where an admin page was expected.
|
|
95
|
+
|
|
96
|
+
## API overview
|
|
97
|
+
|
|
98
|
+
| Call | What it does |
|
|
99
|
+
| --- | --- |
|
|
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
|
+
| `client.login(username, password)` | Log in through the admin login form. |
|
|
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. |
|
|
104
|
+
| `client.changelist(path, limit=None, params=None, **filters)` | Iterator over the ids of a change list, all pages. |
|
|
105
|
+
| `client.read(path)` | `parse_change_form` of a page: `{"fields", "inlines", "logged_out"}`. |
|
|
106
|
+
| `client.form(path)` | An `AdminForm` for an add or change page. |
|
|
107
|
+
| `client.parse_datetime(text)` | `parse_admin_datetime` with the client's `tz`. |
|
|
108
|
+
| `AdminForm.has / get / set / set_if_present` | Read and change the values that will be posted. |
|
|
109
|
+
| `AdminForm.add_row(prefix, values) / delete_row(prefix, index)` | Inline formset rows. |
|
|
110
|
+
| `AdminForm.total(prefix) / initial(prefix) / row_text / row_link_id` | Inspect inline formsets. |
|
|
111
|
+
| `AdminForm.readonly(name) / object_id()` | Displayed read-only text, primary key from the URL. |
|
|
112
|
+
| `AdminForm.submit(button="_continue")` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
113
|
+
| `parse_change_form(html)`, `parse_changelist(html)` | The parsers, usable on HTML you fetched yourself. |
|
|
114
|
+
| `parse_admin_datetime(text, tz)` | "Nov. 11, 2026, 1:43 p.m." to `2026-11-11T13:43:00+01:00`. |
|
|
115
|
+
| `NotLoggedIn`, `FormError`, `AdminHTTPError` | Exceptions, all subclasses of `AdminError`. |
|
|
116
|
+
|
|
117
|
+
Every public function and method has a docstring with the details.
|
|
118
|
+
|
|
119
|
+
### Inline formsets
|
|
120
|
+
|
|
121
|
+
Inline fields are addressed by their HTML names, `<prefix>-<index>-<field>`. The prefix is what the admin uses in the page (often the related name, such as `comment_set`); look at `parse_change_form(...)["inlines"].keys()` to find it.
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
form = admin.form("/admin/blog/post/7/change/")
|
|
125
|
+
form.set("comment_set-0-text", "edited") # change a saved row
|
|
126
|
+
form.delete_row("comment_set", 1) # tick its DELETE box
|
|
127
|
+
index = form.add_row("comment_set", {"text": "new"}) # built from the empty-form template
|
|
128
|
+
form.submit()
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`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
|
+
### Dates
|
|
134
|
+
|
|
135
|
+
The admin shows datetimes in the server's time zone without naming it. Pass the zone to get an unambiguous value:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from django_admin_remote import parse_admin_datetime
|
|
139
|
+
parse_admin_datetime("Sept. 29, 2026, 3:03 a.m.", "Europe/Paris") # '2026-09-29T03:03:00+02:00'
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## How it reads read-only fields
|
|
143
|
+
|
|
144
|
+
An editable field carries its exact value in the HTML (`value="..."`, the textarea body, the selected option). A read-only field is only displayed, so the stored value has to be worked out from what Django rendered.
|
|
145
|
+
|
|
146
|
+
For plain values Django's admin renders a read-only field as `linebreaksbr(escape(value))`: the value is HTML-escaped, then every newline becomes `<br>`. The parser reverses exactly those two steps on the cell's inner HTML:
|
|
147
|
+
|
|
148
|
+
1. `<br>` is turned back into a newline.
|
|
149
|
+
2. The result is HTML-unescaped once.
|
|
150
|
+
|
|
151
|
+
The parser strips and collapses nothing. Whatever is in the cell comes back: inner newlines, a trailing newline, leading spaces, and a stored `a < b` (text that itself contains an entity) as `a < b` and not as `a < b`. Taking the visible text of the cell, as a generic scraper would, loses these.
|
|
152
|
+
|
|
153
|
+
One caveat is on Django's side. Newer Django versions strip leading and trailing whitespace from a read-only string before rendering it (seen on Django 6.1, where `display_for_value` calls `strip()`). On such a site the outer whitespace never reaches the HTML, so a stored `5\n` reads as `5`. Older versions render it in full. If exact outer whitespace matters, check what your site sends.
|
|
154
|
+
|
|
155
|
+
The other cases:
|
|
156
|
+
|
|
157
|
+
- The yes / no / unknown icons become `True`, `False` and `None`.
|
|
158
|
+
- A cell made only of links (a foreign key with a link, for example) becomes `{"text": ..., "links": [...]}`.
|
|
159
|
+
- A cell showing just `-` is Django's placeholder for an empty value and becomes `""`.
|
|
160
|
+
|
|
161
|
+
What cannot be recovered: anything the admin formats before display. Dates come back as displayed text (see `parse_admin_datetime`), numbers may be localized, foreign keys show the object's `str()`, and a read-only method that returns HTML through `format_html` or `mark_safe` comes back with that markup. A stored value that is literally `-` is indistinguishable from an empty one.
|
|
162
|
+
|
|
163
|
+
## Limitations
|
|
164
|
+
|
|
165
|
+
- **It relies on stock admin markup.** The parsers look for the classes and ids Django's own templates produce (`form-row`, `field-<name>`, `readonly`, `inline-group`, `result_list`, `paginator`, `login-form`). Custom admin themes and replacement admin skins change that markup and may break parsing partly or completely. Check against your site before relying on it. The offline tests use markup saved from an older Django; a manual run against Django 6.1 (login, change list pages and filters, read, save, inline add and delete) also passed.
|
|
166
|
+
- **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
|
+
- **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
|
+
- **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
|
+
- No file uploads. File inputs are left out of the POST, which keeps the existing file.
|
|
170
|
+
- 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
|
+
- Change list parsing returns ids, the count and the pages, not the column values. Read each object for its data.
|
|
172
|
+
- The total count and the dates assume an English-language admin with default formats.
|
|
173
|
+
- Primary keys in `AdminForm.object_id()`, `row_link_id()` and inline `_links` are assumed to be integers.
|
|
174
|
+
- Admin actions (the dropdown above the change list) and the delete confirmation page are not wrapped. `client.get` and `client.post` are there if you need them.
|
|
175
|
+
|
|
176
|
+
## Prior art
|
|
177
|
+
|
|
178
|
+
[`django-admin-client`](https://pypi.org/project/django-admin-client/) had the same idea first: a Python client for a Django admin site that works over HTTP. Its last release on PyPI is 0.2.4 from March 2019. It logs in with a username and password, offers list, get, add, change and delete, and can generate a client spec for a site. If that covers your case, look at it first.
|
|
179
|
+
|
|
180
|
+
`django-admin-remote` was written for things its documentation does not mention:
|
|
181
|
+
|
|
182
|
+
- inline formsets (read, add and delete rows),
|
|
183
|
+
- read-only fields with the stored value recovered,
|
|
184
|
+
- change list filters and pagination across pages, on both old and new Django page numbering,
|
|
185
|
+
- sessions from browser cookies, for admin sites behind SSO,
|
|
186
|
+
- custom submit buttons, and built-in request pacing.
|
|
187
|
+
|
|
188
|
+
On the other side, this package has no delete helper and no spec generation.
|
|
189
|
+
|
|
190
|
+
## Development
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
python -m pip install -e ".[test]"
|
|
194
|
+
python -m pytest -q
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The tests run offline against saved admin HTML and a fake HTTP transport.
|
|
198
|
+
|
|
199
|
+
## License
|
|
200
|
+
|
|
201
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# django-admin-remote
|
|
2
|
+
|
|
3
|
+
Read and edit a remote Django admin site from Python, over plain HTTP, by parsing the HTML the stock admin already serves. Nothing is installed on the server: if you can use the admin in a browser, you can script it.
|
|
4
|
+
|
|
5
|
+
It is meant for the case where you have admin access to a Django site but no API and no way to deploy code to it.
|
|
6
|
+
|
|
7
|
+
- Reads change forms: editable fields, read-only fields (with the stored value recovered, not just the displayed text), yes/no icons, links, and inline formset rows.
|
|
8
|
+
- Reads change lists: ids, total count, all pages, with the same filters the admin puts in the URL.
|
|
9
|
+
- Submits add and change forms the way a browser does: every field is sent back, selects can be set by label, inline rows can be added and deleted, custom submit buttons can be pressed, validation errors are raised.
|
|
10
|
+
- Logs in with a username and password, or reuses the cookies of a browser session for sites behind SSO.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install django-admin-remote
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Python 3.10 or newer. Depends on `requests` and `beautifulsoup4`.
|
|
19
|
+
|
|
20
|
+
## Quickstart
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from django_admin_remote import AdminClient, FormError
|
|
24
|
+
|
|
25
|
+
admin = AdminClient("https://admin.example.com", delay=0.5)
|
|
26
|
+
admin.login("alice", "correct horse battery staple")
|
|
27
|
+
|
|
28
|
+
# ids of every matching row, across all pages of the change list
|
|
29
|
+
ids = list(admin.changelist("/admin/blog/post/", status__exact="draft", limit=50))
|
|
30
|
+
|
|
31
|
+
# read one object, including read-only fields and inline rows
|
|
32
|
+
post = admin.read(f"/admin/blog/post/{ids[0]}/change/")
|
|
33
|
+
print(post["fields"]["title"]["value"], post["inlines"]["comment_set"])
|
|
34
|
+
# change it like a browser would: untouched fields are sent back as they are
|
|
35
|
+
form = admin.form(f"/admin/blog/post/{ids[0]}/change/")
|
|
36
|
+
form.set("title", "A better title")
|
|
37
|
+
form.set("status", "Published") # a select: option value or visible label
|
|
38
|
+
form.add_row("comment_set", {"text": "First!"})
|
|
39
|
+
try:
|
|
40
|
+
saved = form.submit("_continue") # "Save and continue editing"
|
|
41
|
+
except FormError as err:
|
|
42
|
+
print("the admin rejected the form:", err)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
To create an object, load the add page (`admin.form("/admin/blog/post/add/")`), set the fields, submit, and read `saved.object_id()`.
|
|
46
|
+
|
|
47
|
+
## Authentication
|
|
48
|
+
|
|
49
|
+
**Username and password.** `admin.login(username, password)` uses the standard admin login form at `/admin/login/` (change it with `login_path=`). The password is posted once and not kept.
|
|
50
|
+
|
|
51
|
+
**Existing cookies.** Sites that log in through SSO (Google, Okta and so on) have no password form to post to. Log in with a real browser once and hand the session cookies over:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
admin = AdminClient("https://admin.example.com", cookies={"sessionid": "..."})
|
|
55
|
+
# or a Playwright storage_state file, as saved by context.storage_state(path="state.json")
|
|
56
|
+
admin = AdminClient("https://admin.example.com", storage_state="state.json")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Playwright is not a dependency. Only the JSON file it writes is read, and only cookies whose domain matches the base URL are used.
|
|
60
|
+
|
|
61
|
+
When the session is missing or has expired the client raises `NotLoggedIn`. It notices this from a redirect to a login page or to another host, and from the login form showing up where an admin page was expected.
|
|
62
|
+
|
|
63
|
+
## API overview
|
|
64
|
+
|
|
65
|
+
| Call | What it does |
|
|
66
|
+
| --- | --- |
|
|
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
|
+
| `client.login(username, password)` | Log in through the admin login form. |
|
|
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. |
|
|
71
|
+
| `client.changelist(path, limit=None, params=None, **filters)` | Iterator over the ids of a change list, all pages. |
|
|
72
|
+
| `client.read(path)` | `parse_change_form` of a page: `{"fields", "inlines", "logged_out"}`. |
|
|
73
|
+
| `client.form(path)` | An `AdminForm` for an add or change page. |
|
|
74
|
+
| `client.parse_datetime(text)` | `parse_admin_datetime` with the client's `tz`. |
|
|
75
|
+
| `AdminForm.has / get / set / set_if_present` | Read and change the values that will be posted. |
|
|
76
|
+
| `AdminForm.add_row(prefix, values) / delete_row(prefix, index)` | Inline formset rows. |
|
|
77
|
+
| `AdminForm.total(prefix) / initial(prefix) / row_text / row_link_id` | Inspect inline formsets. |
|
|
78
|
+
| `AdminForm.readonly(name) / object_id()` | Displayed read-only text, primary key from the URL. |
|
|
79
|
+
| `AdminForm.submit(button="_continue")` | POST the form, returns the resulting page as an `AdminForm`. |
|
|
80
|
+
| `parse_change_form(html)`, `parse_changelist(html)` | The parsers, usable on HTML you fetched yourself. |
|
|
81
|
+
| `parse_admin_datetime(text, tz)` | "Nov. 11, 2026, 1:43 p.m." to `2026-11-11T13:43:00+01:00`. |
|
|
82
|
+
| `NotLoggedIn`, `FormError`, `AdminHTTPError` | Exceptions, all subclasses of `AdminError`. |
|
|
83
|
+
|
|
84
|
+
Every public function and method has a docstring with the details.
|
|
85
|
+
|
|
86
|
+
### Inline formsets
|
|
87
|
+
|
|
88
|
+
Inline fields are addressed by their HTML names, `<prefix>-<index>-<field>`. The prefix is what the admin uses in the page (often the related name, such as `comment_set`); look at `parse_change_form(...)["inlines"].keys()` to find it.
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
form = admin.form("/admin/blog/post/7/change/")
|
|
92
|
+
form.set("comment_set-0-text", "edited") # change a saved row
|
|
93
|
+
form.delete_row("comment_set", 1) # tick its DELETE box
|
|
94
|
+
index = form.add_row("comment_set", {"text": "new"}) # built from the empty-form template
|
|
95
|
+
form.submit()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`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
|
+
### Dates
|
|
101
|
+
|
|
102
|
+
The admin shows datetimes in the server's time zone without naming it. Pass the zone to get an unambiguous value:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from django_admin_remote import parse_admin_datetime
|
|
106
|
+
parse_admin_datetime("Sept. 29, 2026, 3:03 a.m.", "Europe/Paris") # '2026-09-29T03:03:00+02:00'
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## How it reads read-only fields
|
|
110
|
+
|
|
111
|
+
An editable field carries its exact value in the HTML (`value="..."`, the textarea body, the selected option). A read-only field is only displayed, so the stored value has to be worked out from what Django rendered.
|
|
112
|
+
|
|
113
|
+
For plain values Django's admin renders a read-only field as `linebreaksbr(escape(value))`: the value is HTML-escaped, then every newline becomes `<br>`. The parser reverses exactly those two steps on the cell's inner HTML:
|
|
114
|
+
|
|
115
|
+
1. `<br>` is turned back into a newline.
|
|
116
|
+
2. The result is HTML-unescaped once.
|
|
117
|
+
|
|
118
|
+
The parser strips and collapses nothing. Whatever is in the cell comes back: inner newlines, a trailing newline, leading spaces, and a stored `a < b` (text that itself contains an entity) as `a < b` and not as `a < b`. Taking the visible text of the cell, as a generic scraper would, loses these.
|
|
119
|
+
|
|
120
|
+
One caveat is on Django's side. Newer Django versions strip leading and trailing whitespace from a read-only string before rendering it (seen on Django 6.1, where `display_for_value` calls `strip()`). On such a site the outer whitespace never reaches the HTML, so a stored `5\n` reads as `5`. Older versions render it in full. If exact outer whitespace matters, check what your site sends.
|
|
121
|
+
|
|
122
|
+
The other cases:
|
|
123
|
+
|
|
124
|
+
- The yes / no / unknown icons become `True`, `False` and `None`.
|
|
125
|
+
- A cell made only of links (a foreign key with a link, for example) becomes `{"text": ..., "links": [...]}`.
|
|
126
|
+
- A cell showing just `-` is Django's placeholder for an empty value and becomes `""`.
|
|
127
|
+
|
|
128
|
+
What cannot be recovered: anything the admin formats before display. Dates come back as displayed text (see `parse_admin_datetime`), numbers may be localized, foreign keys show the object's `str()`, and a read-only method that returns HTML through `format_html` or `mark_safe` comes back with that markup. A stored value that is literally `-` is indistinguishable from an empty one.
|
|
129
|
+
|
|
130
|
+
## Limitations
|
|
131
|
+
|
|
132
|
+
- **It relies on stock admin markup.** The parsers look for the classes and ids Django's own templates produce (`form-row`, `field-<name>`, `readonly`, `inline-group`, `result_list`, `paginator`, `login-form`). Custom admin themes and replacement admin skins change that markup and may break parsing partly or completely. Check against your site before relying on it. The offline tests use markup saved from an older Django; a manual run against Django 6.1 (login, change list pages and filters, read, save, inline add and delete) also passed.
|
|
133
|
+
- **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
|
+
- **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
|
+
- **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
|
+
- No file uploads. File inputs are left out of the POST, which keeps the existing file.
|
|
137
|
+
- 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
|
+
- Change list parsing returns ids, the count and the pages, not the column values. Read each object for its data.
|
|
139
|
+
- The total count and the dates assume an English-language admin with default formats.
|
|
140
|
+
- Primary keys in `AdminForm.object_id()`, `row_link_id()` and inline `_links` are assumed to be integers.
|
|
141
|
+
- Admin actions (the dropdown above the change list) and the delete confirmation page are not wrapped. `client.get` and `client.post` are there if you need them.
|
|
142
|
+
|
|
143
|
+
## Prior art
|
|
144
|
+
|
|
145
|
+
[`django-admin-client`](https://pypi.org/project/django-admin-client/) had the same idea first: a Python client for a Django admin site that works over HTTP. Its last release on PyPI is 0.2.4 from March 2019. It logs in with a username and password, offers list, get, add, change and delete, and can generate a client spec for a site. If that covers your case, look at it first.
|
|
146
|
+
|
|
147
|
+
`django-admin-remote` was written for things its documentation does not mention:
|
|
148
|
+
|
|
149
|
+
- inline formsets (read, add and delete rows),
|
|
150
|
+
- read-only fields with the stored value recovered,
|
|
151
|
+
- change list filters and pagination across pages, on both old and new Django page numbering,
|
|
152
|
+
- sessions from browser cookies, for admin sites behind SSO,
|
|
153
|
+
- custom submit buttons, and built-in request pacing.
|
|
154
|
+
|
|
155
|
+
On the other side, this package has no delete helper and no spec generation.
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
python -m pip install -e ".[test]"
|
|
161
|
+
python -m pytest -q
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The tests run offline against saved admin HTML and a fake HTTP transport.
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django-admin-remote"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Read and edit a remote Django admin site over HTTP by parsing its stock HTML. Nothing to install on the server."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Krushn Dayshmookh" }]
|
|
14
|
+
keywords = ["django", "admin", "client", "scraping", "automation", "inlines", "formset"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Framework :: Django",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
28
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
29
|
+
"Typing :: Typed",
|
|
30
|
+
]
|
|
31
|
+
dependencies = [
|
|
32
|
+
"requests>=2.25",
|
|
33
|
+
"beautifulsoup4>=4.9",
|
|
34
|
+
"tzdata; sys_platform == 'win32'",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.optional-dependencies]
|
|
38
|
+
test = ["pytest>=7"]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Repository = "https://github.com/krushndayshmookh/django-admin-remote"
|
|
42
|
+
Issues = "https://github.com/krushndayshmookh/django-admin-remote/issues"
|
|
43
|
+
Changelog = "https://github.com/krushndayshmookh/django-admin-remote/blob/main/CHANGELOG.md"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/django_admin_remote"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.sdist]
|
|
49
|
+
include = ["/src", "/tests", "/README.md", "/CHANGELOG.md", "/LICENSE", "/pyproject.toml"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Drive a remote Django admin site over HTTP by parsing its stock HTML.
|
|
2
|
+
|
|
3
|
+
Nothing is installed on the server. See the README for a quickstart.
|
|
4
|
+
"""
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
from .client import AdminClient
|
|
8
|
+
from .dates import parse_admin_datetime
|
|
9
|
+
from .exceptions import AdminError, AdminHTTPError, FormError, NotLoggedIn
|
|
10
|
+
from .form import AdminForm
|
|
11
|
+
from .parse import field_value, parse_change_form, parse_changelist
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"AdminClient",
|
|
15
|
+
"AdminForm",
|
|
16
|
+
"AdminError",
|
|
17
|
+
"AdminHTTPError",
|
|
18
|
+
"FormError",
|
|
19
|
+
"NotLoggedIn",
|
|
20
|
+
"field_value",
|
|
21
|
+
"parse_admin_datetime",
|
|
22
|
+
"parse_change_form",
|
|
23
|
+
"parse_changelist",
|
|
24
|
+
"__version__",
|
|
25
|
+
]
|