django-admin-filemanager 1.0.0__py3-none-any.whl
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_filemanager-1.0.0.dist-info/METADATA +169 -0
- django_admin_filemanager-1.0.0.dist-info/RECORD +34 -0
- django_admin_filemanager-1.0.0.dist-info/WHEEL +5 -0
- django_admin_filemanager-1.0.0.dist-info/licenses/LICENSE +21 -0
- django_admin_filemanager-1.0.0.dist-info/top_level.txt +1 -0
- filemanager/__init__.py +0 -0
- filemanager/admin.py +254 -0
- filemanager/apps.py +8 -0
- filemanager/locale/en/LC_MESSAGES/django.mo +0 -0
- filemanager/locale/en/LC_MESSAGES/django.po +288 -0
- filemanager/locale/ru/LC_MESSAGES/django.mo +0 -0
- filemanager/locale/ru/LC_MESSAGES/django.po +289 -0
- filemanager/migrations/0001_initial.py +32 -0
- filemanager/migrations/__init__.py +0 -0
- filemanager/models.py +13 -0
- filemanager/static/filemanager/browse.js +85 -0
- filemanager/static/filemanager/html_textarea.js +100 -0
- filemanager/storage.py +220 -0
- filemanager/templates/admin/filemanager/base.html +44 -0
- filemanager/templates/admin/filemanager/browse.html +110 -0
- filemanager/templates/admin/filemanager/delete.html +35 -0
- filemanager/templates/admin/filemanager/path.html +1 -0
- filemanager/templates/admin/filemanager/rename.html +25 -0
- filemanager/templates/admin/filemanager/usage.html +11 -0
- filemanager/templates/admin/filemanager/usage_list.html +16 -0
- filemanager/templates/filemanager/widgets/html_textarea.html +3 -0
- filemanager/tests/__init__.py +9 -0
- filemanager/tests/settings.py +52 -0
- filemanager/tests/test_admin.py +227 -0
- filemanager/tests/test_storage.py +115 -0
- filemanager/tests/test_widgets.py +30 -0
- filemanager/tests/urls.py +6 -0
- filemanager/usage.py +77 -0
- filemanager/widgets.py +27 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-admin-filemanager
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: File manager in the Django admin for the files that HTML text links to
|
|
5
|
+
Author-email: Andrey Novikov <novikov@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/andreynovikov/django-admin-filemanager
|
|
8
|
+
Project-URL: Repository, https://github.com/andreynovikov/django-admin-filemanager.git
|
|
9
|
+
Project-URL: Issues, https://github.com/andreynovikov/django-admin-filemanager/issues
|
|
10
|
+
Project-URL: Releases, https://github.com/andreynovikov/django-admin-filemanager/releases
|
|
11
|
+
Keywords: django,admin,file manager,file browser,media,upload,widget
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Environment :: Web Environment
|
|
14
|
+
Classifier: Framework :: Django
|
|
15
|
+
Classifier: Framework :: Django :: 5.2
|
|
16
|
+
Classifier: Framework :: Django :: 6.0
|
|
17
|
+
Classifier: Framework :: Django :: 6.1
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Classifier: Natural Language :: English
|
|
20
|
+
Classifier: Natural Language :: Russian
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Classifier: Programming Language :: Python
|
|
23
|
+
Classifier: Programming Language :: Python :: 3
|
|
24
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
31
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Site Management
|
|
32
|
+
Requires-Python: >=3.10
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
License-File: LICENSE
|
|
35
|
+
Requires-Dist: Django>=5.2
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# File manager in the Django admin
|
|
39
|
+
|
|
40
|
+
[](https://github.com/andreynovikov/django-admin-filemanager/actions/workflows/test.yaml)
|
|
41
|
+
[](https://github.com/andreynovikov/django-admin-filemanager/releases/latest)
|
|
42
|
+
[](https://pypi.org/project/django-admin-filemanager/)
|
|
43
|
+
[](https://pypi.org/project/django-admin-filemanager/)
|
|
44
|
+
[](https://pypi.org/project/django-admin-filemanager/)
|
|
45
|
+
[](https://github.com/andreynovikov/django-admin-filemanager/issues)
|
|
46
|
+
[](LICENSE)
|
|
47
|
+
|
|
48
|
+
A file browser in the Django admin for the files that pages link to by URL: images, videos and documents in HTML text. It works like a file system. Folders nest freely, files keep the names they are uploaded with, and replacing a file keeps its URL. Files have no versions.
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
- **Browsing.** A folder at a time, with its folders and files, sizes and modification times. Hidden names (with a leading dot) and names that aren't UTF-8 are not shown. Files can be sorted by name, size or modification time, and the order stays while you move between folders.
|
|
53
|
+
- **Translations of labels.** Short labels such as "Delete" have the context `file manager`, so that the catalogs of apps installed earlier, which take precedence, don't replace them.
|
|
54
|
+
- **Uploading.** Several files at a time, by dropping them on the page or choosing them. A file whose name exists is replaced only after the browser asks. The new content is written beside the old file and renamed over it, so its URL never serves a partial file.
|
|
55
|
+
- **Folders.** Making, renaming, moving and deleting folders. Deleting a folder deletes everything in it, after a confirmation that says how many files it holds.
|
|
56
|
+
- **Renaming and moving** files and folders. The new path is typed from the root, and missing folders are made.
|
|
57
|
+
- **Where used.** Lists the records that link to a file or to the files of a folder. The rename and delete pages show it before anything changes. See [Where used](#where-used).
|
|
58
|
+
- **Protected folders.** Folders that hold the files of model fields (`FileField`, `ImageField`) can be browsed and linked from, but not changed.
|
|
59
|
+
- **HTML widget.** A text area for HTML fields that grows with its text, with an "Insert file" button that opens the browser in a popup. The chosen file goes into the text at the cursor: `<img>` for images, `<video controls>` for videos and `<a>` for other files.
|
|
60
|
+
- **Permissions:** the model permissions of `filemanager.File`:
|
|
61
|
+
- view: browse, copy URLs, see where a file is used;
|
|
62
|
+
- add: upload new files and make folders;
|
|
63
|
+
- change: replace, rename and move;
|
|
64
|
+
- delete: delete.
|
|
65
|
+
- **Translations:** English and Russian.
|
|
66
|
+
|
|
67
|
+
## Requirements
|
|
68
|
+
|
|
69
|
+
- Python 3.10 or later.
|
|
70
|
+
- Django 5.2 or later; 5.2, 6.0 and 6.1 are tested.
|
|
71
|
+
- A `FileSystemStorage`. Another storage, such as S3, needs a store class in `storage.py`, see [Other storages](#other-storages).
|
|
72
|
+
|
|
73
|
+
## Installation
|
|
74
|
+
|
|
75
|
+
1. Install the package. It is named `django-admin-filemanager`, and its app is `filemanager`.
|
|
76
|
+
|
|
77
|
+
```shell
|
|
78
|
+
pip install django-admin-filemanager
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
2. Add `'filemanager'` to `INSTALLED_APPS`. Its templates and scripts are found by the default template loaders and static file finders.
|
|
82
|
+
3. Run `python manage.py migrate filemanager`. The model has no table, but the migration creates its permissions.
|
|
83
|
+
4. Give the `HTMLTextarea` widget to the fields that hold HTML, see [The HTML widget](#the-html-widget).
|
|
84
|
+
|
|
85
|
+
The browser is at the changelist URL of `filemanager.File`, `admin:filemanager_file_changelist`.
|
|
86
|
+
|
|
87
|
+
## The HTML widget
|
|
88
|
+
|
|
89
|
+
`filemanager.widgets.HTMLTextarea` is an admin text area for HTML. It grows with its text, also in collapsed fieldsets and in inline rows added later. A `max-height` in its style still applies, and the text area scrolls beyond it. Its "Insert file" button opens the browser in a popup.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from filemanager.widgets import HTMLTextarea
|
|
93
|
+
|
|
94
|
+
class PageAdminForm(forms.ModelForm):
|
|
95
|
+
class Meta:
|
|
96
|
+
widgets = {
|
|
97
|
+
'content': HTMLTextarea(attrs={'rows': 10, 'style': 'width: 95%; max-height: 500px'}),
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`admin_site` names the admin site whose browser the button opens, `'admin'` by default. The button is left out where that site's URLs aren't installed. The widget doesn't know the user, so users without file permissions see the button too. The browser then refuses them.
|
|
102
|
+
|
|
103
|
+
## Settings
|
|
104
|
+
|
|
105
|
+
| Setting | Default | |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| `FILEMANAGER_STORAGE` | `'default'` | The alias in `STORAGES` of the storage whose files are managed. |
|
|
108
|
+
| `FILEMANAGER_PROTECTED` | `[]` | Paths from the root, such as `'products'`, that are read-only together with everything below them. List the `upload_to` folders of your file fields. |
|
|
109
|
+
| `FILEMANAGER_USAGE_EXCLUDE` | `[]` | App labels (`'django_celery_results'`) or models (`'shop.Order'`) that "where used" doesn't search. |
|
|
110
|
+
|
|
111
|
+
## Where used
|
|
112
|
+
|
|
113
|
+
The text fields (`TextField`) of every model registered in the admin site are searched for `/` followed by the path, as stored and as it appears in a URL (`a b.jpg` and `a%20b.jpg`). That finds `/media/images/a.jpg`, `https://example.com/media/images/a.jpg` and aliases that keep the end of the path, such as `/images/a.jpg`. A short path at the root can also match a longer one: `/a.jpg` matches `/images/a.jpg`. Each search scans the tables, which is fine for pages opened one at a time. Exclude big tables that hold no links.
|
|
114
|
+
|
|
115
|
+
Rename and move don't change the links. The rename page lists the records to fix.
|
|
116
|
+
|
|
117
|
+
## Caching
|
|
118
|
+
|
|
119
|
+
Replacing a file keeps its URL, so the URL mustn't be cached for long. Serve the files with a short `max-age`, and with `ETag` or `Last-Modified` so that clients can revalidate. An image cache in front of the files, such as Next.js image optimization, keeps old copies for its own TTL.
|
|
120
|
+
|
|
121
|
+
## Other storages
|
|
122
|
+
|
|
123
|
+
The views use a store returned by `storage.get_store()`, and `FileSystemStore` is the only one so far. A store for an object storage implements the same methods. Folders are key prefixes there, so renaming one means copying and deleting each object under it, and an empty folder needs a placeholder object. Its `save()` puts the object under its final key, which replaces it in one step.
|
|
124
|
+
|
|
125
|
+
## Development
|
|
126
|
+
|
|
127
|
+
### Tests
|
|
128
|
+
|
|
129
|
+
In a project that has the app installed:
|
|
130
|
+
|
|
131
|
+
```shell
|
|
132
|
+
python manage.py test filemanager
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
On all supported Python and Django versions, with [tox](https://tox.wiki/):
|
|
136
|
+
|
|
137
|
+
```shell
|
|
138
|
+
tox
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Or once, without a project, on SQLite, in the repository's root:
|
|
142
|
+
|
|
143
|
+
```shell
|
|
144
|
+
python -m django test filemanager --settings=filemanager.tests.settings
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Translations
|
|
148
|
+
|
|
149
|
+
The source strings are English, and `filemanager/locale/` holds the English and Russian catalogs. To update them after changing strings, run this in the `filemanager` directory (it needs GNU gettext):
|
|
150
|
+
|
|
151
|
+
```shell
|
|
152
|
+
django-admin makemessages -l en -l ru
|
|
153
|
+
django-admin compilemessages
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Releases
|
|
157
|
+
|
|
158
|
+
Set the new version in `pyproject.toml`, commit, then tag the commit and push the tag:
|
|
159
|
+
|
|
160
|
+
```shell
|
|
161
|
+
git tag v1.0.1
|
|
162
|
+
git push origin v1.0.1
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The `publish` workflow tests the tag, publishes it to TestPyPI and PyPI and creates a GitHub release with notes generated from the commits.
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
django_admin_filemanager-1.0.0.dist-info/licenses/LICENSE,sha256=mJkmFxJEhjbo24lRLHcHR_HHzRDZhsUDRqSDyGeFojU,1071
|
|
2
|
+
filemanager/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
filemanager/admin.py,sha256=kNKgz_zFXRhYhDRLdYy34a41NI6k2UXZQpuKc3ueD_g,11312
|
|
4
|
+
filemanager/apps.py,sha256=p93yKbf-ICMopLYPLLYuYwN5WGU1CZMHZbXq-u6IcVY,239
|
|
5
|
+
filemanager/models.py,sha256=rX02LAdLcd4v2KqyQAzg3TMZNKmoOMGIk5mRf8fwBi0,464
|
|
6
|
+
filemanager/storage.py,sha256=fgix2aiu1e87LCL2CnODPsphIGp3VKlZoKd1nHD9Kuk,8465
|
|
7
|
+
filemanager/usage.py,sha256=pKkYYJcm8RDkMA0V4KkM7hg41OLZsDS4RCePf3C_LuY,3028
|
|
8
|
+
filemanager/widgets.py,sha256=ESKFx-2Zi09-QZNK6DXIFz7aL1CkZ2f8h7jCjiwZOgA,1065
|
|
9
|
+
filemanager/locale/en/LC_MESSAGES/django.mo,sha256=dDtWm3W8mz50OcIBkyWQuQs4wJdgNYemjVhaI1i9sWo,4949
|
|
10
|
+
filemanager/locale/en/LC_MESSAGES/django.po,sha256=Eza52ZFZoN2f2m6brFvTj4wWGFj9AmBa5v7YaKOpwMA,7497
|
|
11
|
+
filemanager/locale/ru/LC_MESSAGES/django.mo,sha256=a4Hl1-J4EOceqbJwVXg8C8DOXNYNlgnTbRw1HO-Bh2w,6221
|
|
12
|
+
filemanager/locale/ru/LC_MESSAGES/django.po,sha256=tFOFnIcXkLZs12FSOmvm0_dGRL3J10HnVeIUNhZf3aw,8790
|
|
13
|
+
filemanager/migrations/0001_initial.py,sha256=FVJI3DN9L8hxZxV0Wrg593DSH-Ose1JCtubJGKD3g-s,759
|
|
14
|
+
filemanager/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
15
|
+
filemanager/static/filemanager/browse.js,sha256=DYWXYGJm0VDdOzmIyfqnOvB4Ic9IIayO-ho10J4l4nE,3360
|
|
16
|
+
filemanager/static/filemanager/html_textarea.js,sha256=GMtD_jVaOBfRdAQevGziFW4Wg-9p08vhx2CLW5IJCig,3759
|
|
17
|
+
filemanager/templates/admin/filemanager/base.html,sha256=qrbT0W4gaoK3FWEzsKUAsA0DcTX1vftV8Blk_22jqt4,2028
|
|
18
|
+
filemanager/templates/admin/filemanager/browse.html,sha256=Z9_51nssNedwTDJP3GQfZUsep_xLzTJXFzflb3Gb4gc,6273
|
|
19
|
+
filemanager/templates/admin/filemanager/delete.html,sha256=dDiDTdzKPusiRSHiEIBkDSYtr5hw3d8yaj-gzrkLNbs,1352
|
|
20
|
+
filemanager/templates/admin/filemanager/path.html,sha256=N-n-qnwB-mq4pYZUN_oYzGynAl3ekmCLN3XDHQXJMoM,225
|
|
21
|
+
filemanager/templates/admin/filemanager/rename.html,sha256=U9HVdDEBPLj0870oz8sM3gTPH-4nNI4WkZOCFjfj_Uk,1307
|
|
22
|
+
filemanager/templates/admin/filemanager/usage.html,sha256=K_7znHZUGBpDruiCNuYGfrTQnW5bMAbSC2Ubc0Z2sis,334
|
|
23
|
+
filemanager/templates/admin/filemanager/usage_list.html,sha256=BbTrkCtmvWe537_SPXi-16c4cp-B1US1-vOre6Ys1pI,826
|
|
24
|
+
filemanager/templates/filemanager/widgets/html_textarea.html,sha256=XI-y43_r-Swq0mInat2dgOXGj41Vz7egD88vqt6fWBg,458
|
|
25
|
+
filemanager/tests/__init__.py,sha256=XF-F3zeE8KUuT8djEBVOQA-pUVpsbQx2FNTGJs22jK4,276
|
|
26
|
+
filemanager/tests/settings.py,sha256=8SWCpt1PoRuO2ZKE6BC0a7xcCQiHsJ5SqWu2klwxmeI,1312
|
|
27
|
+
filemanager/tests/test_admin.py,sha256=VZ1oXl4VWC5UqzCQTfGL4zaI2lde0B4A5hmXLGVqHCs,12963
|
|
28
|
+
filemanager/tests/test_storage.py,sha256=mkPz7L84ilj6eYqDROKj08pbC-pdqxAk7zLMrMHUDFY,5767
|
|
29
|
+
filemanager/tests/test_widgets.py,sha256=CNgdeBUwHG4ricHag2KbiXwUsymozwi-tjEiBdSbyII,1290
|
|
30
|
+
filemanager/tests/urls.py,sha256=0k8MHFs6BLpMz8tGM6IA3he2oWZzUmmA9E2XPchodaE,118
|
|
31
|
+
django_admin_filemanager-1.0.0.dist-info/METADATA,sha256=oUKHy11gHYYSaEh4PuzF9hDo7Up-dEiedV5lds-OBOE,9440
|
|
32
|
+
django_admin_filemanager-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
33
|
+
django_admin_filemanager-1.0.0.dist-info/top_level.txt,sha256=QMC_l_eynOtUW2DOmLn8x4y-0NTLV8LUhK5gH1fvwfU,12
|
|
34
|
+
django_admin_filemanager-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrey Novikov
|
|
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 @@
|
|
|
1
|
+
filemanager
|
filemanager/__init__.py
ADDED
|
File without changes
|
filemanager/admin.py
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import posixpath
|
|
2
|
+
import re
|
|
3
|
+
from urllib.parse import urlencode
|
|
4
|
+
|
|
5
|
+
from django.contrib import admin, messages
|
|
6
|
+
from django.core.exceptions import PermissionDenied
|
|
7
|
+
from django.http import Http404, HttpResponseNotAllowed, HttpResponseRedirect
|
|
8
|
+
from django.template.response import TemplateResponse
|
|
9
|
+
from django.urls import path as url_path, reverse
|
|
10
|
+
from django.utils.translation import gettext as _, ngettext, pgettext
|
|
11
|
+
|
|
12
|
+
from .models import File
|
|
13
|
+
from .storage import FileExists, FileManagerError, clean_path, get_store, join
|
|
14
|
+
from .usage import find_usage
|
|
15
|
+
|
|
16
|
+
POPUP_VAR = '_popup'
|
|
17
|
+
TARGET_VAR = '_target'
|
|
18
|
+
TARGET_RE = re.compile(r'^[\w-]{1,200}$')
|
|
19
|
+
ORDER_VAR = 'o'
|
|
20
|
+
# Columns the files can be ordered by: the key, and whether the first click orders them descending
|
|
21
|
+
ORDERINGS = {
|
|
22
|
+
'name': (lambda entry: entry.name.casefold(), False),
|
|
23
|
+
'size': (lambda entry: entry.size, True),
|
|
24
|
+
'modified': (lambda entry: entry.modified, True),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@admin.register(File)
|
|
29
|
+
class FileAdmin(admin.ModelAdmin):
|
|
30
|
+
"""
|
|
31
|
+
The file browser. It takes the place of the change list, and the add and change forms are not used.
|
|
32
|
+
"""
|
|
33
|
+
def get_model_perms(self, request):
|
|
34
|
+
perms = super().get_model_perms(request)
|
|
35
|
+
perms['add'] = False # files are added in the browser, there is no add form
|
|
36
|
+
return perms
|
|
37
|
+
|
|
38
|
+
def get_urls(self):
|
|
39
|
+
info = self.opts.app_label, self.opts.model_name
|
|
40
|
+
view = self.admin_site.admin_view
|
|
41
|
+
return [
|
|
42
|
+
url_path('', view(self.browse_view), name='%s_%s_changelist' % info),
|
|
43
|
+
url_path('upload/', view(self.upload_view), name='%s_%s_upload' % info),
|
|
44
|
+
url_path('mkdir/', view(self.mkdir_view), name='%s_%s_mkdir' % info),
|
|
45
|
+
url_path('rename/', view(self.rename_view), name='%s_%s_rename' % info),
|
|
46
|
+
url_path('delete/', view(self.delete_file_view), name='%s_%s_delete' % info),
|
|
47
|
+
url_path('usage/', view(self.usage_view), name='%s_%s_usage' % info),
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
# Requests
|
|
51
|
+
|
|
52
|
+
def keep_params(self, request):
|
|
53
|
+
"""Parameters that every page of the browser keeps: the file picker's popup and the order of files."""
|
|
54
|
+
data = request.POST if request.method == 'POST' else request.GET
|
|
55
|
+
params = {}
|
|
56
|
+
target = data.get(TARGET_VAR, '')
|
|
57
|
+
if data.get(POPUP_VAR) and TARGET_RE.match(target):
|
|
58
|
+
params.update({POPUP_VAR: '1', TARGET_VAR: target})
|
|
59
|
+
order = data.get(ORDER_VAR, '')
|
|
60
|
+
if order.lstrip('-') in ORDERINGS and order != 'name':
|
|
61
|
+
params[ORDER_VAR] = order
|
|
62
|
+
return params
|
|
63
|
+
|
|
64
|
+
def get_order(self, request):
|
|
65
|
+
"""(column, descending)"""
|
|
66
|
+
order = self.keep_params(request).get(ORDER_VAR, 'name')
|
|
67
|
+
return order.lstrip('-'), order.startswith('-')
|
|
68
|
+
|
|
69
|
+
def browse_url(self, request, folder='', **params):
|
|
70
|
+
query = {'dir': folder} if folder else {}
|
|
71
|
+
query.update(self.keep_params(request))
|
|
72
|
+
query.update(params)
|
|
73
|
+
query = {name: value for name, value in query.items() if value}
|
|
74
|
+
url = reverse('%s:%s_%s_changelist' % (self.admin_site.name, self.opts.app_label, self.opts.model_name))
|
|
75
|
+
return url + ('?' + urlencode(query) if query else '')
|
|
76
|
+
|
|
77
|
+
def columns(self, request, folder):
|
|
78
|
+
"""The sortable columns of the file list, as the admin's change list shows them"""
|
|
79
|
+
column, descending = self.get_order(request)
|
|
80
|
+
result = []
|
|
81
|
+
for name, label in (
|
|
82
|
+
('name', pgettext('file manager', 'Name')),
|
|
83
|
+
('size', pgettext('file manager', 'Size')),
|
|
84
|
+
('modified', pgettext('file manager', 'Modified')),
|
|
85
|
+
):
|
|
86
|
+
sorted_ = name == column
|
|
87
|
+
first_descending = ORDERINGS[name][1]
|
|
88
|
+
toggle = ('' if descending else '-') + name if sorted_ else ('-' if first_descending else '') + name
|
|
89
|
+
result.append({
|
|
90
|
+
'name': name,
|
|
91
|
+
'label': label,
|
|
92
|
+
'sorted': sorted_,
|
|
93
|
+
'ascending': sorted_ and not descending,
|
|
94
|
+
'url': self.browse_url(request, folder, **{ORDER_VAR: '' if toggle == 'name' else toggle}),
|
|
95
|
+
})
|
|
96
|
+
return result
|
|
97
|
+
|
|
98
|
+
def get_path(self, request, name='path'):
|
|
99
|
+
data = request.POST if request.method == 'POST' else request.GET
|
|
100
|
+
try:
|
|
101
|
+
return clean_path(data.get(name))
|
|
102
|
+
except FileManagerError:
|
|
103
|
+
raise Http404()
|
|
104
|
+
|
|
105
|
+
def context(self, request, title, folder, **extra):
|
|
106
|
+
crumbs, parts = [], folder.split('/') if folder else []
|
|
107
|
+
for index, name in enumerate(parts):
|
|
108
|
+
crumbs.append((name, self.browse_url(request, '/'.join(parts[:index + 1]))))
|
|
109
|
+
keep = self.keep_params(request)
|
|
110
|
+
return {
|
|
111
|
+
**self.admin_site.each_context(request),
|
|
112
|
+
'opts': self.opts,
|
|
113
|
+
'title': title,
|
|
114
|
+
'folder': folder,
|
|
115
|
+
'root_url': self.browse_url(request),
|
|
116
|
+
'folder_url': self.browse_url(request, folder),
|
|
117
|
+
'crumbs': crumbs,
|
|
118
|
+
'is_popup': POPUP_VAR in keep,
|
|
119
|
+
'keep_params': keep,
|
|
120
|
+
'keep_query': urlencode(keep),
|
|
121
|
+
**extra,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
def render(self, request, template, context):
|
|
125
|
+
request.current_app = self.admin_site.name
|
|
126
|
+
return TemplateResponse(request, 'admin/filemanager/%s.html' % template, context)
|
|
127
|
+
|
|
128
|
+
# Views
|
|
129
|
+
|
|
130
|
+
def browse_view(self, request):
|
|
131
|
+
if not self.has_view_or_change_permission(request):
|
|
132
|
+
raise PermissionDenied
|
|
133
|
+
store = get_store()
|
|
134
|
+
folder = self.get_path(request, 'dir')
|
|
135
|
+
if folder and store.kind(folder) != 'dir':
|
|
136
|
+
raise Http404()
|
|
137
|
+
folders, files = store.list(folder)
|
|
138
|
+
column, descending = self.get_order(request)
|
|
139
|
+
files.sort(key=ORDERINGS[column][0], reverse=descending)
|
|
140
|
+
if column == 'name' and descending:
|
|
141
|
+
folders.reverse() # folders have no size or time, and stay ordered by name otherwise
|
|
142
|
+
context = self.context(
|
|
143
|
+
request, folder or _('Files'), folder,
|
|
144
|
+
folders=[(entry, store.is_protected(entry.path)) for entry in folders],
|
|
145
|
+
files=files,
|
|
146
|
+
parent_url=self.browse_url(request, posixpath.dirname(folder)) if folder else None,
|
|
147
|
+
protected=store.is_protected(folder),
|
|
148
|
+
can_add=not store.is_protected(folder) and self.has_add_permission(request),
|
|
149
|
+
can_change=self.has_change_permission(request),
|
|
150
|
+
can_delete=self.has_delete_permission(request),
|
|
151
|
+
names=[entry.name for entry in folders + files],
|
|
152
|
+
columns=self.columns(request, folder),
|
|
153
|
+
)
|
|
154
|
+
return self.render(request, 'browse', context)
|
|
155
|
+
|
|
156
|
+
def upload_view(self, request):
|
|
157
|
+
if request.method != 'POST':
|
|
158
|
+
return HttpResponseNotAllowed(['POST'])
|
|
159
|
+
if not self.has_add_permission(request):
|
|
160
|
+
raise PermissionDenied
|
|
161
|
+
store = get_store()
|
|
162
|
+
folder = self.get_path(request, 'dir')
|
|
163
|
+
overwrite = request.POST.get('overwrite') == '1'
|
|
164
|
+
if overwrite and not self.has_change_permission(request):
|
|
165
|
+
raise PermissionDenied
|
|
166
|
+
saved = 0
|
|
167
|
+
for upload in request.FILES.getlist('files'):
|
|
168
|
+
try:
|
|
169
|
+
store.save(folder, upload.name, upload, overwrite=overwrite)
|
|
170
|
+
saved += 1
|
|
171
|
+
except FileExists as e:
|
|
172
|
+
self.message_user(request, _('%(error)s Upload it again and allow replacing.') % {'error': e}, messages.ERROR)
|
|
173
|
+
except FileManagerError as e:
|
|
174
|
+
self.message_user(request, str(e), messages.ERROR)
|
|
175
|
+
if saved:
|
|
176
|
+
self.message_user(request, ngettext('%d file uploaded.', '%d files uploaded.', saved) % saved, messages.SUCCESS)
|
|
177
|
+
return HttpResponseRedirect(self.browse_url(request, folder))
|
|
178
|
+
|
|
179
|
+
def mkdir_view(self, request):
|
|
180
|
+
if request.method != 'POST':
|
|
181
|
+
return HttpResponseNotAllowed(['POST'])
|
|
182
|
+
if not self.has_add_permission(request):
|
|
183
|
+
raise PermissionDenied
|
|
184
|
+
folder = self.get_path(request, 'dir')
|
|
185
|
+
try:
|
|
186
|
+
folder = get_store().mkdir(folder, request.POST.get('name', '').strip())
|
|
187
|
+
except FileManagerError as e:
|
|
188
|
+
self.message_user(request, str(e), messages.ERROR)
|
|
189
|
+
return HttpResponseRedirect(self.browse_url(request, folder))
|
|
190
|
+
|
|
191
|
+
def rename_view(self, request):
|
|
192
|
+
if not self.has_change_permission(request):
|
|
193
|
+
raise PermissionDenied
|
|
194
|
+
store = get_store()
|
|
195
|
+
path = self.get_path(request)
|
|
196
|
+
kind = store.kind(path) if path else None
|
|
197
|
+
if not kind:
|
|
198
|
+
raise Http404()
|
|
199
|
+
error = None
|
|
200
|
+
target = path
|
|
201
|
+
if request.method == 'POST':
|
|
202
|
+
try:
|
|
203
|
+
target = clean_path(request.POST.get('target', '').strip())
|
|
204
|
+
store.rename(path, target)
|
|
205
|
+
self.message_user(request, _('“%(path)s” is now “%(target)s”.') % {'path': path, 'target': target}, messages.SUCCESS)
|
|
206
|
+
return HttpResponseRedirect(self.browse_url(request, posixpath.dirname(target)))
|
|
207
|
+
except FileManagerError as e:
|
|
208
|
+
error = str(e)
|
|
209
|
+
target = request.POST.get('target', '')
|
|
210
|
+
context = self.context(
|
|
211
|
+
request, _('Rename or move “%(name)s”') % {'name': posixpath.basename(path)}, posixpath.dirname(path),
|
|
212
|
+
path=path, is_dir=kind == 'dir', target=target, error=error,
|
|
213
|
+
usage=find_usage(path, kind == 'dir', self.admin_site),
|
|
214
|
+
)
|
|
215
|
+
return self.render(request, 'rename', context)
|
|
216
|
+
|
|
217
|
+
def delete_file_view(self, request):
|
|
218
|
+
if not self.has_delete_permission(request):
|
|
219
|
+
raise PermissionDenied
|
|
220
|
+
store = get_store()
|
|
221
|
+
path = self.get_path(request)
|
|
222
|
+
kind = store.kind(path) if path else None
|
|
223
|
+
if not kind:
|
|
224
|
+
raise Http404()
|
|
225
|
+
folder = posixpath.dirname(path)
|
|
226
|
+
if request.method == 'POST':
|
|
227
|
+
try:
|
|
228
|
+
store.delete(path)
|
|
229
|
+
self.message_user(request, _('“%(path)s” deleted.') % {'path': path}, messages.SUCCESS)
|
|
230
|
+
except FileManagerError as e:
|
|
231
|
+
self.message_user(request, str(e), messages.ERROR)
|
|
232
|
+
return HttpResponseRedirect(self.browse_url(request, folder))
|
|
233
|
+
context = self.context(
|
|
234
|
+
request, _('Delete “%(name)s”') % {'name': posixpath.basename(path)}, folder,
|
|
235
|
+
path=path, is_dir=kind == 'dir', protected=store.is_protected(path),
|
|
236
|
+
file_count=store.count_files(path) if kind == 'dir' else None,
|
|
237
|
+
usage=find_usage(path, kind == 'dir', self.admin_site),
|
|
238
|
+
)
|
|
239
|
+
return self.render(request, 'delete', context)
|
|
240
|
+
|
|
241
|
+
def usage_view(self, request):
|
|
242
|
+
if not self.has_view_or_change_permission(request):
|
|
243
|
+
raise PermissionDenied
|
|
244
|
+
store = get_store()
|
|
245
|
+
path = self.get_path(request)
|
|
246
|
+
kind = store.kind(path) if path else None
|
|
247
|
+
if not kind:
|
|
248
|
+
raise Http404()
|
|
249
|
+
context = self.context(
|
|
250
|
+
request, _('Where “%(name)s” is used') % {'name': posixpath.basename(path)}, posixpath.dirname(path),
|
|
251
|
+
path=path, is_dir=kind == 'dir', url=store.url(path) if kind == 'file' else store.url(join(path, '')),
|
|
252
|
+
usage=find_usage(path, kind == 'dir', self.admin_site),
|
|
253
|
+
)
|
|
254
|
+
return self.render(request, 'usage', context)
|
filemanager/apps.py
ADDED
|
Binary file
|