Skip to content

Commit

Permalink
Guard against multiple ExternalImageSized existing - partial fix for …
Browse files Browse the repository at this point in the history
…MOX-87
  • Loading branch information
cnorthwood committed Nov 26, 2010
1 parent d520ad5 commit 82577a7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion molly/apps/external_media/utils.py
Expand Up @@ -5,6 +5,7 @@
from models import ExternalImage, ExternalImageSized

def resize_external_image(url, width, timeout=None):

ei, created = ExternalImage.objects.get_or_create(url=url)

request = AnyMethodRequest(url, method='HEAD')
Expand Down Expand Up @@ -33,6 +34,11 @@ def resize_external_image(url, width, timeout=None):
ei.last_modified = response.headers.get('Last-Modified')
ei.save()

eis, created = ExternalImageSized.objects.get_or_create(external_image=ei, width=width)
try:
eis, created = ExternalImageSized.objects.get_or_create(external_image=ei, width=width)
except ExternalImageSized.MultipleObjectsReturned:
for eis in ExternalImageSized.objects.filter(external_image=ei, width=width):
eis.delete()
eis, created = ExternalImageSized.objects.get_or_create(external_image=ei, width=width)

return eis

0 comments on commit 82577a7

Please sign in to comment.