-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
165 lines (132 loc) · 5.44 KB
/
users.py
File metadata and controls
165 lines (132 loc) · 5.44 KB
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
import requests
import pdb
import os
import mtgsdk
import flask_paginate
from app import g
from flask import Flask, Blueprint, session, request, render_template, redirect, flash
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, User, Friendship, Card, Bookmark, Deck, CardDeck, Post, bcrypt
from forms import LoginForm, RegisterForm, DeckForm, EditUserForm, NewPostForm
users_blueprint = Blueprint('users_blueprint', __name__, static_folder='static',
template_folder='templates')
CURR_USER_KEY = 'curr-user'
def do_logout():
"""Logout user."""
if CURR_USER_KEY in session:
user = User.query.get(session[CURR_USER_KEY])
del session[CURR_USER_KEY]
# flash(f"Goodbye, {user.username}!")
@users_blueprint.route('/register', methods=['GET', 'POST'])
def register():
"""
GET: Renders the template for a user to login
POST: Submits register form and creates new user & logs in
"""
if g.user:
return redirect('/home')
form = RegisterForm()
if form.validate_on_submit():
return handle_register_form_errors(form)
return render_template('register.html', form=form)
def handle_register_form_errors(form):
"""Determines & handles errors found in register user form. If no errors found, creates new user."""
if User.query.get(form.username.data):
flash(
f'The username "{form.username.data}" is already taken', 'danger')
return redirect('/register')
if len(User.query.filter(User.email == form.email.data).all()) > 0:
flash('That email address is already taken', 'danger')
return redirect('/register')
if not check_confirmed_pwd(form.password.data, form.confirmed_password.data):
flash('Passwords must match - please try again.', 'danger')
return redirect('/register')
return complete_register(form)
def complete_register(form):
"""Creates a new user from the form data and logs that user in"""
image_url = form.image_url.data or "/static/images/default_prof_pic.png"
if User.signup(username=form.username.data, password=form.password.data,
email=form.email.data, image_url=image_url):
db.session.commit()
session[CURR_USER_KEY] = form.username.data
return redirect('/home')
return redirect('/login')
@users_blueprint.route('/login', methods=['GET', 'POST'])
def login():
"""
GET: Renders the template for a user to login
POST: Submits login form and signs in user if correct username & password
"""
if g.user:
return redirect('/home')
form = LoginForm()
if form.validate_on_submit():
user = User.authenticate(
username=form.username.data, password=form.password.data)
if user:
db.session.add(user)
db.session.commit()
session[CURR_USER_KEY] = form.username.data
return redirect('/home')
flash('Username or password is incorrect', 'danger')
return redirect('/login')
return render_template('login.html', form=form)
@users_blueprint.route('/logout')
def logout():
"""Logs out a user and redirets them to the login page"""
if g.user:
do_logout()
return redirect('/login')
def check_confirmed_pwd(pwd, confirmed_pwd):
"""Checks that the confirmed password matches upon registering"""
if pwd != confirmed_pwd:
return False
return True
@users_blueprint.route('/users/<string:username>', methods=['GET', 'POST'])
def user_profile(username):
"""
GET: Route for viewing a user's profile
POST: Create a new Post instance and put it on your page
"""
if g.user:
form = NewPostForm()
if g.user.username == username:
if form.validate_on_submit():
post = Post(username=username, title=form.title.data,
content=form.content.data)
db.session.add(post)
db.session.commit()
return redirect(f'/users/{username}')
user = User.query.get(username)
return render_template('user.html', user=user, form=form)
return redirect('/login')
@users_blueprint.route('/users/<string:username>/edit', methods=['GET', 'POST'])
def edit_profile(username):
"""
GET: Route for editting your profile
POST: Update the user's data
"""
if g.user:
form = EditUserForm()
user = User.query.get(g.user.username)
if form.validate_on_submit():
user.email = form.email.data
user.image_url = form.image_url.data or "/static/images/default_prof_pic.png"
if check_confirmed_pwd(form.password.data, form.confirmed_password.data):
user.password = bcrypt.generate_password_hash(
form.password.data).decode('UTF-8')
else:
flash('Passwords do not match - please try again.', 'danger')
return redirect(f'/users/{user.username}/edit')
db.session.add(user)
db.session.commit()
return redirect('/home')
populate_edit_profile_fields(form, user)
return render_template('edit_user.html', form=form)
return render_template('/login')
def populate_edit_profile_fields(form, user):
"""Populates edit user form fields"""
form.email.data = user.email
form.password.data = user.password
form.confirmed_password.data = user.password
form.image_url.data = user.image_url