MOdified the 0011_populate_photos migration to set photo thumbnail values which include the photos dirname under the media_root. Added a reverse migration, for easier testing from a clean state.

- Legacy-Id: 11264
This commit is contained in:
Henrik Levkowetz 2016-06-02 19:20:34 +00:00
parent c83dae57fb
commit ab095fca5b

View file

@ -15,7 +15,7 @@ def photo_name(person,thumb=False):
def forward(apps,schema_editor): def forward(apps,schema_editor):
Person = apps.get_model('person','Person') Person = apps.get_model('person','Person')
images_dir = os.path.join(settings.PHOTOS_DIR,settings.PHOTO_URL_PREFIX) images_dir = settings.PHOTOS_DIR
image_filenames = [] image_filenames = []
for (dirpath, dirnames, filenames) in os.walk(images_dir): for (dirpath, dirnames, filenames) in os.walk(images_dir):
image_filenames.extend(filenames) image_filenames.extend(filenames)
@ -24,16 +24,22 @@ def forward(apps,schema_editor):
for person in Person.objects.all(): for person in Person.objects.all():
dirty = False dirty = False
if photo_name(person,thumb=False) in image_basenames: if photo_name(person,thumb=False) in image_basenames:
person.photo = image_filenames[image_basenames.index(photo_name(person,thumb=False))] person.photo = os.path.join(settings.PHOTO_URL_PREFIX, image_filenames[image_basenames.index(photo_name(person,thumb=False))])
dirty = True dirty = True
if photo_name(person,thumb=True) in image_basenames: if photo_name(person,thumb=True) in image_basenames:
person.photo_thumb = image_filenames[image_basenames.index(photo_name(person,thumb=True))] person.photo_thumb = os.path.join(settings.PHOTO_URL_PREFIX, image_filenames[image_basenames.index(photo_name(person,thumb=True))])
dirty = True dirty = True
if dirty: if dirty:
person.save() person.save()
def reverse(apps, schema_editor): def reverse(apps, schema_editor):
pass Person = apps.get_model('person','Person')
for person in Person.objects.filter(photo__gt=''):
person.photo = None
person.save()
for person in Person.objects.filter(photo_thumb__gt=''):
person.photo_thumb = None
person.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):