rakkapy/main/views.py

171 lines
7.7 KiB
Python
Raw Normal View History

2021-09-18 21:37:18 +10:00
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
import random as ra
import asyncio
from . import mdl
import functools
def rerender(funky):
@functools.wraps(funky)
def wrap(req,*args,**kwargs):
with open('main/utils/navs') as b:
navs=parsecat(b,4)
namednavs=[{'id':blah[0], 'link':blah[1], 'text':blah[2], 'align':int(blah[3])} for blah in navs['']]
out=funky(*args,**kwargs)
temp=loader.get_template('main/nav.html')
out['nav']=temp.render({"navs":namednavs},None)
return render(req, 'main/temp.html', out)
return wrap
def parsecat(file,n):
if not isinstance(file,str):
# Assume open handle
stuff=file.read()
else:
with open(f'main/pages/{file}') as b:
stuff=b.read()
cats=stuff.split('\n\n')
out={}
head=''
out['']=[]
for cat in cats:
if ':' in cat[:cat.index('\n')]:
# We have a category name
head,cat=cat.split('\n',1)
head=head[:-1]
out[head]=[]
else: head=''
for line in cat.split('\n'):
lin=line.split(',',n-1)
pad=n-len(lin)
lin.extend(['']*pad)
out[head].append(lin)
if out['']==[]: del(out[''])
return out
@rerender
def index():
with open('main/pages/index') as b:
stuff=b.read()
with open('main/utils/song-ids') as b:
raws=b.read()
ids=raws.split('\n')
stuff=stuff.format(ra.choice(ids))
2021-09-20 02:02:59 +10:00
return {'title':'Home','content':stuff,'date':'2021/09/20'}
2021-09-18 21:37:18 +10:00
@rerender
def me():
with open('main/pages/quotes') as b:
stuff=b.read()
stuff={n[:n.index(': ')]:n[n.index(': ')+2:] for n in stuff.split('\n')}
lbn='\\n'
stuff='<br/>\n'.join([f'"{v.replace(lbn,"<br/>")}" - {k.replace(lbn,"<br/>")}' for k,v in stuff.items()])
return {'title':'Me','content':stuff,'date':'2021/08/26'}
def lii(name,proto,link='',note=''):
if link: name=f"<a href='{link}'>{name}</a>"
if note: name=f"{name} ({note})"
return f"\n<li>{proto}: {name}</li>"
@rerender
def contact():
head="Certain people keep asking me about the best ways to contact me. So here they are, in rough order of most to least preferred. More to come as I remember them/feel like it<br/>"
out=parsecat('contacts',4)
out3=''
for k,v in out.items():
out2=f"\n<p><h4>{k}</h4>\n<ul>"
out2+='\n'.join([lii(*l) for l in v])
out2+="\n</ul></p>"
out3+=out2
return {'title':'Contact','content':head+out3,'date':'2021/08/13'}
def specs(req):
with open('main/pages/specs') as b:
return HttpResponse(b.read(), content_type="text/plain")
@rerender
def songs():
with open('main/utils/song-ids') as b:
raws=b.read()
ids=raws.split('\n')
out='This is the full list of songs that can appear on the home page.<br/>'+'<br/>\n'.join([f'<a href="https://invidious.ethibox.fr/watch?v={idd}">https://invidious.ethibox.fr/watch?v={idd}</a>' for idd in ids])
return {'title':'Songs','content':out,'date':'2021/05/06'}
@rerender
def host():
return {'title':'Webhost','content':"I want to do a fully-fledged hosting thing. Like offer an actual VPS. But that's a lot of effort, and I want to do things like \"you have a folder in your VPS that will show up on your custom subdomain\" and such. <strike>I have exactly zero idea how to make multiple subdomains point to a single computer though.</strike> So it turns out <strike>Apache</strike> actually just has a thing to do exactly that. But fuck Apache. nginx can do it too. TODO™",'date':'2021/05/06'}
@rerender
def free():
with open('main/pages/free') as b:
return {'title':'Not free beer','content':b.read(),'date':'2021/07/24'}
@rerender
def myroids():
with open('main/pages/myroids') as b:
return {'title':'#myroid:matrix.org','content':b.read(),'date':'2021/05/06'}
@rerender
def myroidbrowser():
with open('main/pages/myroidbrowse') as b:
return {'title':'#myroid:matrix.org','content':b.read(),'date':'2021/05/15'}
@rerender
def nerdshope():
return {'title':'Nerds\' Hope','content':"Store stuff. TODO. Will include inventory and budget. <a href='https://facebook.com/nerdshope'>FB</a><br/>Address is 21 Kensington St, Glenorchy<br/>Off dates 2021: 24th Sept, 29th Oct, 26th Nov<br/>Will be late on the 1st of Oct<br/>Not sure about 31st of Dec. Depends what other plans are made. I might host a NYE party that night.",'date':'2021/09/05'}
@rerender
def syntax():
return {'title':"What's that syntax?",'content':"<h1>What's that syntax?</h1><br/>A far-future goal, this will be a service for looking up strange bits of syntax you see in programs, that you're trying to find the name for so that you can search for that.<br/>Inspired by other lookup services like <a href='http://trace.moe'>What Anime is That?</a>, and more importantly, by C#'s <a href='https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers'>object initialiser</a>.",'date':'2021/05/06'}
def teapot(req):
return HttpResponse("You're probably missing the joke.",status=418)
@rerender
def articles():
head="<p>Interesting/useful blog posts I've found.</p>"
arts=parsecat('articles',2)
out=[]
for k,v in arts.items():
out.append(f'<h4>{k}</h4>'+ '<br/>'.join([f'<a href="{l[0]}">{l[1] or l[0]}</a>' for l in v]))
return {'title':'Articles','content':'<p>'+('</p><p>'.join(out))+'</p>','date':'2021/05/06'}
@rerender
def todd():
out="""<p>Obviously, this started as a joke and turned into a fully-fledged shitpost. The invite link is <a href= 'https://discordapp.com/oauth2/authorize?client_id=544769594343292929&scope=bot'>here</a>, the culprit is Zergling_man#9849, and... It just spams, kinda. Every 10 minutes in every channel and once every time someone starts a message with a ., or mentions it. Don't forget to buy Skyrim. Try '.wake'!</p>
Credits:<p>Avatar cropped from <a href='https://en.wikipedia.org/wiki/File:ToddHoward2010sm.jpg'>this picture</a>,<br/>
repeating text created by 'Jade', and kept sic.<br/>
Flashrim pictures provided by Emperor Tang.<br/>
Portalrim gif provided by ColeFreeman72.<br/>
Animated memes gif provided by SakoArorim.<br/>
Phonedrop gif provided by Takamatsu.<br/>
Mugrim also provided by Takamatsu.<br/>
Racerim provided by Kotz<br/>
"provided by" indicates the content was not created by the person. If no creator is mentioned, they're unknown.</p>
<p>As of 2021-04-14, I've taken this bot offline forever and released the token (and code). If you see it online, it's not my doing, and I don't care if it gets me banned.</p>"""
return {'title':'It just works','content':out,'date':'2021/07/24'}
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]})
@rerender
def bikebunny():
return {'title':'bikebunny','content':"I'm the rabbit-eared cyclist/bike bunny. And, well, the guy that wears blue bunny ears year-round (for now). This is my site. I live different, I encourage you to join me. <a href='/contact/'>Get in touch</a>.",'date':'2021/07/24'}
def md(req):
loop=asyncio.get_event_loop()
feed=loop.run_until_complete(mdl.makefeed(req.GET.getlist('ids','')))
return HttpResponse(feed, content_type='application/rss+xml')
@rerender
def todo():
2021-10-09 20:36:50 +11:00
return {'title':'Site TODOs','content':"Things to be done on the site:<br/><ul><li><strong><a href='/syntax'>Syntax checker</a></strong> - looking up syntax bits in different languages. Low priority.</li><li><strong>Become (opennic) DNS provider</strong> - strengthen Tasmania's infrastructure. Maybe become the authority for rakka.tk. Would be annoying without ip4. Anyway.</li><li><strong>Become arch/artix repository</strong> - Mostly just so I'm not constantly redownloading the same packgaes.</li><li><strong>Add DB</strong> - So that date modified is actually automatic kek</li></ul>",'date':'2021/09/21'}
@rerender
def autopage(pageid):
return {'title':'','content':'','date':''}