From 67dfd04bbbd9d7c6f23c53b15d3e031d05224e25 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 12 Dec 2023 18:14:34 +0000 Subject: [PATCH] Store page slug --- yamdl_playground/core/models.py | 1 + yamdl_playground/core/utils.py | 6 ++++++ yamdl_playground/core/views.py | 4 ++-- yamdl_playground/settings.py | 2 ++ yamdl_playground/urls.py | 2 +- 5 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 yamdl_playground/core/utils.py diff --git a/yamdl_playground/core/models.py b/yamdl_playground/core/models.py index 2a5904a..80807c1 100644 --- a/yamdl_playground/core/models.py +++ b/yamdl_playground/core/models.py @@ -4,5 +4,6 @@ class Page(models.Model): __yamdl__ = True title = models.CharField(max_length=255) + slug = models.CharField(max_length=128, unique=True, db_index=True) content = models.TextField() diff --git a/yamdl_playground/core/utils.py b/yamdl_playground/core/utils.py new file mode 100644 index 0000000..9c8399d --- /dev/null +++ b/yamdl_playground/core/utils.py @@ -0,0 +1,6 @@ +from yamdl.loader import ModelLoader + +class CustomYamdlLoader(ModelLoader): + def load_fixture(self, model_class, data, file_path): + data["slug"] = file_path.stem + super().load_fixture(model_class, data, file_path) diff --git a/yamdl_playground/core/views.py b/yamdl_playground/core/views.py index dfd0b76..34ccc14 100644 --- a/yamdl_playground/core/views.py +++ b/yamdl_playground/core/views.py @@ -11,7 +11,7 @@ def search(request): pages = Page.objects.filter(id__in=row) return HttpResponse(str(pages), content_type="text/plain") -def content(request, id): - page = get_object_or_404(Page, id=id) +def content(request, slug): + page = get_object_or_404(Page, slug=slug) return HttpResponse(markdown.markdown(page.content), content_type="text/html") diff --git a/yamdl_playground/settings.py b/yamdl_playground/settings.py index c6cd25b..4f471bf 100644 --- a/yamdl_playground/settings.py +++ b/yamdl_playground/settings.py @@ -80,6 +80,8 @@ YAMDL_DIRECTORIES = [ BASE_DIR / "content", ] +YAMDL_LOADER = "yamdl_playground.core.utils.CustomYamdlLoader" + # Password validation # https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators diff --git a/yamdl_playground/urls.py b/yamdl_playground/urls.py index ee13129..9e56f8e 100644 --- a/yamdl_playground/urls.py +++ b/yamdl_playground/urls.py @@ -19,5 +19,5 @@ from .core import views urlpatterns = [ path("search/", views.search), - path("content//", views.content) + path("content//", views.content) ]