forked from nraw/diplomacy_news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
125 lines (106 loc) · 3.88 KB
/
app.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, send_from_directory, render_template_string
import json
import os
import re
app = Flask(__name__)
app.secret_key = 'f63e4ef09ab00ca7eb3519f3'
def load_users():
with open('users.json') as f:
return json.load(f)
def save_message(message, user):
message_entry = {
'user': user,
'message': message
}
messages = []
if os.path.exists('data.json'):
with open('data.json', 'r') as f:
messages = json.load(f)
# Update the existing message if the user already has one
found = False
for entry in messages:
if entry.get('user') == user:
entry['message'] = message
found = True
break
if not found:
# Add a new entry if the user does not have one
messages.append(message_entry)
with open('data.json', 'w') as f:
json.dump(messages, f, indent=4)
def load_user_message(user):
if os.path.exists('data.json'):
with open('data.json', 'r') as f:
messages = json.load(f)
for entry in messages:
if entry.get('user') == user:
return entry.get('message', "")
return ""
def delete_user_message(user):
if os.path.exists('data.json'):
with open('data.json', 'r') as f:
messages = json.load(f)
messages = [entry for entry in messages if entry.get('user') != user]
with open('data.json', 'w') as f:
json.dump(messages, f, indent=4)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
users = load_users()
if username in users and users[username]['password'] == password:
session['user'] = username
return redirect(url_for('message'))
else:
return 'Invalid credentials', 403
return render_template('login.html')
@app.route('/message', methods=['GET', 'POST'])
def message():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
if request.method == 'POST':
message = request.form['message']
save_message(message, user)
user_message = load_user_message(user)
return render_template('message.html', current_user=user, user_message=user_message)
@app.route('/delete', methods=['POST'])
def delete():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
delete_user_message(user)
return redirect(url_for('message'))
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect(url_for('home'))
@app.route('/menu')
def menu():
archive_dir = './archive/'
files = [f for f in os.listdir(archive_dir)]
file_names_without_extension = [
os.path.splitext(f)[0].replace('-', ' ')
for f in files if f.endswith('.html')
]
return render_template('menu.html', files=files, pretty_filename=file_names_without_extension)
def rewrite_static_paths(html_content):
# Replace relative paths starting with static/ to absolute paths starting with /static/
html_content = re.sub(r'(href|src)="static/([^"]*)"', r'\1="/static/\2"', html_content)
return html_content
@app.route('/archive/<filename>')
def view_file(filename):
if filename.endswith('.html'):
file_path = os.path.join('./archive', filename)
if os.path.exists(file_path):
with open(file_path, 'r') as f:
html_content = f.read()
html_content = rewrite_static_paths(html_content)
return render_template_string(html_content)
return send_from_directory('./archive', filename)
if __name__ == '__main__':
app.run(debug=True)