From 9dda2c1f624c4740ae3149510b59f96b65bb819c Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Thu, 26 May 2022 15:11:18 +0200 Subject: [PATCH] add redirects --- shiftregister/redirects/__init__.py | 0 shiftregister/redirects/admin.py | 6 ++++++ shiftregister/redirects/apps.py | 6 ++++++ .../redirects/migrations/0001_initial.py | 20 +++++++++++++++++++ .../redirects/migrations/__init__.py | 0 shiftregister/redirects/models.py | 11 ++++++++++ shiftregister/redirects/tests.py | 3 +++ shiftregister/redirects/urls.py | 8 ++++++++ shiftregister/redirects/views.py | 9 +++++++++ shiftregister/settings.py | 1 + shiftregister/urls.py | 1 + 11 files changed, 65 insertions(+) create mode 100644 shiftregister/redirects/__init__.py create mode 100644 shiftregister/redirects/admin.py create mode 100644 shiftregister/redirects/apps.py create mode 100644 shiftregister/redirects/migrations/0001_initial.py create mode 100644 shiftregister/redirects/migrations/__init__.py create mode 100644 shiftregister/redirects/models.py create mode 100644 shiftregister/redirects/tests.py create mode 100644 shiftregister/redirects/urls.py create mode 100644 shiftregister/redirects/views.py diff --git a/shiftregister/redirects/__init__.py b/shiftregister/redirects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shiftregister/redirects/admin.py b/shiftregister/redirects/admin.py new file mode 100644 index 0000000..e5de724 --- /dev/null +++ b/shiftregister/redirects/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin + +from .models import Redirect + +# Register your models here. +admin.site.register(Redirect) diff --git a/shiftregister/redirects/apps.py b/shiftregister/redirects/apps.py new file mode 100644 index 0000000..2e0e9d7 --- /dev/null +++ b/shiftregister/redirects/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class RedirectsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "shiftregister.redirects" diff --git a/shiftregister/redirects/migrations/0001_initial.py b/shiftregister/redirects/migrations/0001_initial.py new file mode 100644 index 0000000..aa6c410 --- /dev/null +++ b/shiftregister/redirects/migrations/0001_initial.py @@ -0,0 +1,20 @@ +# Generated by Django 4.0.4 on 2022-05-26 13:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="Redirect", + fields=[ + ("slug", models.SlugField(primary_key=True, serialize=False)), + ("target", models.URLField()), + ], + ), + ] diff --git a/shiftregister/redirects/migrations/__init__.py b/shiftregister/redirects/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shiftregister/redirects/models.py b/shiftregister/redirects/models.py new file mode 100644 index 0000000..c339ea4 --- /dev/null +++ b/shiftregister/redirects/models.py @@ -0,0 +1,11 @@ +from django.db import models + +# Create your models here. + + +class Redirect(models.Model): + slug = models.SlugField(primary_key=True) + target = models.URLField() + + def __str__(self): + return f"{self.slug} => {self.target}" diff --git a/shiftregister/redirects/tests.py b/shiftregister/redirects/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/shiftregister/redirects/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/shiftregister/redirects/urls.py b/shiftregister/redirects/urls.py new file mode 100644 index 0000000..a06476b --- /dev/null +++ b/shiftregister/redirects/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from . import views + +app_name = "redirects" +urlpatterns = [ + path("", views.redir, name="redir"), +] diff --git a/shiftregister/redirects/views.py b/shiftregister/redirects/views.py new file mode 100644 index 0000000..7e5c86b --- /dev/null +++ b/shiftregister/redirects/views.py @@ -0,0 +1,9 @@ +from django.shortcuts import redirect, get_object_or_404 +from .models import Redirect + +# Create your views here. + + +def redir(request, slug): + obj = get_object_or_404(Redirect, pk=slug) + return redirect(obj.target) diff --git a/shiftregister/settings.py b/shiftregister/settings.py index 7c70ebf..1ac8423 100644 --- a/shiftregister/settings.py +++ b/shiftregister/settings.py @@ -45,6 +45,7 @@ INSTALLED_APPS = [ "shiftregister.pages.apps.PagesConfig", "shiftregister.metrics.apps.MetricsConfig", "shiftregister.signage.apps.SignageConfig", + "shiftregister.redirects.apps.RedirectsConfig", "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", diff --git a/shiftregister/urls.py b/shiftregister/urls.py index c70146a..2f18ccc 100644 --- a/shiftregister/urls.py +++ b/shiftregister/urls.py @@ -21,6 +21,7 @@ urlpatterns = [ path("team/", include("shiftregister.team.urls")), path("admin/", admin.site.urls), path("p/", include("shiftregister.pages.urls")), + path("r/", include("shiftregister.redirects.urls")), path("metrics/", include("shiftregister.metrics.urls")), path("", include("shiftregister.signage.urls")), ]