From caed8daedaeb98c09ecb3a2e69f5018c89e742a1 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 11 Oct 2024 17:32:46 +0100 Subject: [PATCH] Collect media files together for easy serving --- .gitignore | 2 ++ content/{core.page => core.Page}/test.yaml | 0 content/core.Page/testdir/media.txt | 1 + .../{core.page => core.Page/testdir}/test2.md | 1 + content/{core.tag => core.Tag}/tags.yml | 0 requirements.txt | 2 +- yamdl_playground/core/apps.py | 5 +++++ yamdl_playground/core/models.py | 14 ++++++++++++++ yamdl_playground/settings.py | 16 ++++++++++++++++ 9 files changed, 40 insertions(+), 1 deletion(-) rename content/{core.page => core.Page}/test.yaml (100%) create mode 100644 content/core.Page/testdir/media.txt rename content/{core.page => core.Page/testdir}/test2.md (86%) rename content/{core.tag => core.Tag}/tags.yml (100%) diff --git a/.gitignore b/.gitignore index dfb652b..cb3f739 100644 --- a/.gitignore +++ b/.gitignore @@ -176,3 +176,5 @@ pyrightconfig.json # End of https://www.toptal.com/developers/gitignore/api/python cache/ + +/collected-content-media diff --git a/content/core.page/test.yaml b/content/core.Page/test.yaml similarity index 100% rename from content/core.page/test.yaml rename to content/core.Page/test.yaml diff --git a/content/core.Page/testdir/media.txt b/content/core.Page/testdir/media.txt new file mode 100644 index 0000000..02258db --- /dev/null +++ b/content/core.Page/testdir/media.txt @@ -0,0 +1 @@ +Imagine this is an image. diff --git a/content/core.page/test2.md b/content/core.Page/testdir/test2.md similarity index 86% rename from content/core.page/test2.md rename to content/core.Page/testdir/test2.md index 00f9c82..3038be5 100644 --- a/content/core.page/test2.md +++ b/content/core.Page/testdir/test2.md @@ -3,6 +3,7 @@ title: Test tags: - tag-1 runtime_render: false +media: ./media.txt --- # Heading diff --git a/content/core.tag/tags.yml b/content/core.Tag/tags.yml similarity index 100% rename from content/core.tag/tags.yml rename to content/core.Tag/tags.yml diff --git a/requirements.txt b/requirements.txt index d055c3e..4880143 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ django -git+https://github.com/andrewgodwin/yamdl@8d34d7397f3564a550042e81ea79cca7d2f3777e +git+https://github.com/andrewgodwin/yamdl@237d3607a8e611c723ee45a7b1726c00b1648451 gunicorn markdown chrono diff --git a/yamdl_playground/core/apps.py b/yamdl_playground/core/apps.py index 8200b13..9f64914 100644 --- a/yamdl_playground/core/apps.py +++ b/yamdl_playground/core/apps.py @@ -4,6 +4,8 @@ from django.db.models.signals import post_save import warnings from django.utils.html import strip_tags from django.core.management import call_command +from django.core.files.storage import storages +import shutil class CoreConfig(AppConfig): @@ -15,6 +17,9 @@ class CoreConfig(AppConfig): from .models import Page if not self.loaded: + # Clear storage to ensure files are always fresh + shutil.rmtree(storages["yamdl"].location, ignore_errors=True) + connection = connections["yamdl"] with warnings.catch_warnings(): diff --git a/yamdl_playground/core/models.py b/yamdl_playground/core/models.py index 1c40d8e..86116a3 100644 --- a/yamdl_playground/core/models.py +++ b/yamdl_playground/core/models.py @@ -2,6 +2,11 @@ from django.db import models import markdown from django.utils.functional import cached_property from django.template import Template, Context +from django.core.files.storage import storages +from django.core.files import File +from pathlib import Path +import os.path + class Tag(models.Model): __yamdl__ = True @@ -11,6 +16,8 @@ class Tag(models.Model): content = models.TextField() +def get_page_upload_to(instance, filename): + return f"{instance._meta.verbose_name}/{instance.slug}/{os.path.basename(filename)}" class Page(models.Model): __yamdl__ = True @@ -26,6 +33,8 @@ class Page(models.Model): slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True) file_path = models.CharField(max_length=255) + media = models.FileField(null=True, default=None, storage=storages["yamdl"], upload_to=get_page_upload_to) + tags = models.ManyToManyField(Tag) @classmethod @@ -44,6 +53,11 @@ class Page(models.Model): "tokens": md.toc_tokens } + if data.get("media"): + source_dir = Path(data["file_path"]).parent + + data["media"] = File((source_dir / data["media"]).open("rb")) + instance = cls(**data) if not instance.runtime_render: diff --git a/yamdl_playground/settings.py b/yamdl_playground/settings.py index 4dc00e4..6a659ca 100644 --- a/yamdl_playground/settings.py +++ b/yamdl_playground/settings.py @@ -144,6 +144,22 @@ USE_TZ = True STATIC_URL = 'static/' +STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + "staticfiles": { + "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", + }, + "yamdl": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + "OPTIONS": { + "location": BASE_DIR / "collected-content-media", + "base_url": "/yamdl-media/" + } + } +} + # Default primary key field type # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field