-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.py
More file actions
50 lines (46 loc) · 1.5 KB
/
Copy pathpage.py
File metadata and controls
50 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, render_template, request, redirect
from article import article
import json
from markov import markov
import os
app = Flask(__name__)
@app.route('/')
def index(*args):
articles = []
try:
with open('articles.json') as f:
try:
articles = [article(_) for _ in json.load(f)]
except json.decoder.JSONDecodeError:
os.remove('articles.json')
return redirect('refresh', code = 303)
except FileNotFoundError:
return redirect('refresh', code = 303)
return render_template('main.html', lines = articles)
@app.route('/refresh/')
def refresh():
articles = [article()]
try:
with open('articles.json') as f:
articles += [article(_) for _ in json.load(f)]
except FileNotFoundError:
pass
except json.decoder.JSONDecodeError:
os.remove('articles.json')
with open('articles.json', 'w+') as f:
json.dump([a.__dict__ for a in articles], f)
return redirect('/', code = 303)
@app.route('/articles/<guid>')
def articles(guid):
try:
with open('articles.json') as f:
articles = [article(_) for _ in json.load(f)]
for art in articles:
if(art.guid == guid):
return render_template('article.html', article = art)
except FileNotFoundError:
return redirect('refresh', code = 303)
return redirect('/', code = 303)
if __name__ == '__main__':
app.debug = True
app.run()