app.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. User, Topic, Reply, db = db['User'], db['Topic'], db['Reply'], db['db'] # Get the classes
  7. createLoginManager(app, User) # Create and init the login manager (login helper file)
  8. def getTime(): return time.asctime( time.localtime(time.time()) ) # Get the current time and date
  9. @app.route('/login') # Render the login page, nothing more
  10. def renderLogin(): logout_user(); return render_template('login.html')
  11. @app.route('/login/post', methods=['POST'])
  12. def login(): # Login backend
  13. try: loginUser(request.form['username'], request.form['password'], User); return redirect('/')
  14. except:
  15. try: createUser(request.form['username'], request.form['password'], db, User); return redirect('/')
  16. except: return redirect('/login')
  17. @app.route('/') # Render the homepage
  18. def renderHome():
  19. return render_template('index.html', topics=Topic.query.order_by(Topic.lastActivity.desc())) # List all the topics in the reversed order
  20. @app.route('/post') # Render the 'write new topic' box
  21. @login_required
  22. def renderCreateTopic(): return render_template('post.html')
  23. @app.route('/post/post', methods=['POST']) # Backend of the new topic box
  24. def createTopic():
  25. topic = Topic(request.form['title'], request.form['content'], getTime(), getUsername(), request.form['category'])
  26. db.session.add(topic)
  27. db.session.commit()
  28. return redirect('/topic/' + str(topic.id))
  29. @app.route('/topic/<id>') # Render a topic
  30. def renderTopic(id):
  31. topic = Topic.query.filter_by(id=id).first_or_404(); topic.views += 1 # Add one view
  32. db.session.add(topic); db.session.commit() # Change the value of the view in the database
  33. return render_template('topic.html', topic=topic, replies=Reply.query.filter_by(inReplyTo=id)) # Render the page
  34. @app.route('/reply/<id>', methods=['POST']) # Reply to a post.
  35. @login_required
  36. def replyTo(id):
  37. topic = Topic.query.filter_by(id=id).first_or_404(); topic.reply(getTime()) # Reply to the topic
  38. reply = Reply(request.form['body'], getTime(), current_user.username, id) # Add the reply
  39. db.session.add(reply); db.session.add(topic); db.session.commit() # Add everything in the database
  40. return redirect('/topic/' + str(id)) # Redirect to the correct page
  41. @app.route('/like/<id>') # Like a topic
  42. @login_required
  43. def likeTopic(id):
  44. topic = Topic.query.filter_by(id=id).first_or_404()
  45. topic.like(current_user.username) # Call the 'like' function of the class 'Topic'
  46. db.session.add(topic); db.session.commit()
  47. return redirect('/topic/' + str(id))
  48. @app.route('/like/reply/<id>/<idt>') # Like a reply
  49. @login_required
  50. def likeReply(id, idt):
  51. reply = Reply.query.filter_by(id=id).first_or_404()
  52. reply.like(current_user.username) # Call the like function of the class Reply
  53. db.session.add(reply); db.session.commit()
  54. return redirect('/topic/' + str(idt)) # Return to the topic
  55. @app.route('/top') # Order the list of posts by thoses who have the biggest number of replies
  56. def topList():
  57. topics = Topic.query.order_by(Topic.repliesNum.desc())
  58. return render_template('index.html', topics=topics)
  59. @app.route('/new') # Order the list like normal (redirect)
  60. def redirectIndex(): return redirect('/')
  61. @app.route('/cat/<category>') # Get the list of posts in a category
  62. def catList(category):
  63. topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
  64. return render_template('index.html', topics=topics)
  65. app.run(debug=True) # Run the app in mode debug (change to False otherwise)