/
animalfarm.py
235 lines (176 loc) · 5.92 KB
/
animalfarm.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#For timestamps in comments
import time
#For database commands
import sqlite3
#Microframework used for simple web application
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
#Used to close databases
from contextlib import closing
#Random class for choosing a random topic
import random
#OS to check port and change debug settings
import os
#Configuration
DATABASE = 'barn.db'
DEBUG = False
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
#Creating our application
app = Flask(__name__)
app.config.from_object(__name__)
#Homepage which redirects to a random number in the SQL database
@app.route('/')
def new_url():
cur = g.db.execute('SELECT max(id) FROM topics')
max_id = cur.fetchone()[0]
if(max_id):
page = random.randint(1,max_id)
else:
return redirect(url_for('new_post'))
return redirect(url_for('pages' ,page_num = str(page)))
#Displays topic, text, and comments dynamically
@app.route('/discussion/<page_num>')
def pages(page_num):
pro = True
page_num = int(page_num)
session['topic_id'] = page_num
session['current_page'] = request.path
cur = g.db.execute('SELECT max(id) FROM topics')
limit = cur.fetchone()[0]
if page_num > limit:
return redirect(url_for('invalid_topic'))
cur = g.db.execute('SELECT title, text FROM topics WHERE id=:uid', {'uid': page_num})
row = cur.fetchone()
cur = g.db.execute('SELECT * FROM comments WHERE topicid=:uid', {'uid': page_num})
commentList = cur.fetchall()
return render_template('HomePage.html', row=row, comments=commentList, debug=DEBUG, pro=pro)
#The comment route implements the comment. Essentially redirects back to the
#discussion page at the end
@app.route('/comment', methods=['POST'])
def comment():
if not session.get('logged_in'):
#send to login page, w/ topic number to redirect back
return redirect(url_for('login'))
else:
#handle comment/add to db and send back to page
cur = g.db.execute('SELECT * FROM comments WHERE userid=:uid', {'uid': session.get('user_id')})
comments = cur.fetchall()
if comments:
animal_name = comments[0][2]
else:
animal_name = rand_animal_name()
text = request.form['Response']
if text != None:
g.db.execute('INSERT INTO comments VALUES(?, ?, ?, ?, ?)',
(session.get('topic_id'), session.get('user_id'), animal_name, request.form['Response'], int(time.time())))
g.db.commit()
return redirect(session.get('current_page'))
#Routes to login redirects user back to page where they left off
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
username = request.form['user']
password = request.form['pass']
else:
username = None
password = None
cur = g.db.execute('SELECT * FROM users WHERE main_username=:uid', {'uid': username})
userList = cur.fetchone()
if userList:
if userList[2] == password:
session['logged_in'] = True
return redirect(url_for('pages', page_num=session.get('topic_id')))
else:
error = "There was an error in your submission."
return render_template('Login.html', error=error)
else:
return render_template('Login.html', error=error)
@app.route('/logout')
def logout():
session['logged_in'] = False
return redirect(url_for('login'))
@app.route('/new', methods=['GET', 'POST'])
def new_post():
if not session.get('logged_in'):
#send to login page, w/ topic number to redirect back
return redirect(url_for('login'))
error = None
if request.method == 'POST':
#handle input and send
title = request.form['Title']
body = request.form['Body']
if title == None or body == None:
error = "Field cannot be left blank."
else:
g.db.execute('INSERT INTO topics VALUES(NULL, ?, ?)', (title, body))
g.db.commit()
cur = g.db.execute('SELECT max(id) FROM topics')
max_id = cur.fetchone()[0]
return redirect(url_for('pages', page_num=max_id))
else:
#show page and get input
return render_template('NewTopic.html', error=error)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
error = None
if request.method == 'POST':
username = request.form['user']
cur = g.db.execute("SELECT * FROM users WHERE main_username=:user", {'user':username})
if cur.fetchone():
error = "Username already taken."
else:
password1 = request.form['pass']
password2 = request.form['pass_conf']
if password1 == None or password2 == None:
error = "Password cannot be blank"
elif password1 == password2:
g.db.execute("INSERT INTO users VALUES(NULL, ?, ?)", (username, request.form['pass']))
g.db.commit()
#Implement flash message to say user name sign up was successful
return redirect(url_for('login'))
else:
error = "Passwords did not match."
return render_template('signup.html', error = error)
@app.route('/about')
def about():
return render_template('About.html')
#Generates a random animal name
def rand_animal_name():
with open('adjectives.txt') as f:
adjectives = f.readlines()
with open('animals.txt') as b:
animals = b.readlines()
s1 = random.choice(adjectives).title()
s2 = random.choice(animals)
strpre = s1 + s2
strfinal = strpre.replace("\n", " ")
return strfinal
#Connects to database thru sqlite3
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
#Connects to database at the beginning of each page request
@app.before_request
def before_request():
g.db = connect_db()
#Possibly needs semicolon at end(?)
g.db.execute('PRAGMA foreign_keys= ON')
#Closes database after each page request
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
#Exception when a user tries to access an out of bounds topic
@app.route('/invalid')
def invalid_topic():
return 'This topic does not exist.'
#Error handler for dead links that aren't there
@app.errorhandler(404)
def page_not_found(error):
return 'This page does not exist', 404
#Standard boilerplate for python main
if __name__ == '__main__':
app.run()