Skip to content
Snippets Groups Projects
routes.py 4.31 KiB
Newer Older
Xingchen Zhou's avatar
Xingchen Zhou committed
from flask import render_template, url_for, request, redirect, flash
from blog import app, db, resetDB
from blog.models import User, Post, Comment, Rate
from blog.forms import RegistrationForm, LoginForm, CommentForm,PostForm, RateForm
from flask_login import login_user, logout_user, login_required, current_user
import re

@app.route("/")
@app.route("/home")
def home():
  posts=Post.query.all()

  TAG_RE = re.compile(r'<[^>]+>')

  for post in posts:
    post.shortcontent = TAG_RE.sub('', post.content)
    post.shortcontent = post.shortcontent[:100]

  return render_template('home.html',posts=posts)

@app.route("/allposts")
def allposts():
  sort = request.args.get('sorttype')
  search = request.args.get('searchtext')

  if not search : 
    search = ''

  if (sort=="asc"):
    posts=Post.query.filter(Post.content.contains(search),Post.title.contains(search)).order_by(Post.date.asc())
  else :
    posts=Post.query.filter(Post.content.contains(search),Post.title.contains(search)).order_by(Post.date.desc())

  return render_template('allposts.html',posts=posts)

@app.route("/aboutus")
def aboutus():
  return render_template('aboutus.html', title='About')

@app.route("/license")
def license():
  return render_template('license.html', title='License')

@app.route("/search",methods=['GET','POST'])
def search():
  searchtext = request.form.get('search');
  return redirect(url_for('allposts', searchtext=searchtext))


@app.route("/post/<int:post_id>")
def post(post_id):
  post = Post.query.get_or_404(post_id)
  comments = Comment.query.filter(Comment.post_id==post.id)
  form=CommentForm()

  rates=Rate.query.filter(Rate.post_id==post.id)
  sRate = 0
  countRate = 0
  for rate in rates :
    sRate = sRate + rate.rateValue
    countRate = countRate + 1
  if countRate == 0 :
    sRate = 0
  else :
    sRate = sRate / countRate
  sRate = int(sRate)
  return render_template('post.html',post=post,comments=comments,rate=sRate,form=form)

@app.route('/postcomment/<int:post_id>',methods=['GET','POST'])
@login_required
def post_comment(post_id):
  post=Post.query.get_or_404(post_id)
  form=CommentForm()
  if form.validate_on_submit():
    db.session.add(Comment(content=form.comment.data,post_id=post.id,author_id=current_user.id))
    db.session.commit()
    return redirect(f'/post/{post.id}')
  comments=Comment.query.filter(Comment.post_id==post.id)
  rates=Rate.query.filter(Rate.post_id==post.id)
  sRate = 0
  countRate = 0
  for rate in rates :
    sRate = sRate + rate.rateValue
    countRate = countRate + 1
  if countRate == 0 :
    sRate = 0
  else :
    sRate = sRate / countRate
  sRate = int(sRate)
  return render_template('post.html',post=post,comments=comments,rate=sRate,form=form)

@app.route('/ratepost/<int:post_id>',methods=['GET','POST'])
@login_required
def post_rate(post_id):
  post=Post.query.get_or_404(post_id)
  rateValue = request.args.get('rate')
  rateObj=Rate.query.filter(Rate.post_id==post_id,Rate.author_id==current_user.id).first()

  if rateObj :
    rateObj.rateValue=rateValue
  else :
    db.session.add(Rate(rateValue=rateValue,post_id=post.id,author_id=current_user.id))

  db.session.commit()

  return "done"

@app.route("/register",methods=['GET','POST'])
def register():
  form = RegistrationForm()
  if form.validate_on_submit():
    user = User(username=form.username.data,email=form.email.data,password=form.password.data)
    db.session.add(user)
    db.session.commit()
    flash('Registration successful!')
    return redirect(url_for('home'))
  return render_template('register.html',title='Register',form=form)

@app.route("/login",methods=['GET','POST'])
def login():
  form = LoginForm()
  if form.validate_on_submit():
    user = User.query.filter_by(email=form.email.data).first()
    if user is not None and user.verify_password(form.password.data):
      login_user(user)
      flash('Login successful!')
      return redirect(url_for('home'))
    flash('Invalid email address or password, please try again')
    
    return render_template('login.html',form=form)

  return render_template('login.html',title='Login',form=form)

@app.route("/logout")
def logout():
  logout_user()
  return redirect(url_for('home'))

@app.route("/resetdb")
def resetdb():
  resetDB.resetDB(db)
  return "done"