Use an absolute url for open graph image

This commit is contained in:
Gabriel Augendre 2020-11-28 20:26:37 +01:00
parent 3b09af8972
commit a3bd932a2d
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
7 changed files with 61 additions and 12 deletions

View file

@ -44,5 +44,10 @@ def plausible(request):
return {"plausible_domain": settings.PLAUSIBLE_DOMAIN} return {"plausible_domain": settings.PLAUSIBLE_DOMAIN}
def open_graph_image(request): def open_graph_image_url(request):
return {"open_graph_image": Attachment.objects.get_open_graph_image()} open_graph_image = Attachment.objects.get_open_graph_image()
return {
"open_graph_image_url": open_graph_image.processed_file.get_full_absolute_url(
request
)
}

View file

@ -1,7 +1,6 @@
import re import re
import markdown import markdown
from django.conf import settings
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import models from django.db import models
@ -12,6 +11,7 @@ from django.utils import timezone
from markdown.extensions.codehilite import CodeHiliteExtension from markdown.extensions.codehilite import CodeHiliteExtension
from articles.markdown import LazyLoadingImageExtension from articles.markdown import LazyLoadingImageExtension
from articles.utils import build_full_absolute_url
class User(AbstractUser): class User(AbstractUser):
@ -67,10 +67,7 @@ class Article(AdminUrlMixin, models.Model):
def get_full_absolute_url(self, request: HttpRequest = None): def get_full_absolute_url(self, request: HttpRequest = None):
url = self.get_absolute_url() url = self.get_absolute_url()
if request: return build_full_absolute_url(request, url)
return request.build_absolute_uri(url)
else:
return (settings.BLOG["base_url"] + url).replace("//", "/")
def get_abstract(self): def get_abstract(self):
html = self.get_formatted_content() html = self.get_formatted_content()

View file

@ -12,8 +12,8 @@
<meta property="og:site_name" content="{{ blog_title }}"> <meta property="og:site_name" content="{{ blog_title }}">
<meta property="og:title" content="{{ article.title }}" /> <meta property="og:title" content="{{ article.title }}" />
{% endif %} {% endif %}
{% if open_graph_image %} {% if open_graph_image_url %}
<meta property="og:image" content="{{ open_graph_image.processed_file.url }}"> <meta property="og:image" content="{{ open_graph_image_url }}">
{% endif %} {% endif %}
<title>{% block title %}Home | {% endblock %}Gab's Notes</title> <title>{% block title %}Home | {% endblock %}Gab's Notes</title>
<link rel="stylesheet" id="code-light" href="{% static 'code-light.css' %}" type="text/css"> <link rel="stylesheet" id="code-light" href="{% static 'code-light.css' %}" type="text/css">

8
articles/utils.py Normal file
View file

@ -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("//", "/")

View file

@ -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=""
),
),
]

View file

@ -6,8 +6,20 @@ import requests
from django.conf import settings from django.conf import settings
from django.core.files import File from django.core.files import File
from django.db import models from django.db import models
from django.db.models.fields.files import FieldFile
from PIL import Image 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): class AttachmentManager(models.Manager):
def get_open_graph_image(self): def get_open_graph_image(self):
@ -16,8 +28,8 @@ class AttachmentManager(models.Manager):
class Attachment(models.Model): class Attachment(models.Model):
description = models.CharField(max_length=500) description = models.CharField(max_length=500)
original_file = models.FileField() original_file = AbsoluteUrlFileField()
processed_file = models.FileField(blank=True, null=True) processed_file = AbsoluteUrlFileField(blank=True, null=True)
open_graph_image = models.BooleanField(blank=True) open_graph_image = models.BooleanField(blank=True)
objects = AttachmentManager() objects = AttachmentManager()

View file

@ -101,7 +101,7 @@ TEMPLATES = [
"articles.context_processors.date_format", "articles.context_processors.date_format",
"articles.context_processors.git_version", "articles.context_processors.git_version",
"articles.context_processors.plausible", "articles.context_processors.plausible",
"articles.context_processors.open_graph_image", "articles.context_processors.open_graph_image_url",
], ],
}, },
}, },