Mercurial > hg > index.fcgi > www > www-1
comparison laterlinks3/laterlinks_flask_app.py @ 117:476cb019ad9f
add laterlinks3
author | paulo |
---|---|
date | Tue, 08 Sep 2020 23:52:57 -0700 |
parents | |
children | 65db090a697e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a4f973bf5d09 |
---|---|
1 import csv | |
2 import datetime | |
3 import os | |
4 | |
5 import flask | |
6 | |
7 from html3.html3 import HTML | |
8 | |
9 app = flask.Flask(__name__) | |
10 | |
11 PIN = os.environ.get('LLPIN') | |
12 STRTIME_FMT = "%Y-%m-%d %H:%M:%S" | |
13 | |
14 | |
15 class LLDialect(csv.Dialect): | |
16 delimiter = '\t' | |
17 quoting = csv.QUOTE_NONE | |
18 lineterminator = '\n' | |
19 | |
20 | |
21 LLDIALECT = LLDialect() | |
22 LLDB_FN = "lldb.tsv" | |
23 LLDB_UNREAD_FN = "lldb_unread.tsv" | |
24 | |
25 | |
26 class LLError(Exception): | |
27 pass | |
28 | |
29 class PinFailError(LLError): | |
30 def __str__(self): | |
31 return "PIN FAIL!" | |
32 | |
33 class PinSetupError(LLError): | |
34 def __str__(self): | |
35 return "PIN SETUP ERROR!" | |
36 | |
37 class MissingFieldsError(LLError): | |
38 def __str__(self): | |
39 return "MISSING FIELD(s)!" | |
40 | |
41 | |
42 def lldb_unread_load(): | |
43 return csv.reader(open(LLDB_UNREAD_FN), LLDIALECT) | |
44 | |
45 | |
46 def lldb_add(inp): | |
47 title = inp.get("title") | |
48 url = inp.get("url") | |
49 if not (title and url): | |
50 raise MissingFieldsError() | |
51 | |
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]) | |
57 | |
58 | |
59 def lldb_unread_delete(inp): | |
60 delete = inp.getlist("delete") | |
61 if not delete: | |
62 raise MissingFieldsError() | |
63 | |
64 lldb_unread = [i for i in lldb_unread_load()] | |
65 lldb_unread_f = open(LLDB_UNREAD_FN, 'w') | |
66 | |
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() | |
76 | |
77 | |
78 | |
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 | |
84 | |
85 if is_post: | |
86 if not PIN: | |
87 raise PinSetupError | |
88 elif cookies.get("llpin") != PIN: | |
89 raise PinFailError | |
90 | |
91 if inp["submit"] == "Add": | |
92 lldb_add(inp) | |
93 elif inp["submit"] == "Delete": | |
94 lldb_unread_delete(inp) | |
95 | |
96 title = "later links..." | |
97 root = HTML("html") | |
98 | |
99 header = root.head | |
100 header.link(rel="stylesheet", type="text/css", href=flask.url_for("static", filename="index.css")) | |
101 header.title(title) | |
102 | |
103 body = root.body | |
104 body.h1(title) | |
105 | |
106 form = body.form(action="/", method="post") | |
107 | |
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") | |
113 | |
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) | |
119 | |
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") | |
126 | |
127 return str(root).encode("utf-8") |