From 69719b9d2696251bf6a21c2e2293601bd0efeb81 Mon Sep 17 00:00:00 2001 From: Bobby Date: Fri, 10 Mar 2023 00:39:59 -0500 Subject: Optimize image views for older browsers --- blog/context_processors.py | 1 - blog_admin/views.py | 1 - ignis/views.py | 33 ++++++++++------------ templates/blog/partials/base.html | 2 +- templates/blog/partials/mathjax.html | 54 ++++++++++++------------------------ templates/blog/post.html | 2 +- 6 files changed, 35 insertions(+), 58 deletions(-) diff --git a/blog/context_processors.py b/blog/context_processors.py index 895bdb7c..53a16417 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -60,7 +60,6 @@ def highlight_code_blocks(code_block): cb = code_block.string except: cb = code_block - print(cb) cb = cb.replace(u'\xa0', u' ') # guess the language as there is no data-lang attribute diff --git a/blog_admin/views.py b/blog_admin/views.py index 68447179..29b1f0aa 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -74,7 +74,6 @@ def new_post(request): post.save() return redirect(reverse('blog-admin:edit-post', kwargs = { 'slug': post.slug })) except Exception as e: - print(e) messages.error(request, 'Some error occured while creating post.', extra_tags='error') return redirect('blog-admin:posts') diff --git a/ignis/views.py b/ignis/views.py index b198b4f5..1b8b6be1 100644 --- a/ignis/views.py +++ b/ignis/views.py @@ -69,21 +69,20 @@ def post_image(request, size, post_id): @csrf_exempt def get_image(request, post_id, image_name): - if 'rpi_' in post_id: - # post has random post identifier => means it's a new post and not saved yet - # get image from temp_post_id - pi = PostImage.objects.filter(temp_post_id=post_id, name=image_name) - else: - # post has id => means it's an existing post - # get image from post_id - pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image_name) + # get image from post_id + pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image_name) if not pi: return HttpResponse('No image found!', status=404) # open image and return image = pi[0].image with open(image.path, 'rb') as f: - return HttpResponse(f.read(), content_type='image/{}'.format(image.name.split('.')[-1])) + image_file = f.read() + # convert to gif + image = Image.open(BytesIO(image_file)) + output = BytesIO() + image.save(output, format='GIF') + return HttpResponse(output.getvalue(), content_type='image/gif') @csrf_exempt def cover_image(request, repository): @@ -99,7 +98,6 @@ def cover_image(request, repository): # image is not in RepositoryTitles # get image url = 'https://socialify.thatcomputerscientist.com/luciferreeves/{}/png?font=KoHo&language=1&language2=1&name=1&theme=Dark&pattern=Solid'.format(repository) - print("Getting image for repository: {}".format(repository)) image = requests.get(url).content # reduce image size to 320x160 @@ -142,14 +140,13 @@ def upload_image(request): # upload image to PostImage model image = request.FILES['image'] post_id = request.POST['id'] - if 'rpi_' in post_id: - # post has random post identifier => means it's a new post and not saved yet - # save image to temp_post_id - pi = PostImage(image=image, temp_post_id=post_id, post=None, name=image.name) - else: - # post has id => means it's an existing post - # save image to post_id - pi = PostImage(image=image, post=Post.objects.get(id=post_id), name=image.name) + # check if image already exists + pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image.name) + if pi: + # image already exists, delete it + pi[0].delete() + # save image to post_id + pi = PostImage(image=image, post=Post.objects.get(id=post_id), name=image.name) pi.save() response = { 'url': '/ignis/image/{}/{}'.format(post_id, pi.name) diff --git a/templates/blog/partials/base.html b/templates/blog/partials/base.html index 2f395b1b..a04eae3c 100644 --- a/templates/blog/partials/base.html +++ b/templates/blog/partials/base.html @@ -49,7 +49,7 @@ Ad {% endfor %}
-
+

© {% now "Y" %} That Computer Scientist. Source code available on GitHub diff --git a/templates/blog/partials/mathjax.html b/templates/blog/partials/mathjax.html index f580c4f5..04f18104 100644 --- a/templates/blog/partials/mathjax.html +++ b/templates/blog/partials/mathjax.html @@ -1,39 +1,21 @@ {% load static %} - + \ No newline at end of file diff --git a/templates/blog/post.html b/templates/blog/post.html index 2cd87644..b43e0a89 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -113,7 +113,6 @@

You must be logged in to leave a comment.

{% endif %} -{% include 'blog/partials/mathjax.html' %} +{% include 'blog/partials/mathjax.html' %} {% endblock %} -- cgit v1.2.3