1
Fork 0

Collect media files together for easy serving

This commit is contained in:
Jake Howard 2024-10-11 17:32:46 +01:00
parent 9c87212746
commit caed8daeda
Signed by: jake
GPG key ID: 57AFB45680EDD477
9 changed files with 40 additions and 1 deletions

2
.gitignore vendored
View file

@ -176,3 +176,5 @@ pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python # End of https://www.toptal.com/developers/gitignore/api/python
cache/ cache/
/collected-content-media

View file

@ -0,0 +1 @@
Imagine this is an image.

View file

@ -3,6 +3,7 @@ title: Test
tags: tags:
- tag-1 - tag-1
runtime_render: false runtime_render: false
media: ./media.txt
--- ---
# Heading # Heading

View file

@ -1,5 +1,5 @@
django django
git+https://github.com/andrewgodwin/yamdl@8d34d7397f3564a550042e81ea79cca7d2f3777e git+https://github.com/andrewgodwin/yamdl@237d3607a8e611c723ee45a7b1726c00b1648451
gunicorn gunicorn
markdown markdown
chrono chrono

View file

@ -4,6 +4,8 @@ from django.db.models.signals import post_save
import warnings import warnings
from django.utils.html import strip_tags from django.utils.html import strip_tags
from django.core.management import call_command from django.core.management import call_command
from django.core.files.storage import storages
import shutil
class CoreConfig(AppConfig): class CoreConfig(AppConfig):
@ -15,6 +17,9 @@ class CoreConfig(AppConfig):
from .models import Page from .models import Page
if not self.loaded: if not self.loaded:
# Clear storage to ensure files are always fresh
shutil.rmtree(storages["yamdl"].location, ignore_errors=True)
connection = connections["yamdl"] connection = connections["yamdl"]
with warnings.catch_warnings(): with warnings.catch_warnings():

View file

@ -2,6 +2,11 @@ from django.db import models
import markdown import markdown
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.template import Template, Context 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): class Tag(models.Model):
__yamdl__ = True __yamdl__ = True
@ -11,6 +16,8 @@ class Tag(models.Model):
content = models.TextField() 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): class Page(models.Model):
__yamdl__ = True __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) slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True)
file_path = models.CharField(max_length=255) 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) tags = models.ManyToManyField(Tag)
@classmethod @classmethod
@ -44,6 +53,11 @@ class Page(models.Model):
"tokens": md.toc_tokens "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) instance = cls(**data)
if not instance.runtime_render: if not instance.runtime_render:

View file

@ -144,6 +144,22 @@ USE_TZ = True
STATIC_URL = 'static/' 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 # Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field