annotate user_photos/s3.py @ 870:ee56c8c8cf98

More WIP on ssl_images management command and tests.
author Brian Neal <bgneal@gmail.com>
date Mon, 15 Dec 2014 21:16:47 -0600
parents bf5340705d0c
children
rev   line source
bgneal@718 1 """Module for all S3 related operations for the user_photos application."""
bgneal@718 2
bgneal@718 3 import logging
bgneal@718 4
bgneal@718 5 from django.conf import settings
bgneal@718 6
bgneal@718 7 from core.s3 import S3Bucket
bgneal@718 8
bgneal@718 9
bgneal@718 10 logger = logging.getLogger(__name__)
bgneal@718 11
bgneal@718 12
bgneal@718 13 def delete_photos(qs):
bgneal@718 14 """Delete the photos stored on S3 for the given Photo queryset.
bgneal@718 15
bgneal@718 16 Returns the number of photos actually deleted.
bgneal@718 17
bgneal@718 18 """
bgneal@718 19
bgneal@718 20 bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY,
bgneal@718 21 settings.USER_PHOTOS_SECRET_KEY,
bgneal@718 22 settings.USER_PHOTOS_BASE_URL,
bgneal@718 23 settings.USER_PHOTOS_BUCKET)
bgneal@718 24
bgneal@718 25 key_urls = []
bgneal@718 26 for photo in qs:
bgneal@718 27 key_urls.append(photo.url)
bgneal@718 28 key_urls.append(photo.thumb_url)
bgneal@718 29 req_cnt = len(key_urls)
bgneal@718 30
bgneal@718 31 logger.info("Requesting deletion of %d user photo(s) from S3", req_cnt)
bgneal@718 32
bgneal@718 33 act_cnt = bucket.delete_keys(key_urls)
bgneal@718 34
bgneal@718 35 if act_cnt == req_cnt:
bgneal@718 36 logger.info("Deleted %d user photo(s) from S3", act_cnt)
bgneal@718 37 else:
bgneal@718 38 logger.warning("Deleted %d user photo(s) out of %d", act_cnt, req_cnt)
bgneal@718 39
bgneal@718 40 return act_cnt