-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
154 lines (143 loc) · 5.44 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
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
from flask import Flask, render_template, redirect, session, flash, request
from pymongo import MongoClient
from forms import JoinForm, LoginForm, ContentForm, SettingsForm
from werkzeug.security import check_password_hash, generate_password_hash
from flask_mail import Mail, Message
from flask_bootstrap import Bootstrap
from datetime import datetime
import logging
import traceback
app = Flask(__name__)
app.config.from_pyfile('settings.cfg')
client = MongoClient(app.config.get('MONGO_URI'))
db = client.dandelion
mail = Mail(app)
bootstrap = Bootstrap(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/join', methods=['GET', 'POST'])
def join():
form = JoinForm()
if request.method == 'POST':
if form.validate_on_submit():
# get form data
username = form.username.data
learner_name = form.learner_name.data
email = form.email.data
hashed_password = generate_password_hash(form.password.data)
# add form data to database
info = {
"username": username,
"password": hashed_password,
"email": email,
"learner_name": learner_name,
'added': datetime.utcnow(),
}
db.users.insert_one(info)
# add info to session
session["email"] = email
session["username"] = username
session["learner_name"] = learner_name
# log
app.logger.info('%s joined successfully', username)
# flash message
flash('You have successfully registered and logged in')
return redirect('/dashboard')
else:
# add what happens if it doesn't validate
for item in form.errors.items():
app.logger.error("item")
return render_template('join.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
if "username" not in session:
form = LoginForm()
if request.method == 'POST':
if form.validate_on_submit():
# get form data
password = form.password.data
username = form.username.data
user_info = db.users.find_one({"username": username})
# check password
hashed_password = user_info["password"]
if check_password_hash(hashed_password, password):
# add user to session and redirect to dashboard
session['username'] = username
# log
app.logger.info('%s joined successfully', username)
flash('You have successfully logged in')
return redirect("/dashboard")
else:
# add what happens if passwords don't match
flash('Please check your password and try again')
for item in form.errors.items():
app.logger.error("item")
return redirect("/login")
else:
return redirect('/dashboard')
return render_template('login.html', form=form)
@app.route('/logout')
def logout():
# log
app.logger.info('%s logged out successfully', session["username"])
session.clear()
return redirect('/login')
@app.route('/dashboard')
def dashboard():
username = session['username']
# add what happens if a user ends up here with no username in their session
app.logger.info('%s visited the dashboard', session["username"])
if username == None:
return redirect("/login")
return render_template('dashboard.html', username=username)
@app.route('/settings', methods=['GET', 'POST'])
def settings():
form = SettingsForm()
if request.method == 'POST':
if form.validate_on_submit():
# get form data
test = form.test.data
# add to db
info = { "settings": {
'test': test,
}
}
db.users.update_one({
"username": session['username']
}, {
"$set": info
})
# return to add content page
app.logger.info('%s updated settings', session["username"])
flash("Settings updated")
return redirect("/dashboard")
return render_template('settings.html', form=form)
@app.route('/add_content', methods=['GET', 'POST'])
def add_content():
form = ContentForm()
if request.method == 'POST':
if form.validate_on_submit():
# get form data
title = form.title.data
content = form.content.data
category = form.category.data
# add to db
info = {
'title': title,
'content': content,
'category': category,
'added': datetime.utcnow(),
'user': session['username'],
}
db.content.insert_one(info)
# return to add content page
message = '"' + title + '" added successfully'
log_message = "%s added " + title
app.logger.info(log_message, session["username"])
flash(message)
return redirect("/add_content")
return render_template('add_content.html', form=form)
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s - %(message)s', filename='error.log',level=logging.DEBUG)
app.run(debug=True, host='0.0.0.0')