feat: fix thumbnails button

This commit is contained in:
relikd
2025-06-12 00:56:37 +02:00
parent b98b882665
commit a78c8da503
6 changed files with 51 additions and 6 deletions

View File

@@ -69,3 +69,4 @@ class PlaceAdmin(admin.ModelAdmin):
list_display = ['title', 'category', 'sort', 'created'] list_display = ['title', 'category', 'sort', 'created']
search_fields = ['title'] search_fields = ['title']
list_filter = ['category'] list_filter = ['category']
change_list_template = 'admin/place_tools.html'

View File

@@ -81,16 +81,17 @@ class Place(models.Model):
return rv return rv
def update_cover_image(self): def update_cover_image(self):
path = self.fixed_os_path('cov.jpg')
if self.image: if self.image:
img = Image.open(self.image.path) img = Image.open(self.image.path)
thumb = ImageOps.fit(img, (600, 400)) thumb = ImageOps.fit(img, (600, 400))
# img.thumbnail((600, 400)) # img.thumbnail((600, 400))
path.parent.mkdir(parents=True, exist_ok=True) path = self.image.path.replace('img.jpg', 'cov.jpg')
# path.parent.mkdir(parents=True, exist_ok=True)
thumb.save(path, 'jpeg') thumb.save(path, 'jpeg')
else: # else:
if path.exists(): # path = self.fixed_os_path('cov.jpg')
os.remove(path) # if path.exists():
# os.remove(path)
@staticmethod @staticmethod
def update_json(): def update_json():
@@ -115,6 +116,11 @@ class Place(models.Model):
}) })
return rv return rv
@staticmethod
def recreateThumbnails() -> None:
for x in Place.objects.all():
x.update_cover_image()
@receiver(post_delete, sender=Place) @receiver(post_delete, sender=Place)
def on_delete_Place(sender, instance: 'Place', using, **kwargs): def on_delete_Place(sender, instance: 'Place', using, **kwargs):

23
backend/app/urls.py Normal file
View File

@@ -0,0 +1,23 @@
from django.http import HttpRequest, JsonResponse
from django.urls import path
from app.models.place import Place
def run_tool(request: HttpRequest):
if request.method != 'POST':
return JsonResponse({'error': 'unsupported method type'})
action = request.POST.get('action')
if action == 'generate-thumbnails':
Place.recreateThumbnails()
else:
return JsonResponse({'error': 'unknown action'})
return JsonResponse({'success': 'ok'})
urlpatterns = [
path('tool/', run_tool, name='exec-tool'),
]

View File

@@ -68,7 +68,7 @@ ROOT_URLCONF = 'config.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], # BASE_DIR / 'templates' 'DIRS': [BASE_DIR / 'templates'], # BASE_DIR / 'templates'
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [

View File

@@ -23,4 +23,5 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path(settings.ADMIN_URL, admin.site.urls), path(settings.ADMIN_URL, admin.site.urls),
path('', include('common.urls')), path('', include('common.urls')),
path('app/', include('app.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@@ -0,0 +1,14 @@
{% extends "admin/change_list.html" %}
{% block object-tools-items %}
{{ block.super }}
<li>
<form action="{% url 'exec-tool' %}" method="post" autocomplete="off">
{% csrf_token %}
<select name="action" onchange="this.form.submit()" style="width:3em">
<option value="">...</option>
<option value="generate-thumbnails">Rebuild Thumbnails</option>
</select>
</form>
</li>
{% endblock %}