django-image-toolkit 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_image_toolkit-0.1.0/.gitignore +11 -0
- django_image_toolkit-0.1.0/LICENSE +21 -0
- django_image_toolkit-0.1.0/PKG-INFO +16 -0
- django_image_toolkit-0.1.0/README.md +0 -0
- django_image_toolkit-0.1.0/image_toolkit/__init__.py +0 -0
- django_image_toolkit-0.1.0/image_toolkit/admin.py +32 -0
- django_image_toolkit-0.1.0/image_toolkit/apps.py +22 -0
- django_image_toolkit-0.1.0/image_toolkit/fields.py +212 -0
- django_image_toolkit-0.1.0/image_toolkit/handlers.py +27 -0
- django_image_toolkit-0.1.0/image_toolkit/static/admin_image_grid.css +177 -0
- django_image_toolkit-0.1.0/image_toolkit/static/admin_image_grid.js +39 -0
- django_image_toolkit-0.1.0/image_toolkit/storages.py +17 -0
- django_image_toolkit-0.1.0/image_toolkit/templates/image_toolkit/admin/image_grid.html +98 -0
- django_image_toolkit-0.1.0/image_toolkit/templates/image_toolkit/cam_image_input.html +168 -0
- django_image_toolkit-0.1.0/image_toolkit/templates/image_toolkit/preview_image_input.html +63 -0
- django_image_toolkit-0.1.0/image_toolkit/widgets.py +57 -0
- django_image_toolkit-0.1.0/pyproject.toml +51 -0
- django_image_toolkit-0.1.0/uv.lock +501 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eduardo Skoroboatei Gomes
|
|
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,16 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-image-toolkit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An image toolkit for media driven apps in django. Custom image fields with format conversion, cropping and validation. Hashed based filename storage. Cleanup and widgets
|
|
5
|
+
Project-URL: Homepage, https://github.com/edu292/django-image-tookit
|
|
6
|
+
Project-URL: Issues, https://github.com/edu292/django-image-toolkit/issues
|
|
7
|
+
Author-email: Eduardo Skoroboatei Gomes <eduskoroboatei@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Framework :: Django
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Requires-Python: >=3.14
|
|
15
|
+
Requires-Dist: django>=6.0.4
|
|
16
|
+
Requires-Dist: pillow>=12.2.0
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ImageGridAdminMixin(admin.ModelAdmin):
|
|
5
|
+
change_list_template = 'image_toolkit/admin/image_grid.html'
|
|
6
|
+
image_field = 'file'
|
|
7
|
+
name_field = 'name'
|
|
8
|
+
edit_page = True
|
|
9
|
+
|
|
10
|
+
def get_list_editable(self, request):
|
|
11
|
+
editable = list(super().get_list_editable(request))
|
|
12
|
+
if self.name_field and self.name_field not in editable:
|
|
13
|
+
editable.append(self.name_field)
|
|
14
|
+
return tuple(editable)
|
|
15
|
+
|
|
16
|
+
def changelist_view(self, request, extra_context=None):
|
|
17
|
+
response = super().changelist_view(request, extra_context=extra_context)
|
|
18
|
+
|
|
19
|
+
if hasattr(response, 'context_data') and 'cl' in response.context_data:
|
|
20
|
+
cl = response.context_data['cl']
|
|
21
|
+
for obj in cl.result_list:
|
|
22
|
+
file_attr = getattr(obj, self.image_field, None)
|
|
23
|
+
obj._image = file_attr
|
|
24
|
+
obj._name = getattr(obj, self.name_field, '')
|
|
25
|
+
obj._name_field = self.name_field
|
|
26
|
+
obj._abs_url = request.build_absolute_uri(file_attr.url) if file_attr else ''
|
|
27
|
+
|
|
28
|
+
return response
|
|
29
|
+
|
|
30
|
+
class Media:
|
|
31
|
+
css = {'all': ('image_toolkit/css/admin_image_grid.css',)}
|
|
32
|
+
js = ('image_toolkit/js/admin_image_grid.js',)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
from django.db.models import FileField
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ImageToolkitConfig(AppConfig):
|
|
6
|
+
name = 'image_toolkit'
|
|
7
|
+
|
|
8
|
+
def ready(self) -> None:
|
|
9
|
+
from django.apps import apps
|
|
10
|
+
|
|
11
|
+
for model in apps.get_models():
|
|
12
|
+
has_file_field = any(isinstance(field, FileField) for field in model._meta.fields)
|
|
13
|
+
if has_file_field:
|
|
14
|
+
self.connect_signal(model)
|
|
15
|
+
|
|
16
|
+
def connect_signal(self, model):
|
|
17
|
+
from django.db.models.signals import post_delete, pre_save
|
|
18
|
+
|
|
19
|
+
from .handlers import delete_old_file, delete_on_model_delete
|
|
20
|
+
|
|
21
|
+
post_delete.connect(delete_on_model_delete, sender=model, dispatch_uid=f'{model._meta.label}_cleanup_delete')
|
|
22
|
+
pre_save.connect(delete_old_file, sender=model, dispatch_uid=f'{model._meta.label}_cleanup_change')
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import math
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from django import forms
|
|
7
|
+
from django.core.exceptions import ValidationError
|
|
8
|
+
from django.core.files.base import ContentFile
|
|
9
|
+
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
10
|
+
from django.db.models import ImageField
|
|
11
|
+
from django.utils.translation import gettext_lazy as _
|
|
12
|
+
from PIL import Image
|
|
13
|
+
|
|
14
|
+
from .widgets import ResizableImageWidget
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def is_float(str: Any):
|
|
18
|
+
try:
|
|
19
|
+
float(str)
|
|
20
|
+
except TypeError:
|
|
21
|
+
return False
|
|
22
|
+
|
|
23
|
+
return True
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class WebPImageField(ImageField):
|
|
27
|
+
def __init__(self, quality: int = 80, *args: Any, **kwargs: Any) -> None:
|
|
28
|
+
if not is_float(quality):
|
|
29
|
+
raise TypeError(f'Expect quality to be a valid float. Given {quality}')
|
|
30
|
+
super().__init__(*args, **kwargs)
|
|
31
|
+
self.quality = quality
|
|
32
|
+
|
|
33
|
+
def deconstruct(self) -> Any:
|
|
34
|
+
name, path, args, kwargs = super().deconstruct()
|
|
35
|
+
kwargs['quality'] = self.quality
|
|
36
|
+
|
|
37
|
+
return name, path, args, kwargs
|
|
38
|
+
|
|
39
|
+
def process_image(self, img):
|
|
40
|
+
return img
|
|
41
|
+
|
|
42
|
+
def get_prep_value(self, value):
|
|
43
|
+
prepped_value = super().get_prep_value(value)
|
|
44
|
+
|
|
45
|
+
if prepped_value == '':
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
return prepped_value
|
|
49
|
+
|
|
50
|
+
def pre_save(self, model_instance, add):
|
|
51
|
+
file = getattr(model_instance, self.attname)
|
|
52
|
+
|
|
53
|
+
if file and not file._committed:
|
|
54
|
+
img = Image.open(file)
|
|
55
|
+
|
|
56
|
+
if img.format != 'WEBP':
|
|
57
|
+
if img.mode in ('RGBA', 'P'):
|
|
58
|
+
img = img.convert('RGBA')
|
|
59
|
+
elif img.mode != 'RGB':
|
|
60
|
+
img = img.convert('RGB')
|
|
61
|
+
|
|
62
|
+
img = self.process_image(img)
|
|
63
|
+
|
|
64
|
+
output = io.BytesIO()
|
|
65
|
+
img.save(output, format='WEBP', quality=self.quality)
|
|
66
|
+
output.seek(0)
|
|
67
|
+
|
|
68
|
+
file.file = ContentFile(output.read())
|
|
69
|
+
file.name = Path(file.name).with_suffix('.webp')
|
|
70
|
+
|
|
71
|
+
return super().pre_save(model_instance, add)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class ImageDimensionMixin:
|
|
75
|
+
def __init__(
|
|
76
|
+
self,
|
|
77
|
+
width: int | None = None,
|
|
78
|
+
height: int | None = None,
|
|
79
|
+
aspect_ratio: str | None = None,
|
|
80
|
+
*args: Any,
|
|
81
|
+
**kwargs: Any,
|
|
82
|
+
) -> None:
|
|
83
|
+
if aspect_ratio and (width or height):
|
|
84
|
+
raise TypeError('Provide EITHER width and height OR aspect_ratio, not both.')
|
|
85
|
+
|
|
86
|
+
invalid_aspect_ratio_error = TypeError(
|
|
87
|
+
f'Aspect ratio must be "width/height" (e.g., "1/1" or "16/9"). Current: {aspect_ratio}'
|
|
88
|
+
)
|
|
89
|
+
if aspect_ratio:
|
|
90
|
+
parts = aspect_ratio.split('/', 1)
|
|
91
|
+
if len(parts) != 2 or any(not is_float(p) for p in parts):
|
|
92
|
+
raise invalid_aspect_ratio_error
|
|
93
|
+
|
|
94
|
+
self.width = width
|
|
95
|
+
self.height = height
|
|
96
|
+
self.aspect_ratio = aspect_ratio
|
|
97
|
+
|
|
98
|
+
super().__init__(*args, **kwargs)
|
|
99
|
+
|
|
100
|
+
def deconstruct(self) -> Any:
|
|
101
|
+
name, path, args, kwargs = super().deconstruct()
|
|
102
|
+
|
|
103
|
+
if self.width:
|
|
104
|
+
kwargs['width'] = self.width
|
|
105
|
+
if self.height:
|
|
106
|
+
kwargs['height'] = self.height
|
|
107
|
+
if self.aspect_ratio:
|
|
108
|
+
kwargs['aspect_ratio'] = self.aspect_ratio
|
|
109
|
+
|
|
110
|
+
return name, path, args, kwargs
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class WebPValidatedImageField(ImageDimensionMixin, WebPImageField):
|
|
114
|
+
def formfield(self, **kwargs):
|
|
115
|
+
defaults = {'validators': [self.validate_dimensions]}
|
|
116
|
+
defaults.update(kwargs)
|
|
117
|
+
return super().formfield(**defaults)
|
|
118
|
+
|
|
119
|
+
def validate_dimensions(self, image):
|
|
120
|
+
if not image:
|
|
121
|
+
return
|
|
122
|
+
|
|
123
|
+
img = Image.open(image)
|
|
124
|
+
image_width, image_height = img.size
|
|
125
|
+
|
|
126
|
+
if self.width and image_width != self.width:
|
|
127
|
+
raise ValidationError(_(f'Width must be {self.width}px (given: {image_width}px).'))
|
|
128
|
+
if self.height and image_height != self.height:
|
|
129
|
+
raise ValidationError(_(f'Height must be {self.height}px (given: {image_height}px).'))
|
|
130
|
+
|
|
131
|
+
if self.aspect_ratio:
|
|
132
|
+
target_width_aspect, target_height_aspect = (float(ratio) for ratio in self.aspect_ratio.split('/'))
|
|
133
|
+
target_ratio = target_width_aspect / target_height_aspect
|
|
134
|
+
image_ratio = image_width / image_height
|
|
135
|
+
if not math.isclose(target_ratio, image_ratio):
|
|
136
|
+
divisor = math.gcd(image_width, image_height)
|
|
137
|
+
image_width_aspect = image_width / divisor
|
|
138
|
+
image_height_aspect = image_height / divisor
|
|
139
|
+
raise ValidationError(
|
|
140
|
+
_(
|
|
141
|
+
f'The image must be in the {target_width_aspect}/{target_height_aspect} aspect ratio (given: {image_width_aspect}/{image_height_aspect})'
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class WebPAutoCropField(ImageDimensionMixin, WebPImageField):
|
|
147
|
+
def process_image(self, img: Image.Image) -> Image.Image:
|
|
148
|
+
if self.aspect_ratio:
|
|
149
|
+
target_w_aspect, target_h_aspect = (float(aspect) for aspect in self.aspect_ratio.split('/'))
|
|
150
|
+
target_ratio = target_w_aspect / target_h_aspect
|
|
151
|
+
elif self.width and self.height:
|
|
152
|
+
target_ratio = self.width / self.height
|
|
153
|
+
|
|
154
|
+
original_w, original_h = img.size
|
|
155
|
+
current_ratio = original_w / original_h
|
|
156
|
+
|
|
157
|
+
if not math.isclose(current_ratio, target_ratio):
|
|
158
|
+
if current_ratio > target_ratio:
|
|
159
|
+
new_w = int(original_h * target_ratio)
|
|
160
|
+
offset = (original_w - new_w) // 2
|
|
161
|
+
img = img.crop((offset, 0, offset + new_w, original_h))
|
|
162
|
+
else:
|
|
163
|
+
new_h = int(original_w / target_ratio)
|
|
164
|
+
offset = (original_h - new_h) // 2
|
|
165
|
+
img = img.crop((0, offset, original_w, offset + new_h))
|
|
166
|
+
|
|
167
|
+
if self.width and self.height and img.width > self.width:
|
|
168
|
+
img = img.resize((self.width, self.height), Image.Resampling.LANCZOS)
|
|
169
|
+
|
|
170
|
+
return img
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class DynamicImageFormField(forms.MultiValueField):
|
|
174
|
+
widget = ResizableImageWidget
|
|
175
|
+
|
|
176
|
+
def __init__(self, **kwargs):
|
|
177
|
+
fields = (
|
|
178
|
+
forms.ImageField(required=kwargs.get('required', True)),
|
|
179
|
+
forms.IntegerField(required=False, min_value=1),
|
|
180
|
+
forms.IntegerField(required=False, min_value=1),
|
|
181
|
+
forms.IntegerField(required=False, min_value=1, max_value=100),
|
|
182
|
+
)
|
|
183
|
+
super().__init__(fields, require_all_fields=False, **kwargs)
|
|
184
|
+
|
|
185
|
+
def compress(self, data_list):
|
|
186
|
+
if not data_list or not data_list[0]:
|
|
187
|
+
return None
|
|
188
|
+
|
|
189
|
+
img_file, max_width, max_height, quality = data_list
|
|
190
|
+
quality = quality or 80
|
|
191
|
+
|
|
192
|
+
if not hasattr(img_file, 'read'):
|
|
193
|
+
return img_file
|
|
194
|
+
|
|
195
|
+
img = Image.open(img_file)
|
|
196
|
+
|
|
197
|
+
if img.mode in ('RGBA', 'P'):
|
|
198
|
+
img = img.convert('RGBA')
|
|
199
|
+
elif img.mode != 'RGB':
|
|
200
|
+
img = img.convert('RGB')
|
|
201
|
+
|
|
202
|
+
if max_width or max_height:
|
|
203
|
+
target_w = max_width or img.width
|
|
204
|
+
target_h = max_height or img.height
|
|
205
|
+
img.thumbnail((target_w, target_h), Image.Resampling.LANCZOS)
|
|
206
|
+
|
|
207
|
+
output = io.BytesIO()
|
|
208
|
+
img.save(output, format='WEBP', quality=quality)
|
|
209
|
+
output.seek(0)
|
|
210
|
+
|
|
211
|
+
new_name = Path(img_file.name).with_suffix('.webp').name
|
|
212
|
+
return SimpleUploadedFile(new_name, output.read(), content_type='image/webp')
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from django.db import transaction
|
|
2
|
+
from django.db.models import FileField
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def delete_on_model_delete(sender, instance, **kwargs):
|
|
6
|
+
for field in instance._meta.fields:
|
|
7
|
+
if isinstance(field, FileField):
|
|
8
|
+
file = getattr(instance, field.name)
|
|
9
|
+
if file and file.storage.exists(file.name):
|
|
10
|
+
transaction.on_commit(lambda f=file: f.delete(save=False))
|
|
11
|
+
|
|
12
|
+
def delete_old_file(sender, instance, **kwargs):
|
|
13
|
+
if not instance.pk:
|
|
14
|
+
return
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
old_instance = sender.objects.get(pk=instance.pk)
|
|
18
|
+
except sender.DoesNotExist:
|
|
19
|
+
return
|
|
20
|
+
|
|
21
|
+
for field in instance._meta.fields:
|
|
22
|
+
if isinstance(field, FileField):
|
|
23
|
+
old_file = getattr(old_instance, field.name)
|
|
24
|
+
new_file = getattr(instance, field.name)
|
|
25
|
+
|
|
26
|
+
if old_file and old_file != new_file and old_file.storage.exists(old_file.name):
|
|
27
|
+
transaction.on_commit(lambda f=old_file: f.delete(save=False))
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
.card-grid {
|
|
2
|
+
display: grid;
|
|
3
|
+
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
|
|
4
|
+
gap: 16px;
|
|
5
|
+
padding: 16px 0;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.image-card {
|
|
9
|
+
display: flex;
|
|
10
|
+
flex-direction: column;
|
|
11
|
+
border: 1px solid var(--hairline-color, #e0e0e0);
|
|
12
|
+
border-radius: 4px;
|
|
13
|
+
background-color: var(--body-bg, #ffffff);
|
|
14
|
+
overflow: hidden;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.image-card__figure {
|
|
18
|
+
position: relative;
|
|
19
|
+
margin: 0;
|
|
20
|
+
aspect-ratio: 1;
|
|
21
|
+
background-color: var(--darkened-bg, #f8f8f8);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.image-card__link {
|
|
25
|
+
display: block;
|
|
26
|
+
width: 100%;
|
|
27
|
+
height: 100%;
|
|
28
|
+
text-decoration: none;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.image-card__img {
|
|
32
|
+
width: 100%;
|
|
33
|
+
height: 100%;
|
|
34
|
+
object-fit: cover;
|
|
35
|
+
display: block;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.image-card__control {
|
|
39
|
+
position: absolute;
|
|
40
|
+
top: 8px;
|
|
41
|
+
z-index: 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.image-card__control--top-left {
|
|
45
|
+
left: 8px;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.image-card__control--top-right {
|
|
49
|
+
right: 8px;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.image-card__checkbox {
|
|
53
|
+
width: 16px;
|
|
54
|
+
height: 16px;
|
|
55
|
+
margin: 0;
|
|
56
|
+
cursor: pointer;
|
|
57
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.image-card__badge {
|
|
61
|
+
position: absolute;
|
|
62
|
+
bottom: 8px;
|
|
63
|
+
padding: 2px 6px;
|
|
64
|
+
background-color: rgba(0, 0, 0, 0.65);
|
|
65
|
+
color: #ffffff;
|
|
66
|
+
font-size: 11px;
|
|
67
|
+
border-radius: 2px;
|
|
68
|
+
pointer-events: none;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.image-card__badge--bottom-left {
|
|
72
|
+
left: 8px;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.image-card__badge--bottom-right {
|
|
76
|
+
right: 8px;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.image-card__footer {
|
|
80
|
+
display: flex;
|
|
81
|
+
justify-content: space-between;
|
|
82
|
+
align-items: center;
|
|
83
|
+
padding: 8px 12px;
|
|
84
|
+
border-top: 1px solid var(--hairline-color, #e0e0e0);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.image-card__name-group {
|
|
88
|
+
flex: 1;
|
|
89
|
+
min-width: 0;
|
|
90
|
+
margin-right: 8px;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.image-card__name-display {
|
|
94
|
+
display: flex;
|
|
95
|
+
align-items: flex-start;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.image-card__title {
|
|
99
|
+
font-size: 13px;
|
|
100
|
+
color: var(--body-fg, #333333);
|
|
101
|
+
white-space: nowrap;
|
|
102
|
+
overflow: hidden;
|
|
103
|
+
text-overflow: ellipsis;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.image-card__name-edit {
|
|
107
|
+
display: none;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.image-card__name-group--editing .image-card__name-display {
|
|
111
|
+
display: none;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.image-card__name-group--editing .image-card__name-edit {
|
|
115
|
+
display: block;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.image-card__input {
|
|
119
|
+
width: 100%;
|
|
120
|
+
box-sizing: border-box;
|
|
121
|
+
padding: 4px;
|
|
122
|
+
border: 1px solid var(--border-color, #ccc);
|
|
123
|
+
border-radius: 3px;
|
|
124
|
+
font-size: 13px;
|
|
125
|
+
background-color: var(--body-bg, #ffffff);
|
|
126
|
+
color: var(--body-fg, #333333);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.image-card__input:focus {
|
|
130
|
+
border-color: var(--primary, #79aec8);
|
|
131
|
+
outline: none;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.image-card__action {
|
|
135
|
+
background: transparent;
|
|
136
|
+
border: none;
|
|
137
|
+
padding: 4px;
|
|
138
|
+
cursor: pointer;
|
|
139
|
+
color: var(--body-quiet-color, #666666);
|
|
140
|
+
display: flex;
|
|
141
|
+
align-items: center;
|
|
142
|
+
justify-content: center;
|
|
143
|
+
border-radius: 3px;
|
|
144
|
+
text-decoration: none;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.image-card__action:hover,
|
|
148
|
+
.image-card__action:focus {
|
|
149
|
+
color: var(--primary, #79aec8);
|
|
150
|
+
background-color: var(--darkened-bg, #f8f8f8);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.image-card__action--edit {
|
|
154
|
+
margin-left: 4px;
|
|
155
|
+
margin-top: -2px;
|
|
156
|
+
padding: 2px;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.image-card__action--overlay {
|
|
160
|
+
background-color: rgba(255, 255, 255, 0.9);
|
|
161
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.image-card__action--overlay:hover,
|
|
165
|
+
.image-card__action--overlay:focus {
|
|
166
|
+
background-color: var(--body-bg, #ffffff);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.image-card__action--danger {
|
|
170
|
+
color: var(--delete-button-bg, #ba2121);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
.image-card__action--danger:hover,
|
|
174
|
+
.image-card__action--danger:focus {
|
|
175
|
+
color: var(--error-fg, #ffffff);
|
|
176
|
+
background-color: var(--delete-button-bg, #ba2121);
|
|
177
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
document.addEventListener("click", (e) => {
|
|
2
|
+
const editBtn = e.target.closest(".image-card__action--edit");
|
|
3
|
+
if (editBtn) {
|
|
4
|
+
const group = editBtn.closest(".image-card__name-group");
|
|
5
|
+
group.classList.add("image-card__name-group--editing");
|
|
6
|
+
const input = group.querySelector(".image-card__input");
|
|
7
|
+
input.focus();
|
|
8
|
+
const val = input.value;
|
|
9
|
+
input.value = "";
|
|
10
|
+
input.value = val;
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const copyBtn = e.target.closest(".image-card__action--copy");
|
|
15
|
+
if (copyBtn && copyBtn.dataset.url) {
|
|
16
|
+
navigator.clipboard.writeText(copyBtn.dataset.url);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
function saveEdit(e) {
|
|
21
|
+
const form = e.target.closest("form");
|
|
22
|
+
const saveButton = form.querySelector('input[name="_save"]');
|
|
23
|
+
|
|
24
|
+
form.requestSubmit(saveButton);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
document.addEventListener("focusout", (e) => {
|
|
28
|
+
if (!e.target.matches(".image-card__input")) return;
|
|
29
|
+
const group = e.target.closest(".image-card__name-group");
|
|
30
|
+
group.classList.remove("image-card__name-group--editing");
|
|
31
|
+
saveEdit(e);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
document.addEventListener("keydown", (e) => {
|
|
35
|
+
if (e.key === "Enter" && e.target.matches(".image-card__input")) {
|
|
36
|
+
e.preventDefault();
|
|
37
|
+
saveEdit(e);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from django.core.files import File
|
|
5
|
+
from django.core.files.storage import FileSystemStorage
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HashedFileSystemStorage(FileSystemStorage):
|
|
9
|
+
def _save(self, name, content: File):
|
|
10
|
+
original_filepath = Path(name)
|
|
11
|
+
hasher = hashlib.blake2b(digest_size=16)
|
|
12
|
+
for chunck in content.chunks():
|
|
13
|
+
hasher.update(chunck)
|
|
14
|
+
|
|
15
|
+
hash = hasher.hexdigest()
|
|
16
|
+
hashed_filepath = original_filepath.parent / Path(hash + original_filepath.suffix)
|
|
17
|
+
return super()._save(hashed_filepath, content)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
{% extends "admin/change_list.html" %}
|
|
2
|
+
{% load i18n static %}
|
|
3
|
+
{% block result_list %}
|
|
4
|
+
{% if cl.result_list %}
|
|
5
|
+
<div class="card-grid">
|
|
6
|
+
{% for result in cl.result_list %}
|
|
7
|
+
<div class="image-card">
|
|
8
|
+
<figure class="image-card__figure">
|
|
9
|
+
{% if result._image %}
|
|
10
|
+
<input type="checkbox"
|
|
11
|
+
name="_selected_action"
|
|
12
|
+
value="{{ result.pk }}"
|
|
13
|
+
class="action-select image-card__control image-card__control--top-left image-card__checkbox"
|
|
14
|
+
aria-label="{% translate 'Select item' %}" />
|
|
15
|
+
<a href="{% url 'admin:'|add:cl.opts.app_label|add:'_'|add:cl.opts.model_name|add:'_delete' result.pk %}"
|
|
16
|
+
class="image-card__action image-card__action--overlay image-card__action--danger image-card__control image-card__control--top-right"
|
|
17
|
+
title="{% translate 'Delete' %}">
|
|
18
|
+
<svg width="16"
|
|
19
|
+
height="16"
|
|
20
|
+
viewBox="0 0 24 24"
|
|
21
|
+
fill="none"
|
|
22
|
+
stroke="currentColor"
|
|
23
|
+
stroke-width="2"
|
|
24
|
+
stroke-linecap="round"
|
|
25
|
+
stroke-linejoin="round">
|
|
26
|
+
<polyline points="3 6 5 6 21 6"></polyline>
|
|
27
|
+
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
|
|
28
|
+
</svg>
|
|
29
|
+
</a>
|
|
30
|
+
{% if cl.model_admin.edit_page %}
|
|
31
|
+
<a href="{% url 'admin:'|add:cl.opts.app_label|add:'_'|add:cl.opts.model_name|add:'_change' result.pk %}"
|
|
32
|
+
class="image-card__link">
|
|
33
|
+
<img class="image-card__img"
|
|
34
|
+
src="{{ result._image.url }}"
|
|
35
|
+
alt="{{ result._name }}" />
|
|
36
|
+
</a>
|
|
37
|
+
{% else %}
|
|
38
|
+
<img class="image-card__img"
|
|
39
|
+
src="{{ result._image.url }}"
|
|
40
|
+
alt="{{ result._name }}" />
|
|
41
|
+
{% endif %}
|
|
42
|
+
<span class="image-card__badge image-card__badge--bottom-left">{{ result._image.size|filesizeformat }}</span>
|
|
43
|
+
<span class="image-card__badge image-card__badge--bottom-right">{{ result._image.width }}x{{ result._image.height }}</span>
|
|
44
|
+
{% endif %}
|
|
45
|
+
</figure>
|
|
46
|
+
<footer class="image-card__footer">
|
|
47
|
+
<div class="image-card__name-group">
|
|
48
|
+
<div class="image-card__name-display">
|
|
49
|
+
<span class="image-card__title" title="{{ result._name }}">{{ result._name }}</span>
|
|
50
|
+
<button type="button"
|
|
51
|
+
class="image-card__action image-card__action--edit"
|
|
52
|
+
title="{% translate 'Edit name' %}">
|
|
53
|
+
<svg width="12"
|
|
54
|
+
height="12"
|
|
55
|
+
viewBox="0 0 24 24"
|
|
56
|
+
fill="none"
|
|
57
|
+
stroke="currentColor"
|
|
58
|
+
stroke-width="2"
|
|
59
|
+
stroke-linecap="round"
|
|
60
|
+
stroke-linejoin="round">
|
|
61
|
+
<path d="M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"></path>
|
|
62
|
+
</svg>
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
<div class="image-card__name-edit">
|
|
66
|
+
<input type="hidden"
|
|
67
|
+
name="form-{{ forloop.counter0 }}-id"
|
|
68
|
+
value="{{ result.pk }}" />
|
|
69
|
+
<input type="text"
|
|
70
|
+
name="form-{{ forloop.counter0 }}-{{ result._name_field }}"
|
|
71
|
+
value="{{ result._name }}"
|
|
72
|
+
class="image-card__input"
|
|
73
|
+
required
|
|
74
|
+
aria-label="{% translate 'Edit name' %}" />
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
<button type="button"
|
|
78
|
+
class="image-card__action image-card__action--copy"
|
|
79
|
+
aria-label="{% translate 'Copy URL' %}"
|
|
80
|
+
data-url="{{ result._abs_url }}">
|
|
81
|
+
<svg width="16"
|
|
82
|
+
height="16"
|
|
83
|
+
viewBox="0 0 24 24"
|
|
84
|
+
fill="none"
|
|
85
|
+
stroke="currentColor"
|
|
86
|
+
stroke-width="2"
|
|
87
|
+
stroke-linecap="round"
|
|
88
|
+
stroke-linejoin="round">
|
|
89
|
+
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
|
|
90
|
+
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
|
|
91
|
+
</svg>
|
|
92
|
+
</button>
|
|
93
|
+
</footer>
|
|
94
|
+
</div>
|
|
95
|
+
{% endfor %}
|
|
96
|
+
</div>
|
|
97
|
+
{% endif %}
|
|
98
|
+
{% endblock %}
|