本文整理匯總了Python中photos.models.Photo.generate_photo_id方法的典型用法代碼示例。如果您正苦於以下問題:Python Photo.generate_photo_id方法的具體用法?Python Photo.generate_photo_id怎麽用?Python Photo.generate_photo_id使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類photos.models.Photo
的用法示例。
在下文中一共展示了Photo.generate_photo_id方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: migrate_photo
# 需要導入模塊: from photos.models import Photo [as 別名]
# 或者: from photos.models.Photo import generate_photo_id [as 別名]
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"])
示例2: add_youtube_photo
# 需要導入模塊: from photos.models import Photo [as 別名]
# 或者: from photos.models.Photo import generate_photo_id [as 別名]
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)