Skip to content

Commit

Permalink
Add basic redirect for legacy print URLs (refs #442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Almad committed Mar 9, 2024
1 parent 65b5cb3 commit f6dd684
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
62 changes: 50 additions & 12 deletions ddcz/tests/test_integration/test_legacy_redirects.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
from unittest import TestCase

from django.template import Context, Template
from django.test import Client
from django.test import Client, TestCase

from ddcz.models import ApprovalChoices, CommonArticle, RangerSpell, Skill, Quest

from ddcz.models import Quest

class RedirectTestCase(TestCase):
fixtures = ["pages"]

class TestDobrodruztviRedirect(TestCase):
def setUp(self):
self.quest = Quest.objects.create(
super().setUp()
self.create_creation()
self.client = Client()

def assert_redirect(self, from_url, to_url_prefix):
response = self.client.get(from_url)
self.assertEquals(response.status_code, 301)
self.assertEquals(
response.url,
f"{to_url_prefix}/{self.creation.id}-{self.creation.get_slug()}/",
)


class TestQuestRedirect(RedirectTestCase):
def create_creation(self):
self.creation = Quest.objects.create(
id=21,
name="Test quest",
abstract="yolo",
keywords="test, quest",
)
self.client = Client()

def test_dobrodruzstvi_without_suffix(self):
response = self.client.get(f"/dobrodruzstvi/{self.quest.id}/")
self.assertEquals(response.status_code, 301)
self.assertEquals(
response.url, f"/dobrodruzstvi/{self.quest.id}-{self.quest.get_slug()}/"
def test_redirect(self):
self.assert_redirect(f"/dobrodruzstvi/{self.creation.id}/", "/dobrodruzstvi")


class TestSkillPrintRedirect(RedirectTestCase):
def create_creation(self):
self.creation = Skill.objects.create(
id=1, name="Test skill", description="yolo"
)

def test_redirect(self):
self.assert_redirect(
f"/code/dovednosti/dovednosti_tisk.php?id={self.creation.id}",
"/rubriky/dovednosti",
)


class TestRangerSpellRedirect(RedirectTestCase):
def create_creation(self):
self.creation = RangerSpell.objects.create(
name="Super Kouzlo",
magenergy=1,
is_published=ApprovalChoices.APPROVED.value,
)

def test_redirect(self):
self.assert_redirect(
f"/code/hranicarkouzla/hranicarkouzla_tisk.php?id={self.creation.id}",
"/rubriky/hranicarkouzla",
)
7 changes: 6 additions & 1 deletion ddcz/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

from . import views
from .views import news, tavern, misc, email
from .views.legacy import legacy_router
from .views.legacy import legacy_router, print_legacy_router

app_name = "ddcz"

urlpatterns = [
path("", RedirectView.as_view(url="aktuality/", permanent=True)),
path("index.php", legacy_router, name="legacy-router"),
re_path(
"code/(?P<page_category>[a-zA-Z0-9_-]+)/(?P<page_category_second>[a-zA-Z0-9_-]+)_tisk.php",
print_legacy_router,
name="legacy-router-print",
),
path(
"robots.txt",
TemplateView.as_view(template_name="robots.txt", content_type="text/plain"),
Expand Down
25 changes: 23 additions & 2 deletions ddcz/views/legacy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from django.apps import apps
from ddcz.models.used.creations import Creation, CreativePage
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404
from django.urls.base import reverse
Expand Down Expand Up @@ -50,6 +50,11 @@
"noverasy": "noverasy",
}

PRINT_CATEGORIES_TO_CREATIVE_PAGE_MAP = {
"dovednosti": "dovednosti",
"hranicarkouzla": "hranicar",
}


ALLOWED_CREATION_PAGES = [
"bestiar",
Expand Down Expand Up @@ -123,7 +128,7 @@ def get_creation_detail_redirect(page, article_id):
app, class_name = page.model_class.split(".")
model = apps.get_model(app, class_name)
article = get_object_or_404(model, id=article_id)
return HttpResponseRedirect(
return HttpResponsePermanentRedirect(
reverse(
"ddcz:creation-detail",
kwargs={
Expand All @@ -133,3 +138,19 @@ def get_creation_detail_redirect(page, article_id):
},
)
)


@require_http_methods(["HEAD", "GET"])
def print_legacy_router(request, page_category, page_category_second):
id = request.GET.get("id", False)

if page_category in ALLOWED_CREATION_PAGES:
page = CreativePage.objects.get(slug=page_category)
return get_creation_detail_redirect(page, id)
else:
raise ValueError(id)
### Finally if no route is found, redirect to news and log
logger.warning(
f"Bad print redirect: No redirect could be found for a legacy URL {request.get_full_path()}"
)
return HttpResponseRedirect(reverse("ddcz:news"))

0 comments on commit f6dd684

Please sign in to comment.