-
Notifications
You must be signed in to change notification settings - Fork 0
/
collabomo.py
executable file
·142 lines (106 loc) · 3.87 KB
/
collabomo.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# Copyright (c) 2014 Mohammed Yaseen Mowzer
#
# This Software is subject to the terms of the MIT License which can be found
# LICENSE file.
from flask import Flask, render_template, redirect, url_for, request, session
import os
import datetime
app = Flask(__name__)
@app.route('/')
def index():
if 'username' in session and session['username'] in members:
return redirect(url_for('collabomo'))
else:
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
if authenticate(request.form):
session['username'] = request.form['username']
return redirect(url_for('collabomo'))
else:
return render_template('login.html',
app_name="Collabomo Access Denied")
else:
return render_template('login.html', app_name="Collabomo")
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))
@app.route('/collabomo', methods=["GET", "POST"])
def collabomo():
if 'username' in session and session['username'] in members:
if request.method == 'POST':
user = session['username']
for i in range(1, NUM_OF_QUESTIONS + 1):
answers[user][i] = request.form['answer' + str(i)]
save_answers()
return render_template('collabomo.html',
names = members,
username = session['username'],
NUM_OF_QUESTIONS = NUM_OF_QUESTIONS,
times = ["No posts" if len(t) <= 0 else str(t[-1][0]) + ' ' + str(t[-1][2]) for t in forum_threads ],
answers = answers)
else:
return 'You are not allowed to view this'
@app.route('/thread/<questnum>', methods=["GET", "POST"])
def forum(questnum):
try:
number = int(questnum)
if request.method == "POST":
text = request.form["posttext"]
user = session['username']
if user in members:
post_to_forum(user, text, number)
return render_template('thread.html',
app_name = "Collabomo",
question = questnum,
posts = forum_threads[number])
except SyntaxError:
return "Page not found"
def authenticate(form):
correct_password = 'password' in form and form['password'] == password
correct_username = 'username' in form and form['username'] in members
return correct_password and correct_username
def save_answers():
with open(ANSWERS_FILE, 'w') as f:
f.write(str(answers))
def init_answers():
for m in members:
answers[m] = [None for i in range(NUM_OF_QUESTIONS + 1)]
forum_path = 'posts/'
def read_forum_data():
forum_data = list()
for i in range(NUM_OF_QUESTIONS + 1):
filename = forum_path + str(i)
if os.path.exists(filename):
with open(filename) as f:
forum_data.append(eval(f.readline()))
else:
forum_data.append(list())
return forum_data
def post_to_forum(user, text, thread):
forum_threads[thread].append((user, text, datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=2)))))
with open(forum_path + str(thread), 'w') as f:
f.write(str(forum_threads[thread]))
members = list()
password = ''
answers = dict()
NUM_OF_QUESTIONS = 30
ANSWERS_FILE = 'answerdict'
forum_threads = read_forum_data()
with open('secrets') as f:
app.secret_key = f.readline()
for i in range(4):
members.append(f.readline().strip())
with open(ANSWERS_FILE) as f2:
try:
answers = eval(f2.readline())
if not isinstance(answers, dict):
raise SyntaxError()
except SyntaxError:
init_answers()
password = f.readline().strip()
if __name__ == '__main__':
app.run(debug=True)