Mercurial > hg > index.fcgi > www > www-1
view laterlinks3/laterlinks_flask_app.py @ 117:476cb019ad9f
add laterlinks3
author | paulo |
---|---|
date | Tue, 08 Sep 2020 23:52:57 -0700 |
parents | |
children | 65db090a697e |
line source
1 import csv
2 import datetime
3 import os
5 import flask
7 from html3.html3 import HTML
9 app = flask.Flask(__name__)
11 PIN = os.environ.get('LLPIN')
12 STRTIME_FMT = "%Y-%m-%d %H:%M:%S"
15 class LLDialect(csv.Dialect):
16 delimiter = '\t'
17 quoting = csv.QUOTE_NONE
18 lineterminator = '\n'
21 LLDIALECT = LLDialect()
22 LLDB_FN = "lldb.tsv"
23 LLDB_UNREAD_FN = "lldb_unread.tsv"
26 class LLError(Exception):
27 pass
29 class PinFailError(LLError):
30 def __str__(self):
31 return "PIN FAIL!"
33 class PinSetupError(LLError):
34 def __str__(self):
35 return "PIN SETUP ERROR!"
37 class MissingFieldsError(LLError):
38 def __str__(self):
39 return "MISSING FIELD(s)!"
42 def lldb_unread_load():
43 return csv.reader(open(LLDB_UNREAD_FN), LLDIALECT)
46 def lldb_add(inp):
47 title = inp.get("title")
48 url = inp.get("url")
49 if not (title and url):
50 raise MissingFieldsError()
52 dt_str = datetime.datetime.now().strftime(STRTIME_FMT)
53 with open(LLDB_FN, 'a') as lldb_f:
54 csv.writer(lldb_f, LLDIALECT).writerow([title, url, dt_str])
55 with open(LLDB_UNREAD_FN, 'a') as lldb_f:
56 csv.writer(lldb_f, LLDIALECT).writerow([title, url, dt_str])
59 def lldb_unread_delete(inp):
60 delete = inp.getlist("delete")
61 if not delete:
62 raise MissingFieldsError()
64 lldb_unread = [i for i in lldb_unread_load()]
65 lldb_unread_f = open(LLDB_UNREAD_FN, 'w')
67 try:
68 for i in delete:
69 for j in lldb_unread:
70 dt_str = j[2]
71 if i == dt_str:
72 lldb_unread.remove(j)
73 finally:
74 csv.writer(lldb_unread_f, LLDIALECT).writerows(lldb_unread)
75 lldb_unread_f.close()
79 @app.route("/", methods=["GET", "POST"])
80 def index():
81 is_post = (flask.request.method == "POST")
82 inp = flask.request.form
83 cookies = flask.request.cookies
85 if is_post:
86 if not PIN:
87 raise PinSetupError
88 elif cookies.get("llpin") != PIN:
89 raise PinFailError
91 if inp["submit"] == "Add":
92 lldb_add(inp)
93 elif inp["submit"] == "Delete":
94 lldb_unread_delete(inp)
96 title = "later links..."
97 root = HTML("html")
99 header = root.head
100 header.link(rel="stylesheet", type="text/css", href=flask.url_for("static", filename="index.css"))
101 header.title(title)
103 body = root.body
104 body.h1(title)
106 form = body.form(action="/", method="post")
108 table = form.table
109 hrow = table.tr
110 hrow.th("Link")
111 hrow.th("Created")
112 hrow.th.input(type="submit", name="submit", value="Delete")
114 for (title, url, dt_str) in lldb_unread_load():
115 row = table.tr
116 row.td.a(title, href=url)
117 row.td(dt_str)
118 row.td.input(type="checkbox", name="delete", value=dt_str)
120 p1 = form.p
121 p1.label("Title").input(type="text", name="title", size="64")
122 p1.br
123 p1.label("URL").input(type="text", name="url", size="64")
124 p1.br
125 p1.input(type="submit", name="submit", value="Add")
127 return str(root).encode("utf-8")