django-admin-helpers 2.0.0__tar.gz → 2.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_helpers-2.0.0/src/django_admin_helpers.egg-info → django_admin_helpers-2.2.0}/PKG-INFO +2 -1
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/pyproject.toml +11 -4
- django_admin_helpers-2.2.0/src/django_admin_helpers/__init__.py +1 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers/links.py +3 -2
- django_admin_helpers-2.2.0/src/django_admin_helpers/mixins.py +57 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0/src/django_admin_helpers.egg-info}/PKG-INFO +2 -1
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers.egg-info/SOURCES.txt +3 -1
- django_admin_helpers-2.2.0/tests/test_mixins.py +108 -0
- django_admin_helpers-2.0.0/src/django_admin_helpers/__init__.py +0 -1
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/LICENSE +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/README.md +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/setup.cfg +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers/apps.py +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers/conf.py +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers/py.typed +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers.egg-info/dependency_links.txt +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers.egg-info/requires.txt +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/src/django_admin_helpers.egg-info/top_level.txt +0 -0
- {django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0}/tests/test_links.py +0 -0
{django_admin_helpers-2.0.0/src/django_admin_helpers.egg-info → django_admin_helpers-2.2.0}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: django-admin-helpers
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: A collections of helpers for the Django admin.
|
|
5
5
|
Author-email: Bruno Alla <oss@browniebroke.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -22,6 +22,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
22
22
|
Classifier: Programming Language :: Python :: 3.12
|
|
23
23
|
Classifier: Programming Language :: Python :: 3.13
|
|
24
24
|
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
26
|
Classifier: Topic :: Software Development :: Libraries
|
|
26
27
|
Requires-Python: >=3.10
|
|
27
28
|
Description-Content-Type: text/markdown
|
|
@@ -4,7 +4,7 @@ requires = [ "setuptools>=77.0.3" ]
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "django-admin-helpers"
|
|
7
|
-
version = "2.
|
|
7
|
+
version = "2.2.0"
|
|
8
8
|
description = "A collections of helpers for the Django admin."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -27,6 +27,7 @@ classifiers = [
|
|
|
27
27
|
"Programming Language :: Python :: 3.12",
|
|
28
28
|
"Programming Language :: Python :: 3.13",
|
|
29
29
|
"Programming Language :: Python :: 3.14",
|
|
30
|
+
"Programming Language :: Python :: 3.15",
|
|
30
31
|
"Topic :: Software Development :: Libraries",
|
|
31
32
|
]
|
|
32
33
|
dependencies = [
|
|
@@ -40,6 +41,8 @@ urls.repository = "https://github.com/browniebroke/django-admin-helpers"
|
|
|
40
41
|
[dependency-groups]
|
|
41
42
|
dev = [
|
|
42
43
|
"coverage",
|
|
44
|
+
"django-stubs>=6.0.9",
|
|
45
|
+
"mypy>=2.3",
|
|
43
46
|
"pytest>=9,<10",
|
|
44
47
|
"pytest-django>=4.5,<5",
|
|
45
48
|
]
|
|
@@ -56,9 +59,9 @@ django61 = [ "django>=6.1a1,<6.2; python_version>='3.12'" ]
|
|
|
56
59
|
[tool.uv]
|
|
57
60
|
conflicts = [
|
|
58
61
|
[
|
|
59
|
-
{ group = "django61" },
|
|
60
|
-
{ group = "django60" },
|
|
61
62
|
{ group = "django52" },
|
|
63
|
+
{ group = "django60" },
|
|
64
|
+
{ group = "django61" },
|
|
62
65
|
],
|
|
63
66
|
]
|
|
64
67
|
|
|
@@ -98,7 +101,7 @@ lint.per-file-ignores."tests/**/*" = [
|
|
|
98
101
|
lint.isort.known-first-party = [ "django_admin_helpers", "tests" ]
|
|
99
102
|
|
|
100
103
|
[tool.pyproject-fmt]
|
|
101
|
-
max_supported_python = "3.
|
|
104
|
+
max_supported_python = "3.15"
|
|
102
105
|
|
|
103
106
|
[tool.mypy]
|
|
104
107
|
mypy_path = "src/"
|
|
@@ -112,10 +115,14 @@ disallow_incomplete_defs = true
|
|
|
112
115
|
check_untyped_defs = true
|
|
113
116
|
warn_unused_ignores = true
|
|
114
117
|
warn_unreachable = true
|
|
118
|
+
plugins = [ "mypy_django_plugin.main" ]
|
|
115
119
|
overrides = [ { module = "tests.*", allow_untyped_defs = true }, { module = "docs.*", ignore_errors = true } ]
|
|
116
120
|
no_implicit_optional = true
|
|
117
121
|
show_error_codes = true
|
|
118
122
|
|
|
123
|
+
[tool.django-stubs]
|
|
124
|
+
django_settings_module = "tests.settings"
|
|
125
|
+
|
|
119
126
|
[tool.pytest]
|
|
120
127
|
addopts = [ "-v", "-Wonce" ]
|
|
121
128
|
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.2.0"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import TYPE_CHECKING
|
|
3
|
+
from typing import TYPE_CHECKING, cast
|
|
4
4
|
|
|
5
5
|
from django.db.models import Model
|
|
6
6
|
from django.urls import reverse
|
|
@@ -19,7 +19,8 @@ if TYPE_CHECKING:
|
|
|
19
19
|
|
|
20
20
|
def get_app_model_names(model: Model | type[Model]) -> tuple[str, str]:
|
|
21
21
|
"""Get app label and model name from private meta attribute."""
|
|
22
|
-
|
|
22
|
+
# model_name is only None for classes not going through ModelBase.__new__
|
|
23
|
+
return model._meta.app_label, cast("str", model._meta.model_name)
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
def admin_list_url(model: type[Model] | Model) -> str:
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import ClassVar
|
|
5
|
+
|
|
6
|
+
from django.db.models import Model, QuerySet
|
|
7
|
+
from django.http import HttpRequest
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class FilesDeleteMixin:
|
|
13
|
+
"""
|
|
14
|
+
Mixin for ``ModelAdmin`` classes to delete files along with model instances.
|
|
15
|
+
|
|
16
|
+
By default, Django leaves the files of ``FileField`` (and ``ImageField``)
|
|
17
|
+
in storage when the model instances referencing them are deleted. This
|
|
18
|
+
mixin hooks into the admin deletion flow to also delete the files from
|
|
19
|
+
storage, both when deleting a single instance from its change page and
|
|
20
|
+
when using the bulk delete action from the change list.
|
|
21
|
+
|
|
22
|
+
Set the ``file_field_names`` attribute to the list of file field names
|
|
23
|
+
to delete (defaults to ``["file"]``)::
|
|
24
|
+
|
|
25
|
+
class DocumentAdmin(FilesDeleteMixin, admin.ModelAdmin):
|
|
26
|
+
file_field_names = ["file", "thumbnail"]
|
|
27
|
+
|
|
28
|
+
If a file cannot be deleted, the error is logged and the instance is
|
|
29
|
+
deleted from the database anyway.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
file_field_names: ClassVar[list[str]] = ["file"]
|
|
33
|
+
|
|
34
|
+
def delete_model(self, request: HttpRequest, obj: Model) -> None:
|
|
35
|
+
"""Delete the file(s) associated with the instance."""
|
|
36
|
+
self._delete_files_from_obj(obj)
|
|
37
|
+
super().delete_model(request, obj) # type: ignore[misc]
|
|
38
|
+
|
|
39
|
+
def delete_queryset(self, request: HttpRequest, queryset: QuerySet[Model]) -> None:
|
|
40
|
+
"""Delete the file(s) associated with the instances in the queryset."""
|
|
41
|
+
for obj in queryset:
|
|
42
|
+
self._delete_files_from_obj(obj)
|
|
43
|
+
super().delete_queryset(request, queryset) # type: ignore[misc]
|
|
44
|
+
|
|
45
|
+
def _delete_files_from_obj(self, obj: Model) -> None:
|
|
46
|
+
"""Delete the file(s) associated with the model instance."""
|
|
47
|
+
for field_name in self.file_field_names:
|
|
48
|
+
try:
|
|
49
|
+
getattr(obj, field_name).delete(save=False)
|
|
50
|
+
except Exception as exc:
|
|
51
|
+
logger.exception(
|
|
52
|
+
"Could not delete file on field '%s' from %s (%r) "
|
|
53
|
+
"- proceeding with deleting record from DB",
|
|
54
|
+
field_name,
|
|
55
|
+
obj,
|
|
56
|
+
exc,
|
|
57
|
+
)
|
{django_admin_helpers-2.0.0 → django_admin_helpers-2.2.0/src/django_admin_helpers.egg-info}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: django-admin-helpers
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: A collections of helpers for the Django admin.
|
|
5
5
|
Author-email: Bruno Alla <oss@browniebroke.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -22,6 +22,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
22
22
|
Classifier: Programming Language :: Python :: 3.12
|
|
23
23
|
Classifier: Programming Language :: Python :: 3.13
|
|
24
24
|
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
26
|
Classifier: Topic :: Software Development :: Libraries
|
|
26
27
|
Requires-Python: >=3.10
|
|
27
28
|
Description-Content-Type: text/markdown
|
|
@@ -5,10 +5,12 @@ src/django_admin_helpers/__init__.py
|
|
|
5
5
|
src/django_admin_helpers/apps.py
|
|
6
6
|
src/django_admin_helpers/conf.py
|
|
7
7
|
src/django_admin_helpers/links.py
|
|
8
|
+
src/django_admin_helpers/mixins.py
|
|
8
9
|
src/django_admin_helpers/py.typed
|
|
9
10
|
src/django_admin_helpers.egg-info/PKG-INFO
|
|
10
11
|
src/django_admin_helpers.egg-info/SOURCES.txt
|
|
11
12
|
src/django_admin_helpers.egg-info/dependency_links.txt
|
|
12
13
|
src/django_admin_helpers.egg-info/requires.txt
|
|
13
14
|
src/django_admin_helpers.egg-info/top_level.txt
|
|
14
|
-
tests/test_links.py
|
|
15
|
+
tests/test_links.py
|
|
16
|
+
tests/test_mixins.py
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import ClassVar
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from django.contrib import admin
|
|
6
|
+
from django.core.files.base import ContentFile
|
|
7
|
+
|
|
8
|
+
from django_admin_helpers.mixins import FilesDeleteMixin
|
|
9
|
+
|
|
10
|
+
from .testapp.models import Author, Blog
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AuthorAdmin(FilesDeleteMixin, admin.ModelAdmin):
|
|
14
|
+
file_field_names: ClassVar[list[str]] = ["picture", "attachment"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class BlogAdmin(FilesDeleteMixin, admin.ModelAdmin):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@pytest.fixture
|
|
22
|
+
def author_admin():
|
|
23
|
+
return AuthorAdmin(Author, admin.AdminSite())
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def make_author(suffix="", with_attachment=False):
|
|
27
|
+
author = Author(full_name=f"Author {suffix}")
|
|
28
|
+
author.picture.save(f"picture{suffix}.png", ContentFile(b"fake image"), save=False)
|
|
29
|
+
if with_attachment:
|
|
30
|
+
author.attachment.save(
|
|
31
|
+
f"attachment{suffix}.txt", ContentFile(b"fake file"), save=False
|
|
32
|
+
)
|
|
33
|
+
author.save()
|
|
34
|
+
return author
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@pytest.mark.django_db
|
|
38
|
+
def test_delete_model_deletes_file(rf, author_admin):
|
|
39
|
+
author = make_author()
|
|
40
|
+
storage = author.picture.storage
|
|
41
|
+
picture_name = author.picture.name
|
|
42
|
+
assert storage.exists(picture_name)
|
|
43
|
+
|
|
44
|
+
author_admin.delete_model(rf.post("/"), author)
|
|
45
|
+
|
|
46
|
+
assert not storage.exists(picture_name)
|
|
47
|
+
assert not Author.objects.exists()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@pytest.mark.django_db
|
|
51
|
+
def test_delete_model_deletes_all_listed_files(rf, author_admin):
|
|
52
|
+
author = make_author(with_attachment=True)
|
|
53
|
+
storage = author.picture.storage
|
|
54
|
+
file_names = [author.picture.name, author.attachment.name]
|
|
55
|
+
|
|
56
|
+
author_admin.delete_model(rf.post("/"), author)
|
|
57
|
+
|
|
58
|
+
assert not any(storage.exists(name) for name in file_names)
|
|
59
|
+
assert not Author.objects.exists()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@pytest.mark.django_db
|
|
63
|
+
def test_delete_queryset_deletes_files(rf, author_admin):
|
|
64
|
+
authors = [make_author(suffix=str(i)) for i in range(3)]
|
|
65
|
+
storage = authors[0].picture.storage
|
|
66
|
+
picture_names = [author.picture.name for author in authors]
|
|
67
|
+
|
|
68
|
+
author_admin.delete_queryset(rf.post("/"), Author.objects.all())
|
|
69
|
+
|
|
70
|
+
assert not any(storage.exists(name) for name in picture_names)
|
|
71
|
+
assert not Author.objects.exists()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@pytest.mark.django_db
|
|
75
|
+
def test_delete_model_without_file_still_deletes_record(rf, author_admin, caplog):
|
|
76
|
+
author = make_author() # no attachment
|
|
77
|
+
|
|
78
|
+
with caplog.at_level(logging.ERROR):
|
|
79
|
+
author_admin.delete_model(rf.post("/"), author)
|
|
80
|
+
|
|
81
|
+
assert not Author.objects.exists()
|
|
82
|
+
assert caplog.text == ""
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.mark.django_db
|
|
86
|
+
def test_delete_error_is_logged_and_record_deleted(rf, caplog):
|
|
87
|
+
blog = Blog.objects.create(name="Blog", description="A blog")
|
|
88
|
+
blog_admin = BlogAdmin(Blog, admin.AdminSite())
|
|
89
|
+
|
|
90
|
+
# default file_field_names is ["file"], which doesn't exist on Blog
|
|
91
|
+
with caplog.at_level(logging.ERROR):
|
|
92
|
+
blog_admin.delete_model(rf.post("/"), blog)
|
|
93
|
+
|
|
94
|
+
assert not Blog.objects.exists()
|
|
95
|
+
assert "Could not delete file on field 'file'" in caplog.text
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@pytest.mark.django_db
|
|
99
|
+
def test_delete_queryset_error_is_logged_and_records_deleted(rf, caplog):
|
|
100
|
+
for i in range(2):
|
|
101
|
+
Blog.objects.create(name=f"Blog {i}", description="A blog")
|
|
102
|
+
blog_admin = BlogAdmin(Blog, admin.AdminSite())
|
|
103
|
+
|
|
104
|
+
with caplog.at_level(logging.ERROR):
|
|
105
|
+
blog_admin.delete_queryset(rf.post("/"), Blog.objects.all())
|
|
106
|
+
|
|
107
|
+
assert not Blog.objects.exists()
|
|
108
|
+
assert caplog.text.count("Could not delete file on field 'file'") == 2
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "2.0.0"
|
|
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
|