comparison user_photos/s3.py @ 718:bf5340705d0c

Completed view to delete user photos. Still need to modify the admin to delete not just the model instance but the S3 bucket keys.
author Brian Neal <bgneal@gmail.com>
date Wed, 18 Sep 2013 21:34:05 -0500
parents
children
comparison
equal deleted inserted replaced
717:846cf9a06a04 718:bf5340705d0c
1 """Module for all S3 related operations for the user_photos application."""
2
3 import logging
4
5 from django.conf import settings
6
7 from core.s3 import S3Bucket
8
9
10 logger = logging.getLogger(__name__)
11
12
13 def delete_photos(qs):
14 """Delete the photos stored on S3 for the given Photo queryset.
15
16 Returns the number of photos actually deleted.
17
18 """
19
20 bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY,
21 settings.USER_PHOTOS_SECRET_KEY,
22 settings.USER_PHOTOS_BASE_URL,
23 settings.USER_PHOTOS_BUCKET)
24
25 key_urls = []
26 for photo in qs:
27 key_urls.append(photo.url)
28 key_urls.append(photo.thumb_url)
29 req_cnt = len(key_urls)
30
31 logger.info("Requesting deletion of %d user photo(s) from S3", req_cnt)
32
33 act_cnt = bucket.delete_keys(key_urls)
34
35 if act_cnt == req_cnt:
36 logger.info("Deleted %d user photo(s) from S3", act_cnt)
37 else:
38 logger.warning("Deleted %d user photo(s) out of %d", act_cnt, req_cnt)
39
40 return act_cnt