From bd76050940e66e293405c04d0a2240eef448f1b8 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 28 Nov 2020 20:09:37 +0100 Subject: [PATCH] Add keywords & meta tags --- articles/admin.py | 2 +- articles/context_processors.py | 5 +++++ articles/migrations/0022_article_keywords.py | 18 ++++++++++++++++++ articles/models.py | 1 + articles/templates/articles/article_list.html | 4 ++-- articles/templates/articles/base.html | 11 +++++++++++ articles/views/html.py | 2 +- attachments/admin.py | 16 +++++++++++++++- .../0005_attachment_open_graph_image.py | 19 +++++++++++++++++++ attachments/models.py | 8 ++++++++ 10 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 articles/migrations/0022_article_keywords.py create mode 100644 attachments/migrations/0005_attachment_open_graph_image.py diff --git a/articles/admin.py b/articles/admin.py index f60b091..d224a0d 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -31,7 +31,7 @@ class ArticleAdmin(admin.ModelAdmin): { "fields": [ ("title", "slug"), - ("author",), + ("author", "keywords"), ("status", "published_at"), ("created_at", "updated_at"), "views_count", diff --git a/articles/context_processors.py b/articles/context_processors.py index 022bde3..57a04ba 100644 --- a/articles/context_processors.py +++ b/articles/context_processors.py @@ -1,6 +1,7 @@ from django.conf import settings from articles.models import Article, Page +from attachments.models import Attachment IGNORED_PATHS = [ "/robots.txt", @@ -41,3 +42,7 @@ def git_version(request): def plausible(request): return {"plausible_domain": settings.PLAUSIBLE_DOMAIN} + + +def open_graph_image(request): + return {"open_graph_image": Attachment.objects.get_open_graph_image()} diff --git a/articles/migrations/0022_article_keywords.py b/articles/migrations/0022_article_keywords.py new file mode 100644 index 0000000..96316d7 --- /dev/null +++ b/articles/migrations/0022_article_keywords.py @@ -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), + ), + ] diff --git a/articles/models.py b/articles/models.py index fc178af..51b7eaa 100644 --- a/articles/models.py +++ b/articles/models.py @@ -48,6 +48,7 @@ class Article(AdminUrlMixin, models.Model): author = models.ForeignKey(User, on_delete=models.PROTECT, default=1) views_count = models.IntegerField(default=0) slug = models.SlugField(unique=True, max_length=255) + keywords = models.CharField(max_length=255, blank=True) objects = models.Manager() without_pages = ArticleManager() diff --git a/articles/templates/articles/article_list.html b/articles/templates/articles/article_list.html index ccde986..e34e829 100644 --- a/articles/templates/articles/article_list.html +++ b/articles/templates/articles/article_list.html @@ -34,9 +34,9 @@
-

{{ index_page.title }}

+

{{ article.title }}

- {{ index_page.get_formatted_content|safe }} + {{ article.get_formatted_content|safe }}
{% endblock %} diff --git a/articles/templates/articles/base.html b/articles/templates/articles/base.html index 73d04e0..5dda22c 100644 --- a/articles/templates/articles/base.html +++ b/articles/templates/articles/base.html @@ -4,6 +4,17 @@ + + + {% if article %} + + + + + {% endif %} + {% if open_graph_image %} + + {% endif %} {% block title %}Home | {% endblock %}Gab's Notes diff --git a/articles/views/html.py b/articles/views/html.py index 12d4764..a35edd2 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -28,7 +28,7 @@ class ArticlesListView(BaseArticleListView): index_page = Page.objects.filter( status=Article.PUBLISHED, position=0 ).first() # type: Page - context["index_page"] = index_page + context["article"] = index_page return context diff --git a/attachments/admin.py b/attachments/admin.py index ccc189d..5125a60 100644 --- a/attachments/admin.py +++ b/attachments/admin.py @@ -1,4 +1,4 @@ -from django.contrib import admin +from django.contrib import admin, messages from django.contrib.admin import register from django.utils.html import format_html @@ -13,6 +13,7 @@ class AttachmentAdmin(admin.ModelAdmin): "original_file_url", "processed_file", "processed_file_url", + "open_graph_image", ] list_display_links = ["description"] fields = [ @@ -21,11 +22,14 @@ class AttachmentAdmin(admin.ModelAdmin): "original_file_url", "processed_file", "processed_file_url", + "open_graph_image", ] readonly_fields = [ "original_file_url", "processed_file_url", + "open_graph_image", ] + actions = ["set_as_open_graph_image"] class Media: js = ["attachments/js/copy_url.js"] @@ -47,3 +51,13 @@ class AttachmentAdmin(admin.ModelAdmin): instance.original_file.url, ) 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" diff --git a/attachments/migrations/0005_attachment_open_graph_image.py b/attachments/migrations/0005_attachment_open_graph_image.py new file mode 100644 index 0000000..2869b04 --- /dev/null +++ b/attachments/migrations/0005_attachment_open_graph_image.py @@ -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, + ), + ] diff --git a/attachments/models.py b/attachments/models.py index 89cea32..73d7b79 100644 --- a/attachments/models.py +++ b/attachments/models.py @@ -9,10 +9,18 @@ from django.db import models 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): description = models.CharField(max_length=500) original_file = models.FileField() processed_file = models.FileField(blank=True, null=True) + open_graph_image = models.BooleanField(blank=True) + + objects = AttachmentManager() class Meta: ordering = ["description"]