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
2021-12-12 21:52:02 +11:00
from . models import Page , Contact , Update
2021-09-18 21:37:18 +10:00
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-12-15 17:54:38 +11:00
return { ' title ' : ' Home ' , ' content ' : stuff , ' date ' : ' 2021/12/15 ' }
2021-09-18 21:37:18 +10:00
2021-12-12 21:52:02 +11:00
def lii ( obj ) :
name = obj . name
if obj . url : name = f " <a href= ' { obj . url } ' > { name } </a> "
if obj . comment : name = f " { name } ( { obj . comment } ) "
2021-12-24 12:01:00 +11:00
if obj . desktop : name = f " { name } 🖥 "
if obj . mobile : name = f " { name } 📱 "
2021-12-12 21:52:02 +11:00
return f " <li> { obj . proto } : { name } </li> "
2021-09-18 21:37:18 +10:00
@rerender
def contact ( ) :
2021-12-24 12:01:00 +11:00
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/>
🖥 = I will receive on my computer , 📱 = I will receive on my phone < br / > """
2021-12-12 21:53:10 +11:00
contacts = sorted ( Contact . objects . all ( ) , key = lambda x : ( x . group , x . priority ) )
2021-12-12 21:52:02 +11:00
out = { }
for n in contacts :
if n . group in out : out [ n . group ] . append ( n )
else : out [ n . group ] = [ n ]
2021-09-18 21:37:18 +10:00
out3 = ' '
for k , v in out . items ( ) :
2021-12-12 21:52:02 +11:00
out2 = f " \n <p><h4> { Contact . groups [ k ] } </h4> \n <ul> "
out2 + = ' \n ' . join ( map ( lii , v ) )
2021-09-18 21:37:18 +10:00
out2 + = " \n </ul></p> "
out3 + = out2
2022-01-26 19:51:03 +11:00
return { ' title ' : ' Contact ' , ' content ' : head + out3 , ' date ' : Update . objects . get_or_create ( page = ' contact ' ) [ 0 ] . date }
2021-09-18 21:37:18 +10:00
def specs ( req ) :
with open ( ' main/pages/specs ' ) as b :
return HttpResponse ( b . read ( ) , content_type = " text/plain " )
2022-01-02 22:04:47 +11:00
@rerender
def comment ( ) :
return { ' title ' : ' Comment box ' , ' content ' : " TODO " , ' date ' : ' 2021/12/24 ' }
2021-09-18 21:37:18 +10:00
@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 nerdshope ( ) :
2022-01-02 22:04:47 +11:00
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 2022:Dunno yet.<br/>NYE party 31st Dec. " , ' date ' : ' 2021/12/24 ' }
2021-09-18 21:37:18 +10:00
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 ' }
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 ] } )
def md ( req ) :
loop = asyncio . get_event_loop ( )
feed = loop . run_until_complete ( mdl . makefeed ( req . GET . getlist ( ' ids ' , ' ' ) ) )
2021-09-21 02:39:54 +10:00
return HttpResponse ( feed , content_type = ' application/rss+xml ' )
2021-11-10 02:45:32 +11:00
def vcard ( req ) :
return HttpResponse ( """ BEGIN:VCARD
VERSION : 4.0
KIND : individual
FN : Zergling_man
EMAIL : zerglingman @fedora.email
URL : https : / / rakka . tk
IMPP : xmpp : zergling_man @xmpp.jp ; matrix : @zergling.man : perthchat . org
NOTE : Anti - professionalism , pro - open standards
CATEGORIES : programmer , biker
SOURCE : https : / / rakka . tk / me . vcf
END : VCARD """ )
2021-12-10 03:33:05 +11:00
2021-10-09 20:36:50 +11:00
@rerender
2021-10-10 00:27:52 +11:00
def autopage ( page ) :
2022-01-26 19:51:03 +11:00
return { ' title ' : page . title , ' content ' : page . contents , ' date ' : page . last_edited }