From a3bd932a2dbd966a5fe31f1f3f82a1f96eb3fefc Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 28 Nov 2020 20:26:37 +0100 Subject: [PATCH] Use an absolute url for open graph image --- articles/context_processors.py | 9 +++++-- articles/models.py | 7 ++--- articles/templates/articles/base.html | 4 +-- articles/utils.py | 8 ++++++ .../migrations/0006_auto_20201128_2022.py | 27 +++++++++++++++++++ attachments/models.py | 16 +++++++++-- blog/settings.py | 2 +- 7 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 articles/utils.py create mode 100644 attachments/migrations/0006_auto_20201128_2022.py diff --git a/articles/context_processors.py b/articles/context_processors.py index 57a04ba..cdbf1f4 100644 --- a/articles/context_processors.py +++ b/articles/context_processors.py @@ -44,5 +44,10 @@ def plausible(request): return {"plausible_domain": settings.PLAUSIBLE_DOMAIN} -def open_graph_image(request): - return {"open_graph_image": Attachment.objects.get_open_graph_image()} +def open_graph_image_url(request): + open_graph_image = Attachment.objects.get_open_graph_image() + return { + "open_graph_image_url": open_graph_image.processed_file.get_full_absolute_url( + request + ) + } diff --git a/articles/models.py b/articles/models.py index 51b7eaa..82ee0d7 100644 --- a/articles/models.py +++ b/articles/models.py @@ -1,7 +1,6 @@ import re import markdown -from django.conf import settings from django.contrib.auth.models import AbstractUser from django.contrib.contenttypes.models import ContentType from django.db import models @@ -12,6 +11,7 @@ from django.utils import timezone from markdown.extensions.codehilite import CodeHiliteExtension from articles.markdown import LazyLoadingImageExtension +from articles.utils import build_full_absolute_url class User(AbstractUser): @@ -67,10 +67,7 @@ class Article(AdminUrlMixin, models.Model): def get_full_absolute_url(self, request: HttpRequest = None): url = self.get_absolute_url() - if request: - return request.build_absolute_uri(url) - else: - return (settings.BLOG["base_url"] + url).replace("//", "/") + return build_full_absolute_url(request, url) def get_abstract(self): html = self.get_formatted_content() diff --git a/articles/templates/articles/base.html b/articles/templates/articles/base.html index 46ac1ca..d02fba4 100644 --- a/articles/templates/articles/base.html +++ b/articles/templates/articles/base.html @@ -12,8 +12,8 @@ {% endif %} - {% if open_graph_image %} - + {% if open_graph_image_url %} + {% endif %} {% block title %}Home | {% endblock %}Gab's Notes diff --git a/articles/utils.py b/articles/utils.py new file mode 100644 index 0000000..50d073d --- /dev/null +++ b/articles/utils.py @@ -0,0 +1,8 @@ +from django.conf import settings + + +def build_full_absolute_url(request, url): + if request: + return request.build_absolute_uri(url) + else: + return (settings.BLOG["base_url"] + url).replace("//", "/") diff --git a/attachments/migrations/0006_auto_20201128_2022.py b/attachments/migrations/0006_auto_20201128_2022.py new file mode 100644 index 0000000..ef0b44d --- /dev/null +++ b/attachments/migrations/0006_auto_20201128_2022.py @@ -0,0 +1,27 @@ +# Generated by Django 3.1.1 on 2020-11-28 19:22 + +from django.db import migrations + +import attachments.models + + +class Migration(migrations.Migration): + + dependencies = [ + ("attachments", "0005_attachment_open_graph_image"), + ] + + operations = [ + migrations.AlterField( + model_name="attachment", + name="original_file", + field=attachments.models.AbsoluteUrlFileField(upload_to=""), + ), + migrations.AlterField( + model_name="attachment", + name="processed_file", + field=attachments.models.AbsoluteUrlFileField( + blank=True, null=True, upload_to="" + ), + ), + ] diff --git a/attachments/models.py b/attachments/models.py index 73d7b79..6a30dc1 100644 --- a/attachments/models.py +++ b/attachments/models.py @@ -6,8 +6,20 @@ import requests from django.conf import settings from django.core.files import File from django.db import models +from django.db.models.fields.files import FieldFile from PIL import Image +from articles.utils import build_full_absolute_url + + +class AbsoluteUrlFieldFile(FieldFile): + def get_full_absolute_url(self, request): + return build_full_absolute_url(request, self.url) + + +class AbsoluteUrlFileField(models.FileField): + attr_class = AbsoluteUrlFieldFile + class AttachmentManager(models.Manager): def get_open_graph_image(self): @@ -16,8 +28,8 @@ class AttachmentManager(models.Manager): class Attachment(models.Model): description = models.CharField(max_length=500) - original_file = models.FileField() - processed_file = models.FileField(blank=True, null=True) + original_file = AbsoluteUrlFileField() + processed_file = AbsoluteUrlFileField(blank=True, null=True) open_graph_image = models.BooleanField(blank=True) objects = AttachmentManager() diff --git a/blog/settings.py b/blog/settings.py index 2595b52..df2fb3b 100644 --- a/blog/settings.py +++ b/blog/settings.py @@ -101,7 +101,7 @@ TEMPLATES = [ "articles.context_processors.date_format", "articles.context_processors.git_version", "articles.context_processors.plausible", - "articles.context_processors.open_graph_image", + "articles.context_processors.open_graph_image_url", ], }, },