def test_image_create_compile_error_cached(self): instance = factories.LatexImageErrorFactory() self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "image"))) self.assertIsNotNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error"), instance.compile_error))
def test_image_create_image_url_not_cached(self): instance = factories.LatexImageFactory() self.assertIsNotNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "data_url"), instance.data_url)) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def create_pdf_cache_on_save(sender, instance, **kwargs): # We will cache image and data_url try: import django.core.cache as cache except ImproperlyConfigured: return def_cache = cache.caches["default"] from django.conf import settings serializer = LatexPdfSerializer(instance) data = serializer.to_representation(instance) attr_to_cache = [] if (data["pdf"] and getattr(settings, "L2P_API_PDF_RETURNS_RELATIVE_PATH", True)): # We only cache when pdf relative path are requested in api # because we can't access the request thus no way to know # the url and can't build the pdf url. attr_to_cache.append("pdf") for attr in attr_to_cache: attr_value = data[attr] if (attr_value is not None and len(str(attr_value)) <= getattr( settings, "L2P_CACHE_MAX_BYTES", 0)): def_cache.add( get_field_cache_key(instance.collection.zip_file_hash, attr), attr_value, None)
def create_image_cache_on_save(sender, instance, **kwargs): # We will cache image and data_url try: import django.core.cache as cache except ImproperlyConfigured: return def_cache = cache.caches["default"] from django.conf import settings serializer = LatexImageSerializer(instance) data = serializer.to_representation(instance) attr_to_cache = ["compile_error"] if (data["image"] and getattr( settings, "L2I_API_IMAGE_RETURNS_RELATIVE_PATH", True)): # We only cache when image relative path are requested in api # because we can't access the request thus no way to know # the url and can't build the image url. attr_to_cache.append("image") if getattr(settings, "L2I_CACHE_DATA_URL_ON_SAVE", False): attr_to_cache.append("data_url") for attr in attr_to_cache: attr_value = data[attr] if (attr_value is not None and len(str(attr_value)) <= getattr( settings, "L2I_CACHE_MAX_BYTES", 0)): def_cache.add(get_field_cache_key(instance.tex_key, attr), attr_value, None)
def test_delete_image_compile_error_cache_deleted(self): instance = factories.LatexImageErrorFactory() instance.delete() self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def test_image_create_image_relative_path_cached(self): instance = factories.LatexImageFactory() self.assertIsNotNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "image"))) self.assertEqual(self.test_cache.get( get_field_cache_key(instance.tex_key, "image")), str(instance.image)) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "data_url"))) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def test_delete_image_cache_deleted(self): instance = factories.LatexImageFactory() serializer = LatexImageSerializer(instance) data = serializer.to_representation(instance) creator_cache_key = get_field_cache_key(instance.tex_key, "creator") self.test_cache.add(creator_cache_key, data["creator"]) self.assertEqual(self.test_cache.get(creator_cache_key), data["creator"]) creation_time_cache_key = get_field_cache_key( instance.tex_key, "creation_time") self.test_cache.add(creation_time_cache_key, data["creation_time"]) self.assertEqual( self.test_cache.get(creation_time_cache_key), data["creation_time"]) instance.delete() self.assertIsNone(self.test_cache.get(creation_time_cache_key)) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def pdf_delete(sender, instance, **kwargs): """ Delete the associated image when the instance is deleted. """ # “false” to instance.image.delete ensures that ImageField # does not save the model # post_delete signal is sent at the end of a model’s delete() # method and a queryset’s delete() method. # This is safer as it does not execute unless the parent object # is successfully deleted. instance.pdf.delete(False) try: import django.core.cache as cache except ImproperlyConfigured: return def_cache = cache.caches["default"] for attr in ("pdf", ): def_cache.delete(get_field_cache_key(instance.collection.zip_file_hash, attr))