123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- from flask import Flask, redirect, render_template, request, flash
- from login import *
- import helper, json, time
- app = Flask(__name__)
- db = helper.createDb(app)
- Topic, Reply, db = db["Topic"], db["Reply"], db["db"]
- User = initLogin(app, db)
- db.create_all()
- def getTime():
- return time.asctime(time.localtime(time.time()))
- @app.route("/login") # Render the login page, nothing more
- def renderLogin():
- logout_user()
- return render_template("login.html")
- @app.route("/login/post", methods=["POST"])
- def login():
- try:
- loginUser(request.form["username"], request.form["password"], User)
- return redirect("/")
- except:
- try:
- createUser(request.form["username"], request.form["password"], db, User)
- return redirect("/")
- except:
- return redirect("/login")
- @app.route("/") # Render the homepage
- def renderHome():
- return render_template(
- "index.html", topics=Topic.query.order_by(Topic.lastActivity.desc())
- )
- @app.route("/post") # Render the 'write new topic' box
- @login_required
- def renderCreateTopic():
- return render_template("post.html")
- @app.route("/post/post", methods=["POST"]) # Backend of the new topic box
- def createTopic():
- topic = Topic(
- request.form["title"],
- request.form["content"],
- getTime(),
- getUsername(),
- request.form["category"],
- )
- db.session.add(topic)
- db.session.commit()
- return redirect("/topic/" + str(topic.id))
- @app.route("/topic/<id>") # Render a topic
- def renderTopic(id):
- topic = Topic.query.filter_by(id=id).first_or_404()
- topic.views += 1
- db.session.add(topic)
- db.session.commit()
- return render_template(
- "topic.html", topic=topic, replies=Reply.query.filter_by(inReplyTo=id)
- )
- @app.route("/reply/<id>", methods=["POST"]) # Reply to a post.
- @login_required
- def replyTo(id):
- topic = Topic.query.filter_by(id=id).first_or_404()
- topic.reply(getTime())
- reply = Reply(
- request.form["body"], getTime(), current_user.username, id
- )
- db.session.add(reply)
- db.session.add(topic)
- db.session.commit()
- return redirect("/topic/" + str(id))
- @app.route("/like/<id>") # Like a topic
- @login_required
- def likeTopic(id):
- topic = Topic.query.filter_by(id=id).first_or_404()
- topic.like(current_user.username)
- db.session.add(topic)
- db.session.commit()
- return redirect("/topic/" + str(id))
- @app.route("/like/reply/<id>/<idt>") # Like a reply
- @login_required
- def likeReply(id, idt):
- reply = Reply.query.filter_by(id=id).first_or_404()
- reply.like(current_user.username)
- db.session.add(reply)
- db.session.commit()
- return redirect("/topic/" + str(idt))
- @app.route(
- "/top"
- )
- def topList():
- topics = Topic.query.order_by(Topic.repliesNum.desc())
- return render_template("index.html", topics=topics)
- @app.route("/new") # Order the list like normal (redirect)
- def redirectIndex():
- return redirect("/")
- @app.route("/cat/<category>") # Get the list of posts in a category
- def catList(category):
- topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
- return render_template("index.html", topics=topics)
- app.run(debug=True)
|