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.

55 lines
1.8KB

  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 .resources import *
  12. from .relationships import *
  13. from sqlalchemy.sql import func
  14. import markdown
  15. main = Blueprint('main', __name__)
  16. # route for index page
  17. @main.route('/')
  18. def index():
  19. view = request.args.get('view')
  20. tools = Resource.query.filter_by(type='tool').order_by(func.random()).limit(6).all()
  21. if view != 'list':
  22. # append relationships to each tool
  23. append_relationships_multiple(tools)
  24. books = Resource.query.filter_by(type='book').order_by(func.random()).limit(6).all()
  25. if view != 'list':
  26. # append relationships to each book
  27. append_relationships_multiple(books)
  28. with open('content/home.md', 'r') as f:
  29. text = f.read()
  30. text = markdown.markdown(text)
  31. book_showcase = get_full_resource('69')
  32. return render_template('index.html', text=text, tools=tools, books=books, book=book_showcase, view=view)
  33. # route for profile page
  34. @main.route('/profile')
  35. @login_required
  36. def profile():
  37. return render_template('profile.html', name=current_user.name)
  38. # route for test page
  39. @main.route('/test')
  40. def test():
  41. return render_template('test.html')
  42. # route for about page
  43. @main.route('/about')
  44. def about():
  45. with open('content/about.md', 'r') as f:
  46. text = f.read()
  47. text = markdown.markdown(text)
  48. return render_template('about.html', text=text)