You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6KB

  1. # @name: main.py
  2. # @creation_date: 2021-10-20
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: Main route for index and other pages
  6. # @acknowledgements:
  7. # https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
  8. from flask import Blueprint, render_template
  9. from flask_login import login_required, current_user
  10. from .models import Resource
  11. from sqlalchemy.sql import func
  12. import markdown
  13. main = Blueprint('main', __name__)
  14. # route for index page
  15. @main.route('/')
  16. def index():
  17. tools = Resource.query.filter_by(type='tool').order_by(func.random()).limit(1).all()
  18. books = Resource.query.filter_by(type='book').order_by(func.random()).limit(1).all()
  19. tools2 = Resource.query.filter_by(type='tool').order_by(func.random()).limit(1).all()
  20. books2 = Resource.query.filter_by(type='book').order_by(func.random()).limit(1).all()
  21. with open('content/home.md', 'r') as f:
  22. text = f.read()
  23. text = markdown.markdown(text)
  24. return render_template('index.html', text=text, tools=tools,books=books, tools2=tools2,books2=books2)
  25. # route for profile page
  26. @main.route('/profile')
  27. @login_required
  28. def profile():
  29. return render_template('profile.html', name=current_user.name)
  30. # route for test page
  31. @main.route('/test')
  32. def test():
  33. return render_template('test.html')
  34. # route for about page
  35. @main.route('/about')
  36. def about():
  37. with open('content/about.md', 'r') as f:
  38. text = f.read()
  39. text = markdown.markdown(text)
  40. return render_template('about.html', text=text)