Update main.py
parent
3012d1a7a0
commit
b5b54b96c2
73
main.py
73
main.py
|
@ -1,4 +1,4 @@
|
||||||
from flask import Flask, render_template, redirect, request, session, url_for, g
|
from flask import Flask, render_template, redirect, request, session, url_for, g, send_from_directory
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
from flask_admin import Admin, AdminIndexView, expose, BaseView
|
from flask_admin import Admin, AdminIndexView, expose, BaseView
|
||||||
|
@ -12,10 +12,8 @@ from functools import wraps
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import csv
|
# import csv
|
||||||
from sqlalchemy import create_engine, Table, MetaData
|
# from sqlalchemy import create_engine, Table, MetaData
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = 'bebra'
|
app.secret_key = 'bebra'
|
||||||
|
@ -55,6 +53,8 @@ class ForumPost(db.Model):
|
||||||
media = db.relationship('Media', backref='forumpost', lazy=True)
|
media = db.relationship('Media', backref='forumpost', lazy=True)
|
||||||
text = db.Column(db.Text, nullable=False)
|
text = db.Column(db.Text, nullable=False)
|
||||||
edited = db.Column(db.Boolean, default=False)
|
edited = db.Column(db.Boolean, default=False)
|
||||||
|
upvotes = db.Column(db.Integer, default=0)
|
||||||
|
downvotes = db.Column(db.Integer, default=0)
|
||||||
|
|
||||||
class ForumComment(db.Model):
|
class ForumComment(db.Model):
|
||||||
__tablename__ = 'forumcomment'
|
__tablename__ = 'forumcomment'
|
||||||
|
@ -65,6 +65,8 @@ class ForumComment(db.Model):
|
||||||
media = db.Column(db.Integer)
|
media = db.Column(db.Integer)
|
||||||
text = db.Column(db.Text, nullable=False)
|
text = db.Column(db.Text, nullable=False)
|
||||||
edited = db.Column(db.Boolean, default=False)
|
edited = db.Column(db.Boolean, default=False)
|
||||||
|
upvotes = db.Column(db.Integer, default=0)
|
||||||
|
downvotes = db.Column(db.Integer, default=0)
|
||||||
|
|
||||||
post = relationship("ForumPost", primaryjoin="foreign(ForumComment.post_id) == remote(ForumPost.id)")
|
post = relationship("ForumPost", primaryjoin="foreign(ForumComment.post_id) == remote(ForumPost.id)")
|
||||||
|
|
||||||
|
@ -101,19 +103,7 @@ class UserAdminView(ModelView):
|
||||||
can_export = True
|
can_export = True
|
||||||
export_types = ['csv']
|
export_types = ['csv']
|
||||||
|
|
||||||
class PostAdminView(ModelView):
|
class AdminView(ModelView):
|
||||||
can_export = True
|
|
||||||
export_types = ['csv']
|
|
||||||
|
|
||||||
class ForumCategoryAdminView(ModelView):
|
|
||||||
can_export = True
|
|
||||||
export_types = ['csv']
|
|
||||||
|
|
||||||
class ForumPostAdminView(ModelView):
|
|
||||||
can_export = True
|
|
||||||
export_types = ['csv']
|
|
||||||
|
|
||||||
class ForumCommentAdminView(ModelView):
|
|
||||||
can_export = True
|
can_export = True
|
||||||
export_types = ['csv']
|
export_types = ['csv']
|
||||||
|
|
||||||
|
@ -125,10 +115,11 @@ class LogoutView(BaseView):
|
||||||
|
|
||||||
admin = Admin(app, name='Admin Panel', template_mode='bootstrap3', index_view=MyAdminIndexView())
|
admin = Admin(app, name='Admin Panel', template_mode='bootstrap3', index_view=MyAdminIndexView())
|
||||||
admin.add_view(UserAdminView(User, db.session))
|
admin.add_view(UserAdminView(User, db.session))
|
||||||
admin.add_view(PostAdminView(Post, db.session))
|
admin.add_view(AdminView(Post, db.session))
|
||||||
admin.add_view(ForumCategoryAdminView(ForumCategory, db.session))
|
admin.add_view(AdminView(ForumCategory, db.session))
|
||||||
admin.add_view(ForumPostAdminView(ForumPost, db.session))
|
admin.add_view(AdminView(ForumPost, db.session))
|
||||||
admin.add_view(ForumCommentAdminView(ForumComment, db.session))
|
admin.add_view(AdminView(ForumComment, db.session))
|
||||||
|
admin.add_view(AdminView(Media, db.session))
|
||||||
admin.add_view(LogoutView(name='Logout', endpoint='admin_logout'))
|
admin.add_view(LogoutView(name='Logout', endpoint='admin_logout'))
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
|
@ -245,6 +236,9 @@ def create_post(category_id):
|
||||||
|
|
||||||
form.category_id.choices = [(category.id, category.category_name) for category in ForumCategory.query.all()]
|
form.category_id.choices = [(category.id, category.category_name) for category in ForumCategory.query.all()]
|
||||||
|
|
||||||
|
if not g.user:
|
||||||
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
post_name = request.form['post_name']
|
post_name = request.form['post_name']
|
||||||
text = request.form['text']
|
text = request.form['text']
|
||||||
|
@ -253,13 +247,11 @@ def create_post(category_id):
|
||||||
media_filenames = []
|
media_filenames = []
|
||||||
for file in media_files:
|
for file in media_files:
|
||||||
if file:
|
if file:
|
||||||
filename = secure_filename(file.filename) # type: ignore
|
filename = secure_filename(file.filename)
|
||||||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||||||
media_filenames.append(filename)
|
media_filenames.append(filename)
|
||||||
|
|
||||||
user_nickname = None
|
user_nickname = g.user.username
|
||||||
if g.user:
|
|
||||||
user_nickname = g.user.username
|
|
||||||
|
|
||||||
new_post = ForumPost(
|
new_post = ForumPost(
|
||||||
category_id=category_id,
|
category_id=category_id,
|
||||||
|
@ -268,12 +260,12 @@ def create_post(category_id):
|
||||||
text=text,
|
text=text,
|
||||||
creation_date=datetime.now(),
|
creation_date=datetime.now(),
|
||||||
edited=False
|
edited=False
|
||||||
) # type: ignore
|
)
|
||||||
db.session.add(new_post)
|
db.session.add(new_post)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
for filename in media_filenames:
|
for filename in media_filenames:
|
||||||
new_media = Media(post_id=new_post.id, filename=filename) # type: ignore
|
new_media = Media(post_id=new_post.id, filename=filename)
|
||||||
db.session.add(new_media)
|
db.session.add(new_media)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -282,9 +274,8 @@ def create_post(category_id):
|
||||||
|
|
||||||
return render_template('create_post.html', form=form, category=category)
|
return render_template('create_post.html', form=form, category=category)
|
||||||
|
|
||||||
|
@app.route('/forums/<category_name>/<int:post_id>', methods=['GET', 'POST'])
|
||||||
@app.route('/forums/<int:post_id>', methods=['GET', 'POST'])
|
def view_post(category_name, post_id):
|
||||||
def view_post(post_id):
|
|
||||||
post = ForumPost.query.get_or_404(post_id)
|
post = ForumPost.query.get_or_404(post_id)
|
||||||
comments = ForumComment.query.filter_by(post_id=post_id).all()
|
comments = ForumComment.query.filter_by(post_id=post_id).all()
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
@ -293,7 +284,7 @@ def view_post(post_id):
|
||||||
new_comment = ForumComment(post_id=post_id, created_by=created_by, text=text) # type: ignore
|
new_comment = ForumComment(post_id=post_id, created_by=created_by, text=text) # type: ignore
|
||||||
db.session.add(new_comment)
|
db.session.add(new_comment)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('view_post', post_id=post_id))
|
return redirect(url_for('view_post', category_name=category_name, post_id=post_id))
|
||||||
return render_template('post.html', post=post, comments=comments)
|
return render_template('post.html', post=post, comments=comments)
|
||||||
|
|
||||||
@app.route('/forums/upvote_post/<int:post_id>')
|
@app.route('/forums/upvote_post/<int:post_id>')
|
||||||
|
@ -301,41 +292,45 @@ def upvote_post(post_id):
|
||||||
post = ForumPost.query.get_or_404(post_id)
|
post = ForumPost.query.get_or_404(post_id)
|
||||||
post.upvotes += 1
|
post.upvotes += 1
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('post', post_id=post_id))
|
return redirect(request.referrer)
|
||||||
|
|
||||||
@app.route('/forums/downvote_post/<int:post_id>')
|
@app.route('/forums/downvote_post/<int:post_id>')
|
||||||
def downvote_post(post_id):
|
def downvote_post(post_id):
|
||||||
post = ForumPost.query.get_or_404(post_id)
|
post = ForumPost.query.get_or_404(post_id)
|
||||||
post.downvotes += 1
|
post.downvotes += 1
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('post', post_id=post_id))
|
return redirect(request.referrer)
|
||||||
|
|
||||||
@app.route('/forums/upvote_comment/<int:comment_id>')
|
@app.route('/forums/upvote_comment/<int:comment_id>')
|
||||||
def upvote_comment(comment_id):
|
def upvote_comment(comment_id):
|
||||||
comment = ForumComment.query.get_or_404(comment_id)
|
comment = ForumComment.query.get_or_404(comment_id)
|
||||||
comment.upvotes += 1
|
comment.upvotes += 1
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('post', post_id=comment.post_id))
|
return redirect(request.referrer)
|
||||||
|
|
||||||
@app.route('/forums/downvote_comment/<int:comment_id>')
|
@app.route('/forums/downvote_comment/<int:comment_id>')
|
||||||
def downvote_comment(comment_id):
|
def downvote_comment(comment_id):
|
||||||
comment = ForumComment.query.get_or_404(comment_id)
|
comment = ForumComment.query.get_or_404(comment_id)
|
||||||
comment.downvotes += 1
|
comment.downvotes += 1
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('post', post_id=comment.post_id))
|
return redirect(request.referrer)
|
||||||
|
|
||||||
|
@app.route('/uploads/<path:filename>')
|
||||||
|
def uploaded_file(filename):
|
||||||
|
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
||||||
|
|
||||||
@app.route("/about")
|
@app.route("/about")
|
||||||
def about():
|
def about():
|
||||||
return render_template("about.html")
|
return render_template("about.html")
|
||||||
|
|
||||||
"""
|
|
||||||
def recreate_database():
|
def recreate_database():
|
||||||
if os.path.exists('Picture_Puzzle_web.db'):
|
if os.path.exists('Picture_Puzzle_web.db'):
|
||||||
os.remove('Picture_Puzzle_web.db')
|
os.remove('Picture_Puzzle_web.db')
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
"""
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# recreate_database()
|
recreate_database()
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
Loading…
Reference in New Issue