Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update images in DB #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions wagtailmakeup/management/commands/make_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ def search_for_image(self, query, count):
# TODO, if there is no query support random images
return api.photo.random(query=query, count=count)

def update_wagtail_image(self, image, photos):
"""Replaces an image object with the an unsplash image
def chunk_images(self, chunks):
"""
Split the images into `chunks` chunks, in a random order

Args:
image (class): CustomImage objects
photos (class): unsplash.models.ResultSet of photos
Based on https://stackoverflow.com/a/14861842.
"""
# random selection from photos
photo = random.choice(photos).id
image.file = f"{MEDIA_ROOT}/unsplash-{photo}"
image.save()
images = Image.objects.all().order_by('?')
return [images[i::chunks] for i in range(chunks)]

def handle(self, *args, **options):
if (
Expand All @@ -84,8 +81,8 @@ def handle(self, *args, **options):
self.save_image(photo)

# Update all images
for image in Image.objects.all():
self.update_wagtail_image(image, photos)
for photo, images in zip(photos, self.chunk_images(len(photos))):
images.update(file=f"unsplash-{photo.id}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't image be a list var here, returned from chunk_images? .update() won't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slicing querysets generally produces querysets, but i'm not sure how that fairs with the mutation that chunk_images is doing.

In my testing it did return lists, although I thought that was something going weird as I had no images locally. If it doesn't work I'll just have to tweak chunk_images, but that shouldn't be too difficult.


# Finally, delete the existing image renditions
Image.get_rendition_model().objects.all().delete()