Skip to content

Commit

Permalink
Merge pull request #70 from 2i2c-org/red
Browse files Browse the repository at this point in the history
Add a redirect for content files
  • Loading branch information
yuvipanda authored Sep 23, 2024
2 parents 0b2ab87 + 73a5f90 commit 64746d3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
22 changes: 22 additions & 0 deletions comptest/web/migrations/0017_alter_contentfile_slug.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
2 changes: 1 addition & 1 deletion comptest/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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"
max_length=128, help_text="Slug used to refer to this image", unique=True
)
file = models.FileField(upload_to="content-files/%Y/%m/%d/")

Expand Down
1 change: 1 addition & 0 deletions comptest/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
path("teams/<int:id>", teams.view, name="teams-view"),
path("teams/<int:id>/add-member", teams.add_member, name="teams-add-member"),
path("page/<slug:slug>", pages.view, name="page-view"),
path("file/<slug:slug>", pages.content_file, name="content-file"),
path("leaderboard", default.leaderboard, name="leaderboard"),
path("", pages.home, name="home"),
]
15 changes: 13 additions & 2 deletions comptest/web/views/pages.py
Original file line number Diff line number Diff line change
@@ -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 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
from ..models import ContentFile, Page


def view(request: HttpRequest, slug: str) -> HttpResponse:
Expand Down Expand Up @@ -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)

0 comments on commit 64746d3

Please sign in to comment.