From 6c567292e52c04155ad10b1b916375ee52b1189e Mon Sep 17 00:00:00 2001 From: Zergling_man Date: Sun, 27 Feb 2022 23:23:26 +1100 Subject: [PATCH] Redirects now use DB --- main/admin.py | 3 +-- main/converters.py | 15 +++++++++++++-- main/models.py | 5 ++++- main/pages/redirects | 22 ---------------------- main/urls.py | 3 ++- main/views.py | 7 ++----- 6 files changed, 22 insertions(+), 33 deletions(-) delete mode 100644 main/pages/redirects diff --git a/main/admin.py b/main/admin.py index ed89fe9..d467133 100644 --- a/main/admin.py +++ b/main/admin.py @@ -6,5 +6,4 @@ from .models import Page,Contact,Update,Redirect admin.site.register(Page) admin.site.register(Contact) admin.site.register(Update) -admin.site.register(Redirect) -#dum \ No newline at end of file +admin.site.register(Redirect) \ No newline at end of file diff --git a/main/converters.py b/main/converters.py index e6307c5..d8eb514 100644 --- a/main/converters.py +++ b/main/converters.py @@ -1,4 +1,4 @@ -from .models import Page +from .models import Page,Redirect class AutoPageConverter: regex='.*' @@ -9,4 +9,15 @@ class AutoPageConverter: return p def to_url(self,value): if not isinstance(value,Page): raise ValueError('Given value is not a Page') - return p.url \ No newline at end of file + return p.url + +class RedirectConverter: + regex='.*' + def to_python(self,value): + try: r=Redirect.objects.get(match=value) + except Page.DoesNotExist: raise ValueError(f'No such redirect {value}') + if not r: raise ValueError(f'No such redirect {value}') + return r + def to_url(self,value): + if not isinstance(value,Redirect): raise ValueError('Given value is not a Redirect') + return p.match \ No newline at end of file diff --git a/main/models.py b/main/models.py index d82aef5..55b274f 100644 --- a/main/models.py +++ b/main/models.py @@ -40,4 +40,7 @@ class Update(models.Model): class Redirect(models.Model): match=models.CharField(max_length=200) - output=models.CharField(max_length=500) \ No newline at end of file + output=models.CharField(max_length=500) + + def __str__(self): + return self.match \ No newline at end of file diff --git a/main/pages/redirects b/main/pages/redirects deleted file mode 100644 index 564fb64..0000000 --- a/main/pages/redirects +++ /dev/null @@ -1,22 +0,0 @@ -adobe: https://www.patreon.com/posts/26834357 -faplog: https://docs.google.com/spreadsheets/d/1sIh5evj1Janf_fdefbrksGLyHO_uMMDOjHJReA0P0fw/edit -fpdam: https://docs.google.com/document/d/1XSKSC9PYxAE1ys2u5ALD3wDBaps6LQgGe-GtyAju3q8/edit?usp=sharing -fpdb: https://docs.google.com/spreadsheets/d/1iJd8dBMbAKfwbfcWNnE-tElu4amJdtz4iMxWCxwKNY8/edit -fuckmeyourself: https://gelbooru.com/index.php?page=post&s=view&id=4608040 -hitspark: https://images.squarespace-cdn.com/content/v1/5728734545bf2166bba67b00/1538785588130-I8WVGV947EEW81LII25S/ke17ZwdGBToddI8pDm48kODKEPcE7oOaZ-HX_UAJexAUqsxRUqqbr1mOJYKfIPR7LoDQ9mXPOjoJoqy81S2I8N_N4V1vUb5AoIIIbLZhVYy7Mythp_T-mtop-vrsUOmeInPi9iDjx9w8K4ZfjXt2dhV8WLpqAVTw2_ZfbaHHLLR7LAwPhjFlJ4O4wogcebl8m7cT0R_dexc_UL_zbpz6JQ/frame_advantage_5up.jpg?format=1000w -homo: https://innermantheatre.files.wordpress.com/2020/11/lightspeed-lab-ep.261-homosexuality.jpg -homo/s: https://innermantheatre.com/2021/03/29/lightspeed-lab-homosexuality/ -miruku: http://danbooru.donmai.us/posts/578126 -nadeko: https://docs.google.com/spreadsheets/d/12ZpOI3coZHlSFQ7oFuo1HzbkTkgX8RBk06oeMM56HFQ/edit#gid=0 -nlship: https://docs.google.com/document/d/1oVZSv7v8cePVu22qmbgkCUtm9LnUcSZIaQyH-3b_DhI/edit?usp=sharing -nlvn: https://docs.google.com/document/d/1tVVI2NAY_1vFLsMrbRzn18FiXNzZcDgdxnNXKHEFzTs/edit# -osscans: https://discord.gg/VStXkfm -ossd: https://drive.google.com/drive/folders/14xIZJER551zEB9rP9d655MdpRE0xDBW8 -perm: http://nadekobot.readthedocs.io/en/latest/Permissions%20System/ -rinbot: https://discordapp.com/oauth2/authorize?client_id=448458599673364481&scope=bot -sauce: https://discord.gg/gsUT8B -spook: /static/pics/SPOOKY/SCARY/SKELETONS/SEND/SHIVERS/DOWN/YOUR/SPINE/SHRIEKING/SKULLS/WILL/SHOCK/YOUR/SOUL/SEAL/YOUR/DOOM/TONIGHT/SPOOKY/SCARY/SKELETONS/SPEAK/WITH/SUCH/A/SCREECH/YOU'LL/SHAKE/AND/SHUDDER/IN/SURPRISE/WHEN/YOU/HEAR/THESE/ZOMBIES/SHRIEK/WE'RE/SO/SORRY/SKELETONS/YOU'RE/SO/MISUNDERSTOOD/YOU/ONLY/WANT/TO/SOCIALIZE/BUT/I/DON'T/THINK/WE/SHOULD/CAUSE_SPOOKY_SCARY_SKELETONS_SHOUT_STARTLING_SHRILLY_SCREAMS.gif -wou: https://discord.gg/8sc5m6R -WushuAT: https://docs.google.com/document/d/1wtJ6JCpmpr-ziAb0UR0OpW1tX7Ms5CiDFUQkjPgFzIE/edit?usp=drive_web&ouid=102857329945830311247 -years: https://docs.google.com/spreadsheets/d/1p7NIoC2-o8BJvbLD5N1WazaFAtw4vto7bxJMGsarcgE/edit -support: https://www.nerfnow.com/comic/1434 \ No newline at end of file diff --git a/main/urls.py b/main/urls.py index 2921bdf..8d31315 100644 --- a/main/urls.py +++ b/main/urls.py @@ -2,6 +2,7 @@ from django.urls import path, register_converter from . import views,converters register_converter(converters.AutoPageConverter,'autopage') +register_converter(converters.RedirectConverter,'redirect') urlpatterns=[ path('',views.index,name='idex'), @@ -12,7 +13,7 @@ urlpatterns=[ path('nerdshope',views.nerdshope,name='shop'), path('teapot',views.teapot,name='teapot'), path('toask',views.articles,name='articles'), - path('r/',views.redirect,name='redirect'), + path('r/',views.redirect,name='redirect'), path('md.rss',views.md,name='md'), path('me.vcf',views.vcard,name='vcard'), path('',views.autopage,name='everything'), diff --git a/main/views.py b/main/views.py index a21fc3e..456577b 100644 --- a/main/views.py +++ b/main/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponse import random as ra import asyncio from . import mdl -from .models import Page,Contact,Update +from .models import Page,Contact,Update,Redirect from rakka.utils import rerender,parsecat @@ -74,10 +74,7 @@ def articles(): return {'title':'Articles','content':'

'+('

'.join(out))+'

','date':'2021/05/06'} def redirect(req,src): - with open('main/pages/redirects') as b: - lonks=b.read() - lonks=dict([n.split(': ',1) for n in lonks.split('\n')]) - return HttpResponse(status=302,headers={'Location':lonks[src]}) + return HttpResponse(status=302,headers={'Location':src.output}) def md(req): loop=asyncio.get_event_loop()