Add keywords & meta tags

This commit is contained in:
Gabriel Augendre 2020-11-28 20:09:37 +01:00
parent 6f42238a21
commit bd76050940
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
10 changed files with 81 additions and 5 deletions

View file

@ -31,7 +31,7 @@ class ArticleAdmin(admin.ModelAdmin):
{ {
"fields": [ "fields": [
("title", "slug"), ("title", "slug"),
("author",), ("author", "keywords"),
("status", "published_at"), ("status", "published_at"),
("created_at", "updated_at"), ("created_at", "updated_at"),
"views_count", "views_count",

View file

@ -1,6 +1,7 @@
from django.conf import settings from django.conf import settings
from articles.models import Article, Page from articles.models import Article, Page
from attachments.models import Attachment
IGNORED_PATHS = [ IGNORED_PATHS = [
"/robots.txt", "/robots.txt",
@ -41,3 +42,7 @@ def git_version(request):
def plausible(request): def plausible(request):
return {"plausible_domain": settings.PLAUSIBLE_DOMAIN} return {"plausible_domain": settings.PLAUSIBLE_DOMAIN}
def open_graph_image(request):
return {"open_graph_image": Attachment.objects.get_open_graph_image()}

View file

@ -0,0 +1,18 @@
# Generated by Django 3.1.1 on 2020-11-28 18:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("articles", "0021_auto_20201110_1623"),
]
operations = [
migrations.AddField(
model_name="article",
name="keywords",
field=models.CharField(blank=True, max_length=255),
),
]

View file

@ -48,6 +48,7 @@ class Article(AdminUrlMixin, models.Model):
author = models.ForeignKey(User, on_delete=models.PROTECT, default=1) author = models.ForeignKey(User, on_delete=models.PROTECT, default=1)
views_count = models.IntegerField(default=0) views_count = models.IntegerField(default=0)
slug = models.SlugField(unique=True, max_length=255) slug = models.SlugField(unique=True, max_length=255)
keywords = models.CharField(max_length=255, blank=True)
objects = models.Manager() objects = models.Manager()
without_pages = ArticleManager() without_pages = ArticleManager()

View file

@ -34,9 +34,9 @@
</div> </div>
</nav> </nav>
<section class="index-page"> <section class="index-page">
<h2>{{ index_page.title }}</h2> <h2>{{ article.title }}</h2>
<div> <div>
{{ index_page.get_formatted_content|safe }} {{ article.get_formatted_content|safe }}
</div> </div>
</section> </section>
{% endblock %} {% endblock %}

View file

@ -4,6 +4,17 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Gabriel Augendre">
<meta name="color-scheme" content="light dark">
{% if article %}
<meta name="keywords" content="{{ article.keywords }}">
<meta property="og:type" content="article">
<meta property="og:site_name" content="{{ blog_title }}">
<meta property="og:title" content="{{ article.title }}" />
{% endif %}
{% if open_graph_image %}
<meta name="og:image" content="{{ open_graph_image.processed_file.url }}">
{% 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">
<link rel="stylesheet" id="code-dark" href="{% static 'code-dark.css' %}" type="text/css"> <link rel="stylesheet" id="code-dark" href="{% static 'code-dark.css' %}" type="text/css">

View file

@ -28,7 +28,7 @@ class ArticlesListView(BaseArticleListView):
index_page = Page.objects.filter( index_page = Page.objects.filter(
status=Article.PUBLISHED, position=0 status=Article.PUBLISHED, position=0
).first() # type: Page ).first() # type: Page
context["index_page"] = index_page context["article"] = index_page
return context return context

View file

@ -1,4 +1,4 @@
from django.contrib import admin from django.contrib import admin, messages
from django.contrib.admin import register from django.contrib.admin import register
from django.utils.html import format_html from django.utils.html import format_html
@ -13,6 +13,7 @@ class AttachmentAdmin(admin.ModelAdmin):
"original_file_url", "original_file_url",
"processed_file", "processed_file",
"processed_file_url", "processed_file_url",
"open_graph_image",
] ]
list_display_links = ["description"] list_display_links = ["description"]
fields = [ fields = [
@ -21,11 +22,14 @@ class AttachmentAdmin(admin.ModelAdmin):
"original_file_url", "original_file_url",
"processed_file", "processed_file",
"processed_file_url", "processed_file_url",
"open_graph_image",
] ]
readonly_fields = [ readonly_fields = [
"original_file_url", "original_file_url",
"processed_file_url", "processed_file_url",
"open_graph_image",
] ]
actions = ["set_as_open_graph_image"]
class Media: class Media:
js = ["attachments/js/copy_url.js"] js = ["attachments/js/copy_url.js"]
@ -47,3 +51,13 @@ class AttachmentAdmin(admin.ModelAdmin):
instance.original_file.url, instance.original_file.url,
) )
return "" return ""
def set_as_open_graph_image(self, request, queryset):
if len(queryset) != 1:
messages.error(request, "You must select only one attachment")
return
Attachment.objects.update(open_graph_image=False)
queryset.update(open_graph_image=True)
messages.success(request, "Done")
set_as_open_graph_image.short_description = "Set as open graph image"

View file

@ -0,0 +1,19 @@
# Generated by Django 3.1.1 on 2020-11-28 18:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("attachments", "0004_auto_20200903_2116"),
]
operations = [
migrations.AddField(
model_name="attachment",
name="open_graph_image",
field=models.BooleanField(blank=True, default=False),
preserve_default=False,
),
]

View file

@ -9,10 +9,18 @@ from django.db import models
from PIL import Image from PIL import Image
class AttachmentManager(models.Manager):
def get_open_graph_image(self):
return self.filter(open_graph_image=True).first()
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 = models.FileField()
processed_file = models.FileField(blank=True, null=True) processed_file = models.FileField(blank=True, null=True)
open_graph_image = models.BooleanField(blank=True)
objects = AttachmentManager()
class Meta: class Meta:
ordering = ["description"] ordering = ["description"]