# Simple task list with editing via web interface import cgi, time, calendar from wsgiref import util # Inital values of text fields jobs = ["1", "2", "3", "4", "5", "6", "7", "8"] colors = ("red", "#FF8000", "yellow", "green", "blue", "navy", "#202080", "#404080", "#606080", "#808080") #colors = ("#FF8000", "yellow", "#80ff80", "0080ff", "#8080ff", "#808080", "#b0b0b0", "#b0b0b0") #colors = ("#FF2020", "#FF4040", "#FF6060", "#FF7070", "#FF8080", "#FF9090", "#FFA0A0", "#FFB0B0", "#FFC0C0", "#FFD0D0", "#FFE0E0") css = b'''body {background-color: black; color: white; font-size: 20px; font-family: Verdana, Tahoma, Arial, sans-serif;} h1 {color: white; font-size: 30px; font-family: Verdana, Tahoma, Arial, sans-serif;} h2 {color: black; font-size: 24px; font-family: Verdana, Tahoma, Arial, sans-serif;}''' forma = b''' Tasklist Editor

''' formc = b'''
''' def app(environ, start_response): global jobs now = time.localtime() page = util.shift_path_info(environ) if page == "edit": if environ['REQUEST_METHOD'] == 'POST': post_env = environ.copy() post_env['QUERY_STRING'] = '' post = cgi.FieldStorage( fp=environ['wsgi.input'], environ=post_env, keep_blank_values=True ) for index, job in enumerate(jobs): txt = "Text"+str(index) jobs[index] = post[txt].value html = forma + css + formb for index, job in enumerate(jobs): txt = "Text"+str(index) html = html + b'''Delete MoveUp MoveDown ''' html = html + b'Insert new' + formc start_response('200 OK', [('Content-Type', 'text/html')]) return [html] elif page.startswith("del"): x = int(page[3:]) print ("Deleting page " + str(x)) del jobs[x] html = b'''TasklistThis should redirect to http://localhost:8000/edit''' start_response('200 OK', [('Content-Type', 'text/html')]) return [html] elif page.startswith("mov"): x = int(page[4:]) print ("Moving job " + str(page[4:]) + " Direction " + str(page[3:4])) # move job up if page[3:4] == "u": if x > 0: temp = jobs[x-1] jobs[x-1] = jobs[x] jobs[x] = temp # move job down if page[3:4] == "d": if x < (len(jobs)-1): temp = jobs[x+1] jobs[x+1] = jobs[x] jobs[x] = temp html = b'''TasklistThis should redirect to http://localhost:8000/edit''' start_response('200 OK', [('Content-Type', 'text/html')]) return [html] elif page.startswith("ins"): print ("Inserting job at bottom") jobs.insert(len(jobs), "") html = b'''TasklistThis should redirect to http://localhost:8000/edit''' start_response('200 OK', [('Content-Type', 'text/html')]) return [html] else: cal = calendar.HTMLCalendar(calendar.MONDAY) month = cal.formatmonth(now[0], now[1]) html = b'''Tasklist''' for index, job in enumerate(jobs): if index >= len(colors): color = colors[len(colors)-1].encode("utf-8") else: color = colors[index].encode("utf-8") html = html + b'''''' html = html + b'

''' + jobs[index].encode("utf-8") + b'''

' start_response('200 OK', [('Content-Type', 'text/html')]) return [html] if __name__ == '__main__': from wsgiref.simple_server import make_server httpd = make_server('', 8000, app) print('Serving on port 8000...') httpd.serve_forever()