annotate user_photos/s3.py @ 1157:e4f2d6a4b401

Rework S3 connection logic for latest versions of Python 2.7. Had to make these changes for Ubuntu 16.04. Seems backward compatible with production.
author Brian Neal <bgneal@gmail.com>
date Thu, 19 Jan 2017 18:35:53 -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