-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
178 lines (161 loc) · 6.64 KB
/
Copy pathapp.py
File metadata and controls
178 lines (161 loc) · 6.64 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
166
167
168
169
170
171
172
173
174
175
176
177
178
from flask import Flask, render_template, flash, redirect, url_for, request
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager, current_user, login_user, logout_user, login_required
from datetime import datetime
import logging
from logging.handlers import SMTPHandler
from flask_mail import Mail
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view="login"
mail=Mail(app)
from models import User, Post
from forms import LoginForm, RegistrationForm, EditForm, EmptyForm, PostForm
if True:
if app.config["MAIL_SERVER"]:
auth = None
if app.config["MAIL_USERNAME"] or app.config["MAIL_PASSWORD"]:
auth = (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"])
secure = None
if app.config["MAIL_USE_TLS"]:
secure=()
mail_handler = SMTPHandler(
mailhost =(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
fromaddr="pythonatsea@gmail.com",
toaddrs=app.config["ADMINS"], subject="Flasknet Error",
credentials=auth, secure=secure
)
mail_handler.setLevel(logging.INFO)
print(mail_handler)
app.logger.addHandler(mail_handler)
@app.route("/")
def index():
if current_user.is_authenticated:
return redirect(url_for("feed"))
return render_template("index.html")
@app.route("/feed", methods=["POST","GET"])
@login_required
def feed():
page = request.args.get("page", 1, type=int)
form = PostForm()
if form.validate_on_submit():
post = Post(body=form.post.data, author=current_user)
db.session.add(post)
db.session.commit()
flash("Your post is now live!")
return redirect(url_for("feed"))
posts=current_user.followed_posts().paginate(page=page, per_page=app.config["POSTS_PER_PAGE"])
next_url = url_for('feed', page=posts.next_num) if posts.has_next else None
prev_url = url_for('feed', page=posts.prev_num) if posts.has_prev else None
return render_template("feed.html", posts=posts.items, form=form, next_url=next_url, prev_url=prev_url)
@app.route("/explore")
@login_required
def explore():
page = request.args.get("page", 1, type=int)
posts=Post.query.order_by(Post.timestamp.desc()).paginate(page=page, per_page=app.config["POSTS_PER_PAGE"])
next_url = url_for('feed', page=posts.next_num) if posts.has_next else None
prev_url = url_for('feed', page=posts.prev_num) if posts.has_prev else None
return render_template("feed.html", posts=posts.items, next_url=next_url, prev_url=prev_url)
@app.route("/login", methods=["GET","POST"])
def login():
if current_user.is_authenticated:
return redirect(url_for("index"))
form=LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash("Invalid Username or password")
return redirect(url_for("login"))
login_user(user, remember=form.remember_me.data)
flash("You have logged in.")
return redirect(url_for("index"))
return render_template("login.html", form=form, title="Login")
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for("index"))
@app.route("/signup", methods=["GET","POST"])
def signup():
if current_user.is_authenticated:
return redirect(url_for("index"))
form = RegistrationForm()
if form.validate_on_submit():
u = User(username=form.username.data, email=form.email.data)
u.set_password(form.password.data)
db.session.add(u)
db.session.commit()
return render_template("signup.html", form=form)
@app.route("/user/<username>")
def user(username):
form=EmptyForm()
page = request.args.get("page", 1, type=int)
user = User.query.filter_by(username=username).first_or_404()
posts = user.post.order_by(Post.timestamp.desc()).paginate(page=page, per_page=app.config["POSTS_PER_PAGE"])
next_url = url_for('user', username=username,page=posts.next_num) if posts.has_next else None
prev_url = url_for('user', username=username, page=posts.prev_num) if posts.has_prev else None
return render_template("user.html", user=user, posts=posts.items, form=form, next_url=next_url, prev_url=prev_url)
@app.route("/edit", methods=["GET","POST"])
@login_required
def edit():
form = EditForm(current_user.username)
if form.validate_on_submit():
current_user.username = form.username.data
current_user.about = form.about.data
db.session.commit()
return redirect(url_for("user", username=current_user.username))
elif request.method == "GET":
form.username.data = current_user.username
form.about.data = current_user.about
return render_template("edit_account.html", form=form)
@app.route("/follow/<username>", methods=["GET","POST"])
@login_required
def follow(username):
form = EmptyForm()
if form.validate_on_submit():
user = User.query.filter_by(username=username).first()
if user is None:
flash("User not found".format(username))
return redirect(url_for("feed"))
if user == current_user:
flash("You can't follow yourself")
return redirect(url_for("feed"))
current_user.follow(user)
db.session.commit()
flash("You are now following {}".format(str(user.username)))
return redirect(url_for("user", username=username))
else:
return redirect(url_for("feed"))
@app.route("/unfollow/<username>", methods=["GET","POST"])
@login_required
def unfollow(username):
form = EmptyForm()
if form.validate_on_submit():
user = User.query.filter_by(username=username).first()
if user is None:
flash("User not found".format(username))
return redirect(url_for("feed"))
if user == current_user:
flash("You can't unfollow yourself")
return redirect(url_for("feed"))
current_user.unfollow(user)
db.session.commit()
flash("You are now not following {}".format(str(user.username)))
return redirect(url_for("user", username=username))
else:
return redirect(url_for("feed"))
@app.before_request
def before_request():
if current_user.is_authenticated:
current_user.last_seen = datetime.utcnow()
db.session.commit()
@app.errorhandler(404)
def not_found_error(error):
return render_template("404.html")
@app.errorhandler(500)
def internal_server_error(error):
return render_template("500.html")