def migrate_photo(old_buckets, s3_bucket, photo): print "Migrating photo: " + str(photo) old_photo_bucket = find_photo_bucket(old_buckets, photo.photo_id) new_subdomain = choose_random_subdomain() new_storage_id = Photo.generate_photo_id() print "New subdomain: " + new_subdomain print "New storage_id: " + new_storage_id def upload(ext): filename = os.path.join(settings.LOCAL_PHOTO_BUCKETS_BASE_PATH, old_photo_bucket, photo.photo_id + ext) print "Uploading: " + filename key = Key(s3_bucket, new_storage_id + ext) key.metadata = {"Content-Type": "image/jpeg"} key.set_contents_from_filename(filename) key.close() upload(".jpg") for ext in all_photo_extensions: upload("_" + ext + ".jpg") photo.subdomain = new_subdomain photo.storage_id = new_storage_id photo.save(update_fields=["subdomain", "storage_id"])
def add_youtube_photo(client_upload_id, storage_id, author, album, now, youtube_id): def get_next_album_index(album): album_index_q = Photo.objects.filter(album=album).aggregate(Max("album_index")) max_album_index = album_index_q["album_index__max"] if max_album_index is None: return 0 else: return max_album_index + 1 success = False while not success: try: with transaction.atomic(): next_album_index = get_next_album_index(album) p, created = Photo.objects.get_or_create( storage_id=storage_id, defaults={ "photo_id": Photo.generate_photo_id(), "media_type": Photo.MEDIA_TYPE_YOUTUBE, "client_upload_id": client_upload_id, "subdomain": Photo.choose_random_subdomain(), "date_created": now, "author": author, "album": album, "album_index": next_album_index, "youtube_id": youtube_id, }, ) except IntegrityError: # This will happen if there is a collision with a duplicate # 'album_index' from a concurrent request success = False else: success = True if created: if not in_testing_mode(): # Update the photo servers: for photo_server in PhotoServer.objects.filter(subdomain=p.subdomain, unreachable=False): # TODO We should use concurrent requests for this num_retries = 5 initial_retry_time = 4 try: request_with_n_retries( num_retries, initial_retry_time, lambda: photo_server_set_photos(photo_server.photos_update_url, photo_server.auth_key, [p]), ) except requests.exceptions.RequestException: # TODO Log this photo_server.set_unreachable() album.save_revision(now, True) photos_added_to_album.send(sender=None, photos=[p.photo_id], by_user=author, to_album=album)