本文整理汇总了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)