Skip to content

Commit

Permalink
Add support to migrate from django-stimage
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed May 20, 2022
1 parent 46cc0fa commit 001da1d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pictures/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def alter_picture_field(
if not isinstance(from_field, PictureField) and isinstance(
to_field, PictureField
):
self.to_picture_field(to_model)
self.to_picture_field(from_model, to_model)
elif isinstance(from_field, PictureField) and not isinstance(
to_field, PictureField
):
Expand Down Expand Up @@ -63,7 +63,15 @@ def from_picture_field(self, from_model: Type[models.Model]):
field_file = getattr(obj, self.name)
field_file.delete_all()

def to_picture_field(self, to_model: Type[models.Model]):
def to_picture_field(
self, from_model: Type[models.Model], to_model: Type[models.Model]
):
from_field = from_model._meta.get_field(self.name)
if hasattr(from_field.attr_class, "delete_variations"):
# remove obsolete django-stdimage variations
for obj in from_model._default_manager.all().iterator():
field_file = getattr(obj, self.name)
field_file.delete_variations()
for obj in to_model._default_manager.all().iterator():
field_file = getattr(obj, self.name)
field_file.save_all()
49 changes: 48 additions & 1 deletion tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from django.core.management import call_command
from django.db import models
from django.db.models.fields.files import ImageFieldFile

from pictures import migrations
from pictures.models import PictureField
Expand Down Expand Up @@ -156,6 +157,51 @@ def test_from_picture_field(self, stub_worker, image_upload_file):

@pytest.mark.django_db
def test_to_picture_field(self, request, stub_worker, image_upload_file):
class FromModel(models.Model):
picture = models.ImageField()

class Meta:
app_label = request.node.name
db_table = "testapp_profile"

class ToModel(models.Model):
name = models.CharField(max_length=100)
picture = models.ImageField(upload_to="testapp/profile/")

class Meta:
app_label = request.node.name
db_table = "testapp_profile"

luke = ToModel.objects.create(name="Luke", picture=image_upload_file)
stub_worker.join()
migration = migrations.AlterPictureField("profile", "picture", PictureField())
migration.to_picture_field(FromModel, Profile)
stub_worker.join()
luke.refresh_from_db()
path = (
Profile.objects.get(pk=luke.pk)
.picture.aspect_ratios["16/9"]["WEBP"][100]
.path
)
assert path.exists()

@pytest.mark.django_db
def test_to_picture_field__from_stdimage(
self, request, stub_worker, image_upload_file
):
class StdImageFieldFile(ImageFieldFile):
delete_variations = Mock()

class StdImageField(models.ImageField):
attr_class = StdImageFieldFile

class FromModel(models.Model):
picture = StdImageField()

class Meta:
app_label = request.node.name
db_table = "testapp_profile"

class ToModel(models.Model):
name = models.CharField(max_length=100)
picture = models.ImageField(upload_to="testapp/profile/")
Expand All @@ -167,7 +213,7 @@ class Meta:
luke = ToModel.objects.create(name="Luke", picture=image_upload_file)
stub_worker.join()
migration = migrations.AlterPictureField("profile", "picture", PictureField())
migration.to_picture_field(Profile)
migration.to_picture_field(FromModel, Profile)
stub_worker.join()
luke.refresh_from_db()
path = (
Expand All @@ -176,6 +222,7 @@ class Meta:
.path
)
assert path.exists()
assert StdImageFieldFile.delete_variations.called

@pytest.mark.django_db(transaction=True)
def test_database_backwards_forwards(self):
Expand Down

0 comments on commit 001da1d

Please sign in to comment.