Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

60 lines
2.3KB

  1. # @name: publisher.py
  2. # @creation_date: 2022-04-05
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: publisher route for publisher-related functions and pages
  6. # @acknowledgements:
  7. # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
  8. from flask import Blueprint, render_template, request, flash, redirect, url_for
  9. from flask_login import login_required, current_user
  10. from .models import Resource
  11. from .resources import *
  12. from .relationships import *
  13. from werkzeug.exceptions import abort
  14. from . import db
  15. import os
  16. publisher = Blueprint('publisher', __name__)
  17. # route for displaying all publishers in database
  18. @publisher.route('/publishers')
  19. def get_publishers():
  20. publishers = Resource.query.filter_by(type='publisher')
  21. return render_template('resources.html', resources=publishers, type='publisher')
  22. # route for displaying a single publisher based on the ID in the database
  23. @publisher.route('/publishers/<int:publisher_id>')
  24. def show_publisher(publisher_id):
  25. publisher = get_resource(publisher_id)
  26. links = get_relationships(publisher_id)
  27. return render_template('resource.html', resource=publisher, links=links)
  28. # route for editing a single publisher based on the ID in the database
  29. @publisher.route('/publishers/<int:publisher_id>/edit', methods=('GET', 'POST'))
  30. @login_required
  31. def edit_publisher(publisher_id):
  32. publisher = get_resource(publisher_id)
  33. if request.method == 'POST':
  34. name = request.form['name']
  35. description = request.form['description']
  36. if not name:
  37. flash('Name is required!')
  38. else:
  39. publisher = Resource.query.get(publisher_id)
  40. publisher.name = name
  41. publisher.description = description
  42. db.session.commit()
  43. return redirect(url_for('publisher.get_publishers',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  44. return render_template('edit.html', resource=publisher)
  45. # route for function to delete a single publisher from the edit page
  46. @publisher.route('/publishers/<int:publisher_id>/delete', methods=('POST',))
  47. @login_required
  48. def delete_publisher(publisher_id):
  49. delete_resource(publisher_id)
  50. return redirect(url_for('publisher.get_publishers',_external=True,_scheme=os.environ.get('SSL_SCHEME')))