From 8502933a71bd327d834129077f2dc948013c2b48 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Mon, 23 Sep 2024 17:42:20 +0530 Subject: [PATCH 1/2] Add a redirect for content files Allows us to not have to hunt and replace file references when we make a new upload. Ref https://github.com/2i2c-org/unnamed-thingity-thing/issues/59 --- .../migrations/0017_alter_contentfile_slug.py | 22 +++++++++++++++++++ comptest/web/models.py | 3 ++- comptest/web/urls.py | 1 + comptest/web/views/pages.py | 15 +++++++++++-- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 comptest/web/migrations/0017_alter_contentfile_slug.py diff --git a/comptest/web/migrations/0017_alter_contentfile_slug.py b/comptest/web/migrations/0017_alter_contentfile_slug.py new file mode 100644 index 0000000..f2fcf3a --- /dev/null +++ b/comptest/web/migrations/0017_alter_contentfile_slug.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.7 on 2024-09-23 12:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("web", "0016_alter_page_mimetype"), + ] + + operations = [ + migrations.AlterField( + model_name="contentfile", + name="slug", + field=models.SlugField( + help_text="Slug used to refer to this image", + max_length=128, + unique=True, + ), + ), + ] diff --git a/comptest/web/models.py b/comptest/web/models.py index 021a307..40b9f02 100644 --- a/comptest/web/models.py +++ b/comptest/web/models.py @@ -116,7 +116,8 @@ class ContentFile(models.Model): """ title = models.CharField(max_length=1024) slug = models.SlugField( - max_length=128, help_text="Slug used to refer to this image" + max_length=128, help_text="Slug used to refer to this image", + unique=True ) file = models.FileField(upload_to="content-files/%Y/%m/%d/") diff --git a/comptest/web/urls.py b/comptest/web/urls.py index 551b821..efc0da8 100644 --- a/comptest/web/urls.py +++ b/comptest/web/urls.py @@ -10,6 +10,7 @@ path("teams/", teams.view, name="teams-view"), path("teams//add-member", teams.add_member, name="teams-add-member"), path("page/", pages.view, name="page-view"), + path("file/", pages.content_file, name="content-file"), path("leaderboard", default.leaderboard, name="leaderboard"), path("", pages.home, name="home"), ] diff --git a/comptest/web/views/pages.py b/comptest/web/views/pages.py index d576106..2c9dfe7 100644 --- a/comptest/web/views/pages.py +++ b/comptest/web/views/pages.py @@ -1,11 +1,11 @@ from django.conf import settings from django.http import Http404, HttpRequest, HttpResponse -from django.shortcuts import render +from django.shortcuts import render, redirect from markdown_it import MarkdownIt from mdit_py_plugins.footnote import footnote_plugin from mdit_py_plugins.front_matter import front_matter_plugin -from ..models import Page +from ..models import Page, ContentFile def view(request: HttpRequest, slug: str) -> HttpResponse: @@ -48,3 +48,14 @@ def home(request: HttpRequest) -> HttpResponse: "challenge_state": settings.CHALLENGE_STATE, } return render(request, "page/view.html", context) + + +def content_file(request: HttpRequest, slug: str) -> HttpResponse: + """ + Redirect to file with given slug + """ + try: + cf = ContentFile.objects.get(slug=slug) + except Page.DoesNotExist: + return Http404() + return redirect(cf.file.url) From 73a5f90f553b70b387c3c4d86b4182f1a86a7270 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:13:21 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comptest/web/models.py | 3 +-- comptest/web/views/pages.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/comptest/web/models.py b/comptest/web/models.py index 40b9f02..db218d1 100644 --- a/comptest/web/models.py +++ b/comptest/web/models.py @@ -116,8 +116,7 @@ class ContentFile(models.Model): """ title = models.CharField(max_length=1024) slug = models.SlugField( - max_length=128, help_text="Slug used to refer to this image", - unique=True + max_length=128, help_text="Slug used to refer to this image", unique=True ) file = models.FileField(upload_to="content-files/%Y/%m/%d/") diff --git a/comptest/web/views/pages.py b/comptest/web/views/pages.py index 2c9dfe7..eb777bd 100644 --- a/comptest/web/views/pages.py +++ b/comptest/web/views/pages.py @@ -1,11 +1,11 @@ from django.conf import settings from django.http import Http404, HttpRequest, HttpResponse -from django.shortcuts import render, redirect +from django.shortcuts import redirect, render from markdown_it import MarkdownIt from mdit_py_plugins.footnote import footnote_plugin from mdit_py_plugins.front_matter import front_matter_plugin -from ..models import Page, ContentFile +from ..models import ContentFile, Page def view(request: HttpRequest, slug: str) -> HttpResponse: