def handle(self, *args, **options): total_count = WebPImage.objects.all().count() for qs in webp_image_querysets(total_count): for webp_image in qs: key = make_image_key(webp_image.static_path, webp_image.quality) cache.delete(key) WebPImage.objects.all().delete() delete_webp_folder() self.stdout.write('Successfully cleared cache')
def test_clear_cache(self): static_path = "images/django-test-image.png" quality = 80 key = make_image_key(static_path, quality) cache.set(key, "test") assert cache.get(key) == "test" WebPImage.objects.create(static_path=static_path, quality=quality) assert WebPImage.objects.all().count() == 1 call_command("clear_webp_cache", "--no-input") assert cache.get(key) is None assert WebPImage.objects.all().count() == 0
def handle(self, *args, **options): if options['interactive']: confirm_message = ( 'This will delete all generated WebP images.\n\n' 'Are you sure you want to do this?\n\n' "Type 'yes' to continue, or 'no' to cancel: " ) if input(confirm_message) != 'yes': raise CommandError("Clearing WebP cache cancelled.") self.stdout.write("Deleting cache entries, WebP images and WebP models...") for webp_image in WebPImage.objects.all().iterator(): key = make_image_key(webp_image.static_path, webp_image.quality) cache.delete(key) default_storage.delete(webp_image.webp_relative_path) webp_image.delete() self.stdout.write("Successfully cleared cache")
def handle(self, *args, **options): webp_path = default_storage.path(WEBP_CONVERTER_PREFIX) if options['interactive']: confirm_message = ( 'This will delete all files in {webp_path}\n\n' 'Are you sure you want to do this?\n\n' "Type 'yes' to continue, or 'no' to cancel: ").format( webp_path=webp_path) if input(confirm_message) != 'yes': raise CommandError("Clearing webp cache cancelled.") self.stdout.write("Deleting cache entries...") for webp_image in WebPImage.objects.all().iterator(): key = make_image_key(webp_image.static_path, webp_image.quality) cache.delete(key) self.stdout.write("Deleting WebPImage models...") WebPImage.objects.all().delete() self.stdout.write("Deleting images...") if os.path.exists(webp_path): shutil.rmtree(webp_path) self.stdout.write("Successfully cleared cache")
def test_make_image_key(self): key = utils.make_image_key("static/path/image.jpg", 60) self.assertEqual( key, webp_settings.WEBP_CONVERTER_CACHE_PREFIX + ":3eac052ec0eaf68475576e75e2305f2b")
def test_clear_cache(self): static_path = 'django-test-image.png' quality = 80 key = make_image_key(static_path, quality) cache.set(key, 'test') assert cache.get(key) == 'test' WebPImage.objects.create( static_path=static_path, quality=quality) assert WebPImage.objects.all().count() == 1 call_command('clear_webp_cache') assert cache.get(key) is None assert WebPImage.objects.all().count() == 0
def static_webp(context, static_path, quality=None): try: webp_compatible = context['webp_compatible'] except KeyError: raise Exception("'webp_converter.context_processors.webp_support' " "needs to be added to your context processors.") if not webp_compatible: return static(static_path) key = make_image_key(static_path, quality) webp_image_url = cache.get(key) if not webp_image_url: webp_image, _ = WebPImage.objects.get_or_create( static_path=static_path, quality=quality) webp_image.save_image() webp_image_url = webp_image.url cache.set(key, webp_image_url) return webp_image_url
def test_make_image_key(self): key = utils.make_image_key('static/path/image.jpg', 60) assert key == webp_settings.WEBP_CONVERTER_CACHE_PREFIX +\ ':3eac052ec0eaf68475576e75e2305f2b'