Skip to content

Commit 33f9192

Browse files
committed
add photo data migration
1 parent f8a52ea commit 33f9192

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Generated by Django 5.1.5 on 2025-06-03 09:58
2+
3+
import shutil
4+
from pathlib import Path
5+
from typing import TYPE_CHECKING
6+
7+
from django.db import migrations
8+
9+
from gallery.thumbnails import resize_photo
10+
11+
if TYPE_CHECKING:
12+
from gallery.models import Photo
13+
14+
15+
def migrate_photos(apps, schema_editor):
16+
Photo: "Photo" = apps.get_model("gallery", "Photo")
17+
18+
for photo in Photo.objects.all():
19+
image_path = Path(photo.image.path)
20+
if not image_path.exists():
21+
photo.delete()
22+
continue
23+
24+
photo.image.save(image_path.name, photo.image)
25+
old_dir = image_path.with_suffix("")
26+
if old_dir.exists():
27+
shutil.rmtree(old_dir)
28+
29+
resize_photo(photo)
30+
31+
32+
class Migration(migrations.Migration):
33+
dependencies = [
34+
("gallery", "0004_alter_photo_image"),
35+
]
36+
37+
operations = [migrations.RunPython(migrate_photos)]

0 commit comments

Comments
 (0)