-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
201 lines (175 loc) · 6.93 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin, BaseView, expose, AdminIndexView
from flask_admin.contrib.sqla import ModelView
from flask_login import UserMixin, LoginManager, login_user, login_required, logout_user, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
from dotenv import load_dotenv
from flask_mail import Mail, Message
from forms import *
import os
load_dotenv()
app = Flask(__name__)
app.config.update(
MAIL_SERVER=os.getenv("MAIL_SERVER"),
MAIL_PORT=os.getenv("MAIL_PORT"),
MAIL_USE_SSL=os.getenv("MAIL_USE_SSL"),
MAIL_USERNAME=os.getenv("MAIL_USERNAME"),
MAIL_PASSWORD=os.getenv("MAIL_PASSWORD")
)
mail = Mail(app)
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("SQLALCHEMY_DATABASE_URI")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = os.getenv('SECRET_KEY')
db = SQLAlchemy(app)
from models import *
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
class Anypageview(BaseView):
@expose('/')
def anypage(self):
return self.render('admin/anypage/index.html')
class Dashboardview(AdminIndexView):
@expose('/')
@login_required
def add_data_db(self):
return self.render('admin/dashboard_admin.html')
admin = Admin(app, template_mode='bootstrap4', index_view=Dashboardview())
admin.add_view(ModelView(User, db.session, name="Users"))
admin.add_view(ModelView(Post, db.session, name="Posts"))
admin.add_view(ModelView(Comment, db.session, name="Comments"))
admin.add_view(Anypageview(name="Pages of the website"))
def send_mail(Message, UserMail, Body):
msg = mail.send_message(
Message,
sender=os.getenv("MAIL_USERNAME"),
recipients=[UserMail],
body=Body
)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def index():
return render_template("index.html")
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html')
@app.errorhandler(503)
def server_overloaded(e):
return "Server is down"
@app.errorhandler(429)
def too_many_request(e):
return "Too many request"
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('profile'))
form = LoginForm()
if form.validate_on_submit():
existing_user = User.query.filter_by(username=form.username.data).first()
if existing_user:
if check_password_hash(existing_user.password, form.password.data):
login_user(existing_user)
flash('Logged in successfully!')
return redirect(url_for('profile'))
else:
flash('Incorrect password')
return redirect(url_for('login'))
else: flash('There is no such user')
return redirect(url_for('login'))
return render_template("login.html", form=form)
@app.route('/profile', methods=['GET', 'POST'])
@login_required
def profile():
form = PostForm()
list_posts = current_user.posts
if form.validate_on_submit():
newPost = Post(text=form.text.data, title=form.title.data, user_id=current_user.id, author=current_user.username)
db.session.add(newPost)
db.session.commit()
return redirect(url_for('blog', ID_POST=newPost.id))
return render_template('dashboard.html', form=form, list=list_posts)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for("login"))
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('profile'))
form = RegisterForm()
if form.validate_on_submit():
existing_user = User.query.filter_by(username=form.username.data).first()
if existing_user is None:
new_user = User(username=form.username.data, email=form.email.data, password=generate_password_hash(form.password.data))
db.session.add(new_user)
try:
db.session.commit()
login_user(new_user)
send_mail(Message="You successfully registered!", UserMail=new_user.email, Body="You're a new user! Try out the features of the application, and contact me if you have some suggestions")
return redirect(url_for('profile'))
except:
db.session.rollback()
else:
flash('Such username already exists')
return redirect(url_for("register"))
return render_template("register.html", form=form)
@app.route('/Forum', methods=["GET", "POST"])
def forum():
form = SeachPostForm()
user = current_user
if form.validate_on_submit():
query = form.title.data
post_list = Post.query.filter(Post.title.like("%"+query+"%")).all()
return render_template("Forum.html", list=post_list, form=form)
post_list = Post.query.all()
post_list.sort(key=lambda x : x.timestamp, reverse=True)
return render_template("Forum.html", list=post_list, form=form)
@app.route('/Forum/<ID_POST>', methods=["GET", "POST"])
def blog(ID_POST):
comment_form = NoteForm()
current_post = Post.query.filter_by(id=ID_POST).first()
comment_list = current_post.comments
if request.method == 'POST':
if current_user.is_authenticated:
if comment_form.validate_on_submit():
new_comment = Comment(text=comment_form.text.data, user_id=current_user.id, post_id=ID_POST)
db.session.add(new_comment)
db.session.commit()
return redirect(url_for('blog', ID_POST=ID_POST))
else:
return redirect(url_for('register'))
return render_template("topic.html", current_post=current_post, form=comment_form, comment_list=comment_list)
@app.route('/delete_blog/<ID_POST>')
@login_required
def delete_blog(ID_POST):
current_post = Post.query.get(ID_POST)
if current_post and current_post.user_id == current_user.id:
db.session.delete(current_post)
db.session.commit()
return redirect(url_for('forum'))
@app.route('/update_blog/<ID_POST>', methods=["GET", "POST"])
@login_required
def update_blog(ID_POST):
current_post = Post.query.get(ID_POST)
form = PostForm()
if current_post and current_post.user_id == current_user.id:
if form.validate_on_submit():
current_post.text = form.text.data
current_post.title = form.title.data
db.session.commit()
return redirect(url_for('blog', ID_POST=ID_POST))
form.title.data = current_post.title
form.text.data = current_post.text
return render_template('edit_post.html', form=form)
@app.route('/token')
@login_required
def token():
token = current_user.gen_token()
return token
if __name__=="__main__":
app.run(host="0.0.0.0", port=80)