Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

resources.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # @name: resources.py
  2. # @creation_date: 2022-02-23
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: functions for resources
  6. # @acknowledgements:
  7. from flask import Blueprint, render_template, request, flash, redirect, url_for
  8. from flask_login import login_required, current_user
  9. from .models import Resource
  10. from .models import Relationship
  11. from werkzeug.exceptions import abort
  12. from . import db
  13. # function to retrieve data about a single resource from the database
  14. def get_resource(resource_id):
  15. resource = Resource.query.filter_by(id=resource_id).first()
  16. if resource is None:
  17. abort(404)
  18. return resource
  19. # function to retrieve linked resources
  20. def get_linked_resources(resource_id):
  21. relationships = Relationship.query.filter_by(first_resource_id=resource_id).all()
  22. if relationships:
  23. links = []
  24. for relationship in relationships:
  25. resource_id = relationship.second_resource_id
  26. links.extend(Resource.query.filter_by(id=resource_id).all())
  27. return links
  28. else:
  29. relationships = Relationship.query.filter_by(second_resource_id=resource_id).all()
  30. if relationships:
  31. links = []
  32. for relationship in relationships:
  33. resource_id = relationship.first_resource_id
  34. links.extend(Resource.query.filter_by(id=resource_id).all())
  35. return links
  36. # function to delete a single resource
  37. def delete_resource(resource_id):
  38. deletion = Resource.query.get(resource_id)
  39. db.session.delete(deletion)
  40. db.session.commit()
  41. flash('Successfully deleted!')