|
@@ -1,79 +1,126 @@
|
|
-from flask import Flask, redirect, render_template, request, flash
|
|
+from flask import Flask, redirect, render_template, request, flash
|
|
-from login import *
|
|
+from login import *
|
|
-import helper, json, time
|
|
+import helper, json, time
|
|
-app = Flask(__name__)
|
|
+
|
|
-db = helper.createDb(app)
|
|
+app = Flask(__name__)
|
|
-Topic, Reply, db = db['Topic'], db['Reply'], db['db']
|
|
+db = helper.createDb(app)
|
|
-User = initLogin(app, db)
|
|
+Topic, Reply, db = db["Topic"], db["Reply"], db["db"]
|
|
|
|
+User = initLogin(app, db)
|
|
db.create_all()
|
|
db.create_all()
|
|
-def getTime(): return time.asctime( time.localtime(time.time()) )
|
|
|
|
|
|
|
|
-@app.route('/login')
|
|
|
|
-def renderLogin(): logout_user(); return render_template('login.html')
|
|
|
|
|
|
|
|
-@app.route('/login/post', methods=['POST'])
|
|
+def getTime():
|
|
-def login():
|
|
+ return time.asctime(time.localtime(time.time()))
|
|
- try: loginUser(request.form['username'], request.form['password'], User); return redirect('/')
|
|
+
|
|
- except:
|
|
+
|
|
- try: createUser(request.form['username'], request.form['password'], db, User); return redirect('/')
|
|
+@app.route("/login")
|
|
- except: return redirect('/login')
|
|
+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('/')
|
|
+@app.route("/")
|
|
def renderHome():
|
|
def renderHome():
|
|
- return render_template('index.html', topics=Topic.query.order_by(Topic.lastActivity.desc()))
|
|
+ return render_template(
|
|
|
|
+ "index.html", topics=Topic.query.order_by(Topic.lastActivity.desc())
|
|
|
|
+ )
|
|
|
|
|
|
-@app.route('/post')
|
|
|
|
-@login_required
|
|
|
|
-def renderCreateTopic(): return render_template('post.html')
|
|
|
|
|
|
|
|
-@app.route('/post/post', methods=['POST'])
|
|
+@app.route("/post")
|
|
|
|
+@login_required
|
|
|
|
+def renderCreateTopic():
|
|
|
|
+ return render_template("post.html")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@app.route("/post/post", methods=["POST"])
|
|
def createTopic():
|
|
def createTopic():
|
|
- topic = Topic(request.form['title'], request.form['content'], getTime(), getUsername(), request.form['category'])
|
|
+ topic = Topic(
|
|
|
|
+ request.form["title"],
|
|
|
|
+ request.form["content"],
|
|
|
|
+ getTime(),
|
|
|
|
+ getUsername(),
|
|
|
|
+ request.form["category"],
|
|
|
|
+ )
|
|
db.session.add(topic)
|
|
db.session.add(topic)
|
|
db.session.commit()
|
|
db.session.commit()
|
|
- return redirect('/topic/' + str(topic.id))
|
|
+ return redirect("/topic/" + str(topic.id))
|
|
|
|
|
|
-@app.route('/topic/<id>')
|
|
+
|
|
|
|
+@app.route("/topic/<id>")
|
|
def renderTopic(id):
|
|
def renderTopic(id):
|
|
- topic = Topic.query.filter_by(id=id).first_or_404(); topic.views += 1
|
|
+ topic = Topic.query.filter_by(id=id).first_or_404()
|
|
- db.session.add(topic); db.session.commit()
|
|
+ topic.views += 1
|
|
- return render_template('topic.html', topic=topic, replies=Reply.query.filter_by(inReplyTo=id))
|
|
+ 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'])
|
|
+
|
|
|
|
+@app.route("/reply/<id>", methods=["POST"])
|
|
@login_required
|
|
@login_required
|
|
def replyTo(id):
|
|
def replyTo(id):
|
|
- topic = Topic.query.filter_by(id=id).first_or_404(); topic.reply(getTime())
|
|
+ topic = Topic.query.filter_by(id=id).first_or_404()
|
|
- reply = Reply(request.form['body'], getTime(), current_user.username, id)
|
|
+ topic.reply(getTime())
|
|
- db.session.add(reply); db.session.add(topic); db.session.commit()
|
|
+ reply = Reply(
|
|
- return redirect('/topic/' + str(id))
|
|
+ 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>')
|
|
+
|
|
|
|
+@app.route("/like/<id>")
|
|
@login_required
|
|
@login_required
|
|
def likeTopic(id):
|
|
def likeTopic(id):
|
|
topic = Topic.query.filter_by(id=id).first_or_404()
|
|
topic = Topic.query.filter_by(id=id).first_or_404()
|
|
- topic.like(current_user.username)
|
|
+ topic.like(current_user.username)
|
|
- db.session.add(topic); db.session.commit()
|
|
+ db.session.add(topic)
|
|
- return redirect('/topic/' + str(id))
|
|
+ db.session.commit()
|
|
|
|
+ return redirect("/topic/" + str(id))
|
|
|
|
|
|
-@app.route('/like/reply/<id>/<idt>')
|
|
+
|
|
|
|
+@app.route("/like/reply/<id>/<idt>")
|
|
@login_required
|
|
@login_required
|
|
def likeReply(id, idt):
|
|
def likeReply(id, idt):
|
|
reply = Reply.query.filter_by(id=id).first_or_404()
|
|
reply = Reply.query.filter_by(id=id).first_or_404()
|
|
- reply.like(current_user.username)
|
|
+ reply.like(current_user.username)
|
|
- db.session.add(reply); db.session.commit()
|
|
+ db.session.add(reply)
|
|
- return redirect('/topic/' + str(idt))
|
|
+ db.session.commit()
|
|
|
|
+ return redirect("/topic/" + str(idt))
|
|
|
|
+
|
|
|
|
|
|
-@app.route('/top')
|
|
+@app.route(
|
|
|
|
+ "/top"
|
|
|
|
+)
|
|
def topList():
|
|
def topList():
|
|
topics = Topic.query.order_by(Topic.repliesNum.desc())
|
|
topics = Topic.query.order_by(Topic.repliesNum.desc())
|
|
- return render_template('index.html', topics=topics)
|
|
+ return render_template("index.html", topics=topics)
|
|
|
|
|
|
-@app.route('/new')
|
|
|
|
-def redirectIndex(): return redirect('/')
|
|
|
|
|
|
|
|
-@app.route('/cat/<category>')
|
|
+@app.route("/new")
|
|
|
|
+def redirectIndex():
|
|
|
|
+ return redirect("/")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@app.route("/cat/<category>")
|
|
def catList(category):
|
|
def catList(category):
|
|
topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
|
|
topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
|
|
- return render_template('index.html', topics=topics)
|
|
+ return render_template("index.html", topics=topics)
|
|
|
|
+
|
|
|
|
|
|
-app.run(debug=True)
|
|
+app.run(debug=True)
|