app.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from flask import Flask, redirect, render_template, request, flash # import flask
  2. from login import * # import the login helper file
  3. import helper, json, time # Import the helper file and other modules
  4. app = Flask(__name__) # Create the app
  5. db = helper.createDb(app) # Get the database
  6. Topic, Reply, db = db["Topic"], db["Reply"], db["db"] # Get the classes
  7. User = initLogin(app, db) # Create and init the login manager (login helper file)
  8. db.create_all()
  9. def getTime():
  10. return time.asctime(time.localtime(time.time())) # Get the current time and date
  11. @app.route("/login") # Render the login page, nothing more
  12. def renderLogin():
  13. logout_user()
  14. return render_template("login.html")
  15. @app.route("/login/post", methods=["POST"])
  16. def login(): # Login backend
  17. try:
  18. loginUser(request.form["username"], request.form["password"], User)
  19. return redirect("/")
  20. except:
  21. try:
  22. createUser(request.form["username"], request.form["password"], db, User)
  23. return redirect("/")
  24. except:
  25. return redirect("/login")
  26. @app.route("/") # Render the homepage
  27. def renderHome():
  28. return render_template(
  29. "index.html", topics=Topic.query.order_by(Topic.lastActivity.desc())
  30. ) # List all the topics in the reversed order
  31. @app.route("/post") # Render the 'write new topic' box
  32. @login_required
  33. def renderCreateTopic():
  34. return render_template("post.html")
  35. @app.route("/post/post", methods=["POST"]) # Backend of the new topic box
  36. def createTopic():
  37. topic = Topic(
  38. request.form["title"],
  39. request.form["content"],
  40. getTime(),
  41. getUsername(),
  42. request.form["category"],
  43. )
  44. db.session.add(topic)
  45. db.session.commit()
  46. return redirect("/topic/" + str(topic.id))
  47. @app.route("/topic/<id>") # Render a topic
  48. def renderTopic(id):
  49. topic = Topic.query.filter_by(id=id).first_or_404()
  50. topic.views += 1 # Add one view
  51. db.session.add(topic)
  52. db.session.commit() # Change the value of the view in the database
  53. return render_template(
  54. "topic.html", topic=topic, replies=Reply.query.filter_by(inReplyTo=id)
  55. ) # Render the page
  56. @app.route("/reply/<id>", methods=["POST"]) # Reply to a post.
  57. @login_required
  58. def replyTo(id):
  59. topic = Topic.query.filter_by(id=id).first_or_404()
  60. topic.reply(getTime()) # Reply to the topic
  61. reply = Reply(
  62. request.form["body"], getTime(), current_user.username, id
  63. ) # Add the reply
  64. db.session.add(reply)
  65. db.session.add(topic)
  66. db.session.commit() # Add everything in the database
  67. return redirect("/topic/" + str(id)) # Redirect to the correct page
  68. @app.route("/like/<id>") # Like a topic
  69. @login_required
  70. def likeTopic(id):
  71. topic = Topic.query.filter_by(id=id).first_or_404()
  72. topic.like(current_user.username) # Call the 'like' function of the class 'Topic'
  73. db.session.add(topic)
  74. db.session.commit()
  75. return redirect("/topic/" + str(id))
  76. @app.route("/like/reply/<id>/<idt>") # Like a reply
  77. @login_required
  78. def likeReply(id, idt):
  79. reply = Reply.query.filter_by(id=id).first_or_404()
  80. reply.like(current_user.username) # Call the like function of the class Reply
  81. db.session.add(reply)
  82. db.session.commit()
  83. return redirect("/topic/" + str(idt)) # Return to the topic
  84. @app.route(
  85. "/top"
  86. ) # Order the list of posts by thoses who have the biggest number of replies
  87. def topList():
  88. topics = Topic.query.order_by(Topic.repliesNum.desc())
  89. return render_template("index.html", topics=topics)
  90. @app.route("/new") # Order the list like normal (redirect)
  91. def redirectIndex():
  92. return redirect("/")
  93. @app.route("/cat/<category>") # Get the list of posts in a category
  94. def catList(category):
  95. topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
  96. return render_template("index.html", topics=topics)
  97. app.run(debug=True) # Run the app in mode debug (change to False otherwise)