routes.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from flask import Flask, url_for, request, render_template;
  2. from app import appVar;
  3. import redis;
  4. # Plug in to NoSQL Redis DB Server
  5. r = redis.StrictRedis(host='localhost', port=6379, db=0, charset='utf-8', decode_responses=True)
  6. #main_page
  7. @appVar.route('/')
  8. def hello():
  9. myLink = url_for('fCreate')
  10. qList = r.keys('*:q')
  11. return render_template('TriviaIndex.html', questions=qList, Link=myLink)
  12. #sub_page
  13. @appVar.route('/createPage', methods=['GET', 'POST'])
  14. def fCreate():
  15. if request.method == 'GET':
  16. # return empty form
  17. return render_template('CreateQuestion.html')
  18. elif request.method == 'POST':
  19. # reqad form data
  20. titleV = request.form['title']
  21. answerV = request.form['answer']
  22. questionV = request.form['question']
  23. # save data to db
  24. r.set(titleV+':q', questionV)
  25. r.set(titleV+':a', answerV)
  26. return render_template('SubmitedPage.html', question = questionV)
  27. else:
  28. return "<h2>Invalid request</h2>"
  29. #var_page
  30. @appVar.route('/q/<tVar>', methods=['GET', 'POST'])
  31. def fQest(tVar):
  32. if request.method == 'GET':
  33. # Prepeare and read from
  34. #get question from db
  35. dbQuestion = r.get(tVar+':q')
  36. return render_template('AnswerQuestion.html', question = dbQuestion)
  37. elif request.method == 'POST':
  38. # checking answer
  39. userAnswer = request.form['answer']
  40. dbAnswer = r.get(tVar+':a')
  41. if userAnswer == dbAnswer:
  42. return render_template('QCorrectPage.html')
  43. else:
  44. return render_template('QWrongPage.html', rAnswer=dbAnswer, uAnswer=userAnswer)
  45. return "<h2>" + tVar + "</h2>"