A search interface for the Performing Patents Otherwise publication as part of the Politics of Patents case study (part of Copim WP6): this parses data from the archive of RTF files and provides additional data from the European Patent Office OPS API. https://patents.copim.ac.uk

main.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # @name: main.py
  2. # @creation_date: 2022-09-07
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: Main route for index page, contents, frontmatter, and other miscellaneous pages
  6. # @acknowledgements:
  7. # https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
  8. # https://www.reddit.com/r/flask/comments/k1bvw4/password_protect_pages/
  9. from flask import Blueprint, render_template, session, request, redirect, url_for, flash
  10. import markdown
  11. import os
  12. from functools import wraps
  13. main = Blueprint('main', __name__)
  14. site_password = os.getenv('SITE_PASSWORD')
  15. # custom decorator to check password
  16. def check_pw(func):
  17. @wraps(func)
  18. def decorated_function(*args, **kwargs):
  19. status = session.get('status')
  20. if status != "good":
  21. return redirect(url_for('main.login'))
  22. return func(*args, **kwargs)
  23. return decorated_function
  24. # route for index page
  25. @main.route('/')
  26. @check_pw
  27. def index():
  28. return render_template('index.html')
  29. # route for site login page
  30. @main.route('/login', methods=['GET','POST'])
  31. def login():
  32. if request.method == "POST":
  33. req = request.form
  34. password = req.get("password")
  35. if password != site_password:
  36. flash('wrong password! try again...')
  37. return redirect(request.url)
  38. session["status"] = 'good'
  39. return redirect(url_for("main.index"))
  40. return render_template('login.html')
  41. # route for table of contents page
  42. @main.route('/contents/')
  43. def contents():
  44. with open('content/TOC.md', 'r') as f:
  45. text = f.read()
  46. text = markdown.markdown(text)
  47. return render_template('toc.html', text=text)
  48. # route for introduction page
  49. @main.route('/introduction/')
  50. def foreword():
  51. with open('content/introduction.md', 'r') as f:
  52. text = f.read()
  53. text = markdown.markdown(text)
  54. return render_template('text.html', text=text)
  55. # route for frontmatter page
  56. @main.route('/frontmatter/')
  57. def frontmatter():
  58. with open('content/frontmatter.md', 'r') as f:
  59. text = f.read()
  60. text = markdown.markdown(text)
  61. return render_template('text.html', text=text)