bgneal@718: """Module for all S3 related operations for the user_photos application."""
bgneal@718: 
bgneal@718: import logging
bgneal@718: 
bgneal@718: from django.conf import settings
bgneal@718: 
bgneal@718: from core.s3 import S3Bucket
bgneal@718: 
bgneal@718: 
bgneal@718: logger = logging.getLogger(__name__)
bgneal@718: 
bgneal@718: 
bgneal@718: def delete_photos(qs):
bgneal@718:     """Delete the photos stored on S3 for the given Photo queryset.
bgneal@718: 
bgneal@718:     Returns the number of photos actually deleted.
bgneal@718: 
bgneal@718:     """
bgneal@718: 
bgneal@718:     bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY,
bgneal@718:                       settings.USER_PHOTOS_SECRET_KEY,
bgneal@718:                       settings.USER_PHOTOS_BASE_URL,
bgneal@718:                       settings.USER_PHOTOS_BUCKET)
bgneal@718: 
bgneal@718:     key_urls = []
bgneal@718:     for photo in qs:
bgneal@718:         key_urls.append(photo.url)
bgneal@718:         key_urls.append(photo.thumb_url)
bgneal@718:     req_cnt = len(key_urls)
bgneal@718: 
bgneal@718:     logger.info("Requesting deletion of %d user photo(s) from S3", req_cnt)
bgneal@718: 
bgneal@718:     act_cnt = bucket.delete_keys(key_urls)
bgneal@718: 
bgneal@718:     if act_cnt == req_cnt:
bgneal@718:         logger.info("Deleted %d user photo(s) from S3", act_cnt)
bgneal@718:     else:
bgneal@718:         logger.warning("Deleted %d user photo(s) out of %d", act_cnt, req_cnt)
bgneal@718: 
bgneal@718:     return act_cnt