No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

58 líneas
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 werkzeug.exceptions import abort
  13. from . import db
  14. publisher = Blueprint('publisher', __name__)
  15. # route for displaying all publishers in database
  16. @publisher.route('/publishers')
  17. def get_publishers():
  18. publishers = Resource.query.filter_by(type='publisher')
  19. return render_template('resources.html', resources=publishers, type='publisher')
  20. # route for displaying a single publisher based on the ID in the database
  21. @publisher.route('/publishers/<int:publisher_id>')
  22. def show_publisher(publisher_id):
  23. publisher = get_resource(publisher_id)
  24. links = get_linked_resources(publisher_id)
  25. return render_template('resource.html', resource=publisher, links=links)
  26. # route for editing a single publisher based on the ID in the database
  27. @publisher.route('/publishers/<int:publisher_id>/edit', methods=('GET', 'POST'))
  28. @login_required
  29. def edit_publisher(publisher_id):
  30. publisher = get_resource(publisher_id)
  31. if request.method == 'POST':
  32. name = request.form['name']
  33. description = request.form['description']
  34. if not name:
  35. flash('Name is required!')
  36. else:
  37. publisher = Resource.query.get(publisher_id)
  38. publisher.name = name
  39. publisher.description = description
  40. db.session.commit()
  41. return redirect(url_for('publisher.get_publishers',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
  42. return render_template('edit.html', resource=publisher)
  43. # route for function to delete a single publisher from the edit page
  44. @publisher.route('/publishers/<int:publisher_id>/delete', methods=('POST',))
  45. @login_required
  46. def delete_publisher(publisher_id):
  47. delete_resource(publisher_id)
  48. return redirect(url_for('publisher.get_publishers',_external=True,_scheme=os.environ.get('SSL_SCHEME')))