Add redirects.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 30 Apr 2020 08:24:49 +0000 (10:24 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 10 Sep 2020 12:15:11 +0000 (14:15 +0200)
src/redirects/__init__.py [new file with mode: 0644]
src/redirects/admin.py [new file with mode: 0644]
src/redirects/apps.py [new file with mode: 0644]
src/redirects/migrations/0001_initial.py [new file with mode: 0644]
src/redirects/migrations/__init__.py [new file with mode: 0644]
src/redirects/models.py [new file with mode: 0644]
src/redirects/tests.py [new file with mode: 0644]
src/redirects/urls.py [new file with mode: 0644]
src/redirects/views.py [new file with mode: 0644]
src/wolnelektury/settings/apps.py
src/wolnelektury/urls.py

diff --git a/src/redirects/__init__.py b/src/redirects/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/redirects/admin.py b/src/redirects/admin.py
new file mode 100644 (file)
index 0000000..6742404
--- /dev/null
@@ -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 (file)
index 0000000..2e67943
--- /dev/null
@@ -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 (file)
index 0000000..9182f34
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/src/redirects/models.py b/src/redirects/models.py
new file mode 100644 (file)
index 0000000..219390d
--- /dev/null
@@ -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 (file)
index 0000000..7ce503c
--- /dev/null
@@ -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 (file)
index 0000000..5e16e01
--- /dev/null
@@ -0,0 +1,7 @@
+from django.urls import path
+from . import views
+from stats.utils import piwik_track_view
+
+urlpatterns = [
+    path('<slug:slug>/', 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 (file)
index 0000000..ed807b7
--- /dev/null
@@ -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
index 85272b9..413504d 100644 (file)
@@ -35,6 +35,7 @@ INSTALLED_APPS_OUR = [
     'paypal',
     'push',
     'club.apps.ClubConfig',
     'paypal',
     'push',
     'club.apps.ClubConfig',
+    'redirects.apps.RedirectsConfig',
 ]
 
 INSTALLED_APPS_CONTRIB = [
 ]
 
 INSTALLED_APPS_CONTRIB = [
index 3ba36da..d4a8ee1 100644 (file)
@@ -46,6 +46,7 @@ urlpatterns += [
     path('formularz/', include('forms_builder.forms.urls')),
     path('isbn/', include('isbn.urls')),
     path('messaging/', include('messaging.urls')),
     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)),
 
     path('paypal/app-form/', RedirectView.as_view(
         url='/towarzystwo/?app=1', permanent=False)),