From 47f52a37e100ff30ce8fe2c62835ae0b596b909b Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 30 Apr 2020 10:24:49 +0200 Subject: [PATCH] Add redirects. --- src/redirects/__init__.py | 0 src/redirects/admin.py | 17 +++++++++++++++++ src/redirects/apps.py | 5 +++++ src/redirects/migrations/0001_initial.py | 24 ++++++++++++++++++++++++ src/redirects/migrations/__init__.py | 0 src/redirects/models.py | 16 ++++++++++++++++ src/redirects/tests.py | 3 +++ src/redirects/urls.py | 7 +++++++ src/redirects/views.py | 12 ++++++++++++ src/wolnelektury/settings/apps.py | 1 + src/wolnelektury/urls.py | 1 + 11 files changed, 86 insertions(+) create mode 100644 src/redirects/__init__.py create mode 100644 src/redirects/admin.py create mode 100644 src/redirects/apps.py create mode 100644 src/redirects/migrations/0001_initial.py create mode 100644 src/redirects/migrations/__init__.py create mode 100644 src/redirects/models.py create mode 100644 src/redirects/tests.py create mode 100644 src/redirects/urls.py create mode 100644 src/redirects/views.py diff --git a/src/redirects/__init__.py b/src/redirects/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/redirects/admin.py b/src/redirects/admin.py new file mode 100644 index 000000000..674240477 --- /dev/null +++ b/src/redirects/admin.py @@ -0,0 +1,17 @@ +from django.contrib import admin +from django.contrib.sites.models import Site +from . import models + + +class RedirectAdmin(admin.ModelAdmin): + list_display = ['slug', 'url', 'counter', 'created_at', 'full_url'] + readonly_fields = ['counter', 'created_at', 'full_url'] + fields = ['slug', 'url', 'counter', 'created_at', 'full_url'] + + def full_url(self, obj): + site = Site.objects.get_current() + url = obj.get_absolute_url() + return f'https://{site.domain}{url}' + + +admin.site.register(models.Redirect, RedirectAdmin) diff --git a/src/redirects/apps.py b/src/redirects/apps.py new file mode 100644 index 000000000..2e6794379 --- /dev/null +++ b/src/redirects/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class RedirectsConfig(AppConfig): + name = 'redirects' diff --git a/src/redirects/migrations/0001_initial.py b/src/redirects/migrations/0001_initial.py new file mode 100644 index 000000000..9182f347a --- /dev/null +++ b/src/redirects/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.10 on 2020-04-30 08:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Redirect', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('slug', models.SlugField(unique=True)), + ('url', models.CharField(max_length=255)), + ('counter', models.IntegerField(default=0)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/src/redirects/migrations/__init__.py b/src/redirects/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/redirects/models.py b/src/redirects/models.py new file mode 100644 index 000000000..219390df2 --- /dev/null +++ b/src/redirects/models.py @@ -0,0 +1,16 @@ +from django.db import models +from django.urls import reverse + + +class Redirect(models.Model): + slug = models.SlugField(unique=True) + url = models.CharField(max_length=255) + counter = models.IntegerField(default=0) + created_at = models.DateTimeField(auto_now_add=True) + + def get_absolute_url(self): + return reverse('redirect', args=[self.slug]) + + def update_counter(self): + type(self).objects.filter(pk=self.pk).update(counter=models.F('counter') + 1) + diff --git a/src/redirects/tests.py b/src/redirects/tests.py new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/src/redirects/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/src/redirects/urls.py b/src/redirects/urls.py new file mode 100644 index 000000000..5e16e018e --- /dev/null +++ b/src/redirects/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views +from stats.utils import piwik_track_view + +urlpatterns = [ + path('/', piwik_track_view(views.WLRedirectView.as_view()), name='redirect'), +] diff --git a/src/redirects/views.py b/src/redirects/views.py new file mode 100644 index 000000000..ed807b700 --- /dev/null +++ b/src/redirects/views.py @@ -0,0 +1,12 @@ +from django.shortcuts import get_object_or_404 +from django.views.generic.base import RedirectView +from . import models + + +class WLRedirectView(RedirectView): + permanent = False + + def get_redirect_url(self, slug): + redirect = get_object_or_404(models.Redirect, slug=slug) + redirect.update_counter() + return redirect.url diff --git a/src/wolnelektury/settings/apps.py b/src/wolnelektury/settings/apps.py index 85272b939..413504de5 100644 --- a/src/wolnelektury/settings/apps.py +++ b/src/wolnelektury/settings/apps.py @@ -35,6 +35,7 @@ INSTALLED_APPS_OUR = [ 'paypal', 'push', 'club.apps.ClubConfig', + 'redirects.apps.RedirectsConfig', ] INSTALLED_APPS_CONTRIB = [ diff --git a/src/wolnelektury/urls.py b/src/wolnelektury/urls.py index 3ba36dae9..d4a8ee157 100644 --- a/src/wolnelektury/urls.py +++ b/src/wolnelektury/urls.py @@ -46,6 +46,7 @@ urlpatterns += [ path('formularz/', include('forms_builder.forms.urls')), path('isbn/', include('isbn.urls')), path('messaging/', include('messaging.urls')), + path('re/', include('redirects.urls')), path('paypal/app-form/', RedirectView.as_view( url='/towarzystwo/?app=1', permanent=False)), -- 2.20.1