Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Last active January 31, 2018 08:09
Show Gist options
  • Save LowerDeez/6c9e0d480835b4fc98550915d81baf79 to your computer and use it in GitHub Desktop.
Save LowerDeez/6c9e0d480835b4fc98550915d81baf79 to your computer and use it in GitHub Desktop.
Django. Download image from url and create object with this image and FIlerImageField
def estate_gallery_creator(images, base_estate_object):
"""
Creates BaseEstateGallery objects for BaseEstate instance
:param images: list of images urls
:param base_estate_object: BaseEstate instance id
:return: creates BaseEstateGallery object
"""
for index, image_url in enumerate(images, 1):
# Get the filename from the url, used for saving later
file_name = image_url.split('/')[-1]
if image_exists(file_name=file_name, index=index, base_estate_object=base_estate_object):
continue
try:
request = requests.get(image_url, stream=True)
# Check request status
if request.status_code != requests.codes.ok:
continue
except requests_exceotions.MissingSchema as e:
continue
# Create a temporary file for image
lf = tempfile.NamedTemporaryFile()
# Read the streamed image in sections
for block in request.iter_content(1024 * 8):
# If no more file then stop
if not block:
break
# Try to write image block to temporary file, if something happens - close the file and break the cycle
try:
lf.write(block)
except Exception as e:
lf.close()
break
# If temporary file exists - create file object
if lf:
file_obj = File(lf, name=file_name)
# If temporary file does not exists, go to the next iteration
else:
continue
created, image = create_image_and_folder(
base_estate_object=base_estate_object,
file_obj=file_obj,
file_name=file_name
)
# If image is new - create new object of BaseEstateGallery
if created:
gallery, created = BaseEstateGallery.objects.get_or_create(
parent=base_estate_object,
media=image,
order=index
)
rialtor_profile = base_estate_object.rialtor
if rialtor_profile and rialtor_profile.water_mark:
add_watermark(
gallery.media.file, rialtor_profile.water_mark.file
)
if index == 1: # if image is first - set it as main
gallery.is_main_photo = True
gallery.save()
# Automatically cleans up the temporary file
lf.close()
def image_exists(file_name, index, base_estate_object):
"""
Check if image already exists for passed base estate object
:param file_name:
:param index:
:param base_estate_object:
:return:
"""
# Check if image with this name already exists
image = Image.objects.filter(name=file_name).first()
# If exists - skip this file and try to get or create BaseEstateGallery
# with base_estate_object if object was deleted and set first image as main
if image and default_storage.exists(image.file):
old_image, created = BaseEstateGallery.objects.get_or_create(
parent=base_estate_object,
media=image)
# set first image as main if index == 1
if index == 1:
old_image.is_main_photo = True
old_image.save()
return True
# if image object exists but file is missing - delete image object
elif image and not default_storage.exists(image.file):
image.delete()
return False
return False
def create_image_and_folder(base_estate_object, file_obj, file_name):
"""
Creates folder for estate's images and image object
:param base_estate_object:
:param file_obj:
:param file_name:
:return:
"""
# Crate parent folder for CRM photos
parent_folder, folder_created = Folder.objects.get_or_create(name='CRM')
# Create sub folder for every ad
folder, sub_folder_created = Folder.objects.get_or_create(
name=(str(base_estate_object.external_id) or 'subfolder'),
parent=parent_folder)
# Create image object and put it in folder with ad's external_id name
image, created = Image.objects.get_or_create(file=file_obj)
image.name = file_name or None
image.folder = folder
image.save()
return created, image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment