def connect(): # Code based on the example at https://github.com/un1t/django-cleanup/tree/2b02f61c151296571e104670c017db98f3d621f9#signals def delete_sorl_thumbnail(file, *args, **kwargs): delete( file, # Should not delete the source file, as this is already done by `django-cleanup` delete_file=False, ) cleanup_pre_delete.connect(delete_sorl_thumbnail)
def test_sorlthumbnail_delete(pic1): # https://github.com/mariocesar/sorl-thumbnail cleanup_pre_delete.connect(sorl_delete) from sorl.thumbnail import get_thumbnail product = Product.objects.create(sorl_image=pic1['filename']) assert os.path.exists(pic1['path']) im = get_thumbnail( product.sorl_image, '100x100', crop='center', quality=50) thumbnail_path = os.path.join(settings.MEDIA_ROOT, im.name) assert os.path.exists(thumbnail_path) with transaction.atomic(get_using(product)): product.delete() assert not os.path.exists(pic1['path']) assert not os.path.exists(thumbnail_path) cleanup_pre_delete.disconnect(sorl_delete)
def test_sorlthumbnail_delete(pic1): # https://github.com/mariocesar/sorl-thumbnail cleanup_pre_delete.connect(sorl_delete) from sorl.thumbnail import get_thumbnail product = Product.objects.create(sorl_image=pic1['filename']) assert os.path.exists(pic1['path']) im = get_thumbnail( product.sorl_image, '100x100', crop='center', quality=50) thumbnail_path = os.path.join(settings.MEDIA_ROOT, im.name) assert os.path.exists(thumbnail_path) with transaction.atomic(get_using(product)): product.delete() assert not os.path.exists(pic1['path']) assert not os.path.exists(thumbnail_path) cleanup_pre_delete.disconnect(sorl_delete)
def test_sorlthumbnail_delete(picture): # https://github.com/mariocesar/sorl-thumbnail models = pytest.importorskip("django_cleanup.testapp.models.integration") ProductIntegration = models.ProductIntegration sorl_delete = models.sorl_delete cleanup_pre_delete.connect(sorl_delete) from sorl.thumbnail import get_thumbnail product = ProductIntegration.objects.create(sorl_image=picture['filename']) assert os.path.exists(picture['path']) im = get_thumbnail( product.sorl_image, '100x100', crop='center', quality=50) thumbnail_path = os.path.join(settings.MEDIA_ROOT, im.name) assert os.path.exists(thumbnail_path) with transaction.atomic(get_using(product)): product.delete() assert not os.path.exists(picture['path']) assert not os.path.exists(thumbnail_path) cleanup_pre_delete.disconnect(sorl_delete)
delete(kwargs['file']) # empty directories 지우는 방법 찾아보기 # def deleted_empty_directories(**kwargs): # import os # for root, dirs, files in os.walk(os.path.join(settings.MEDIA_ROOT)): # print("root=" + root) # # print("dirs=" + dirs) # for d in dirs: # print("directory = " + d) # dir = os.path.join(root, d) # # check if dir is empty # if not os.listdir(dir): # os.rmdir(dir) cleanup_pre_delete.connect(sorl_delete) # cleanup_post_delete.connect(deleted_empty_directories) @api_view(['GET', 'POST']) def list(request): if request.method == 'GET': householdAccountBook = HouseholdAccountBook.objects.all() serializer = HouseholdAccountBookSerializer(householdAccountBook, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = HouseholdAccountBookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
from django_cleanup.signals import cleanup_pre_delete from easy_thumbnails.files import get_thumbnailer from easy_thumbnails.signals import saved_file from easy_thumbnails.signal_handlers import generate_aliases_global import shutil import os saved_file.connect(generate_aliases_global) def delete_thumbnails(**kwargs): kwargs['file'].old_path = kwargs['file'].path get_thumbnailer(kwargs['file']).delete_thumbnails() cleanup_pre_delete.connect(delete_thumbnails) def cleanup_empty_dirs(**kwargs): # initially set first dir path of file ... path = os.path.dirname(kwargs['file'].old_path) # ... and remove all empty parent directories while True: if path == settings.MEDIA_ROOT: break if len(os.listdir(path)) == 0: shutil.rmtree(path) path = os.path.dirname(path)
image = models.FileField(upload_to='testapp', blank=True, null=True) sorl_image = ImageField(upload_to='testapp', blank=True) easy_image = ThumbnailerImageField(upload_to='testapp', blank=True) class Meta: abstract = True class Product(ProductAbstract): pass class ProductProxy(Product): class Meta: proxy = True class ProductUnmanaged(ProductAbstract): id = models.AutoField(primary_key=True) class Meta: managed = False db_table = 'testapp_product' def sorl_delete(**kwargs): from sorl.thumbnail import delete delete(kwargs['file']) cleanup_pre_delete.connect(sorl_delete)
return None def get_missing_at_inat_observations(pulled_inat_ids): """ Get all observations that exist in our database with an iNaturalist ID but that were not returned by the iNaturalist pull. """ missing_indiv = Individual.objects.all().filter( inaturalist_id__isnull=False).exclude( inaturalist_id__in=pulled_inat_ids) missing_nests = Nest.objects.all().filter( inaturalist_id__isnull=False).exclude( inaturalist_id__in=pulled_inat_ids) return list(missing_indiv) + list(missing_nests) # The following functions signals are only there for debugging purposes # (I want to see ERRORs in log file for easy checks) # This should be removed (or at the very least limited to a INFO log level) once issues are solved def cleanup_log_predelete(**kwargs): logger.error(f'Cleanup: predelete {kwargs["file"]} (not an actual error)') def cleanup_log_postdelete(**kwargs): logger.error(f'Cleanup: postdelete {kwargs["file"]} (not an actual error)') cleanup_pre_delete.connect(cleanup_log_predelete) cleanup_post_delete.connect(cleanup_log_postdelete)
def make_thumbnail(self): thumb = Image.open(self.image.path) size = (240, 240) width, height = thumb.size w_ratio = math.floor(width / size[0]) h_ratio = math.floor(height / size[1]) ratio = w_ratio if w_ratio < h_ratio else h_ratio new_width = size[0] * ratio new_height = size[1] * ratio left = math.floor((width - new_width) / 2) top = math.floor((height - new_height) / 2) right = math.floor((width + new_width) / 2) bottom = math.floor((height + new_height) / 2) thumb = thumb.crop((left, top, right, bottom)) thumb.thumbnail(size, Image.ANTIALIAS) if not os.path.exists(self.thumbnail["folder"]): os.makedirs(self.thumbnail["folder"]) thumb.save(self.thumbnail["path"]) def thumbnail_delete(**kwargs): with contextlib.suppress(FileNotFoundError): os.remove(kwargs["file"].instance.thumbnail["path"]) cleanup_pre_delete.connect(thumbnail_delete)
from django_cleanup.signals import cleanup_pre_delete def delete_versatileimagefield(**kwargs): kwargs["file"].delete_all_created_images() cleanup_pre_delete.connect(delete_versatileimagefield)