app.py 3.7 KB

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