- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
Latest commit
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
fromflaskimportFlask, render_template, flash, redirect, url_for, request
fromconfigimportConfig
fromflask_sqlalchemyimportSQLAlchemy
fromflask_migrateimportMigrate
fromflask_loginimportLoginManager, current_user, login_user, logout_user, login_required
fromdatetimeimportdatetime
importlogging
fromlogging.handlersimportSMTPHandler
fromflask_mailimportMail
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)
frommodelsimportUser, Post
fromformsimportLoginForm, RegistrationForm, EditForm, EmptyForm, PostForm
ifTrue:
ifapp.config["MAIL_SERVER"]:
auth=None
ifapp.config["MAIL_USERNAME"] orapp.config["MAIL_PASSWORD"]:
auth= (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"])
secure=None
ifapp.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("/")
defindex():
ifcurrent_user.is_authenticated:
returnredirect(url_for("feed"))
returnrender_template("index.html")
@app.route("/feed", methods=["POST","GET"])
@login_required
deffeed():
page=request.args.get("page", 1, type=int)
form=PostForm()
ifform.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!")
returnredirect(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) ifposts.has_nextelseNone
prev_url=url_for('feed', page=posts.prev_num) ifposts.has_prevelseNone
returnrender_template("feed.html", posts=posts.items, form=form, next_url=next_url, prev_url=prev_url)
@app.route("/explore")
@login_required
defexplore():
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) ifposts.has_nextelseNone
prev_url=url_for('feed', page=posts.prev_num) ifposts.has_prevelseNone
returnrender_template("feed.html", posts=posts.items, next_url=next_url, prev_url=prev_url)
@app.route("/login", methods=["GET","POST"])
deflogin():
ifcurrent_user.is_authenticated:
returnredirect(url_for("index"))
form=LoginForm()
ifform.validate_on_submit():
user=User.query.filter_by(username=form.username.data).first()
ifuserisNoneornotuser.check_password(form.password.data):
flash("Invalid Username or password")
returnredirect(url_for("login"))
login_user(user, remember=form.remember_me.data)
flash("You have logged in.")
returnredirect(url_for("index"))
returnrender_template("login.html", form=form, title="Login")
@app.route("/logout")
deflogout():
logout_user()
returnredirect(url_for("index"))
@app.route("/signup", methods=["GET","POST"])
defsignup():
ifcurrent_user.is_authenticated:
returnredirect(url_for("index"))
form=RegistrationForm()
ifform.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()
returnrender_template("signup.html", form=form)
@app.route("/user/<username>")
defuser(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) ifposts.has_nextelseNone
prev_url=url_for('user', username=username, page=posts.prev_num) ifposts.has_prevelseNone
returnrender_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
defedit():
form=EditForm(current_user.username)
ifform.validate_on_submit():
current_user.username=form.username.data
current_user.about=form.about.data
db.session.commit()
returnredirect(url_for("user", username=current_user.username))
elifrequest.method=="GET":
form.username.data=current_user.username
form.about.data=current_user.about
returnrender_template("edit_account.html", form=form)
@app.route("/follow/<username>", methods=["GET","POST"])
@login_required
deffollow(username):
form=EmptyForm()
ifform.validate_on_submit():
user=User.query.filter_by(username=username).first()
ifuserisNone:
flash("User not found".format(username))
returnredirect(url_for("feed"))
ifuser==current_user:
flash("You can't follow yourself")
returnredirect(url_for("feed"))
current_user.follow(user)
db.session.commit()
flash("You are now following {}".format(str(user.username)))
returnredirect(url_for("user", username=username))
else:
returnredirect(url_for("feed"))
@app.route("/unfollow/<username>", methods=["GET","POST"])
@login_required
defunfollow(username):
form=EmptyForm()
ifform.validate_on_submit():
user=User.query.filter_by(username=username).first()
ifuserisNone:
flash("User not found".format(username))
returnredirect(url_for("feed"))
ifuser==current_user:
flash("You can't unfollow yourself")
returnredirect(url_for("feed"))
current_user.unfollow(user)
db.session.commit()
flash("You are now not following {}".format(str(user.username)))
returnredirect(url_for("user", username=username))
else:
returnredirect(url_for("feed"))
@app.before_request
defbefore_request():
ifcurrent_user.is_authenticated:
current_user.last_seen=datetime.utcnow()
db.session.commit()
@app.errorhandler(404)
defnot_found_error(error):
returnrender_template("404.html")
@app.errorhandler(500)
definternal_server_error(error):
returnrender_template("500.html")