-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
147 lines (112 loc) · 4.31 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
from db import init
from flask import Flask, request, redirect, session, url_for, render_template, flash
from tweets import add_tweet, get_all_tweets, get_tweets_by_username
from users import get_all_users, password_match, get_user_by_username, create_user, get_all_users_following, get_all_users_unfollowing
import random
app = Flask(__name__)
app.secret_key = "super secret key"
init()
# inject username
@app.context_processor
def inject_user():
username = session.get('user', None)
return {'username': username}
@app.route('/')
def index():
# check if the user is logged
if 'user' in session:
# Get last 10 tweets
tweets = get_all_tweets(10)
return render_template('home.html', tweets=tweets)
else:
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
# Getting username and password from the form
username = request.form['username']
password = request.form['password']
if not username or not password:
flash('Username or password cannot be blank!', 'error')
return render_template('login.html')
authenticated = password_match(username, password)
if authenticated:
# Use Flask session
session['user'] = username
flash('Login was successful!', 'info')
return redirect(url_for('index'))
else:
flash('Login Failed. Invalid username or password!', 'error')
return render_template('login.html')
@app.route('/logout')
def logout():
# Use flask session
del session['user']
flash('Logout was successful!', 'info')
return redirect(url_for('index'))
@app.route('/register', methods=['GET'])
def register():
return render_template('register.html')
@app.route('/register_post', methods=['POST'])
def register_post():
username = request.form['username']
password = request.form['password']
if not username or password:
flash('Username or password cannot be blank!', 'error')
return render_template('register.html')
try:
create_user(username, password)
user = get_user_by_username(username)
except Exception as e:
flash(f'Error registering {username}: {e}', 'error')
else:
flash(f'Successfully registered {username}', 'info')
print(user)
return redirect(url_for('index'))
@app.route('/tweet')
def tweet():
return render_template('form.html', action='/save-tweet', header='What is happening?',
fieldtitle='Tweet', fieldname='tweet', buttonvalue='Tweet')
@app.route('/save-tweet', methods=['POST'])
def contact():
tweet = request.form['tweet']
if not tweet:
flash('Tweet can not be empty!', 'error')
return render_template('form.html', action='/save-tweet', header='What is happening?', fieldtitle='Tweet', fieldname='tweet', buttonvalue='Tweet')
# change user info from session
add_tweet(tweet, session['user'])
flash('Tweet posted successfully', 'info')
return redirect(url_for('index'))
@app.route('/users', methods=['GET'])
def users():
# following_users = get_all_users_following()
# unfollowing_users = get_all_users_unfollowing()
following_users = []
unfollowing_users = []
all_users = get_all_users()
final_users = []
for user in all_users:
# add the random bool component to user that implies following/ unfollowing
random_follow = random.choice([True, False])
user = (user[0], user[1], user[2], random_follow)
final_users.append(user)
# add users based on the bool to following/ unfollowing users
if random_follow:
unfollowing_users.append(user)
else:
following_users.append(user)
print(final_users)
return render_template('users_page.html', all_users=final_users, following_users=following_users, unfollowing_users=unfollowing_users)
@app.route('/tweets/<username>')
@app.route('/tweets')
def user_tweets(username=None):
if 'user' not in session:
return redirect(url_for('index'))
if username:
tweets = get_tweets_by_username(username)
else:
tweets = get_all_tweets()
return render_template('tweets_page.html', tweets=tweets, tweet_user=username)
if __name__ == '__main__':
app.debug = True
app.run()(host='0.0.0.0', port=81)
# app.run(host='0.0.0.0', port=81)