Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

69 Zeilen
2.9KB

  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(primary_id):
  21. primary_relationships = Relationship.query.filter_by(first_resource_id=primary_id).all()
  22. links = []
  23. if primary_relationships:
  24. links = []
  25. for relationship in primary_relationships:
  26. secondary_id = relationship.second_resource_id
  27. links.extend(Resource.query.filter_by(id=secondary_id).all())
  28. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  29. if secondary_relationships:
  30. for relationship in secondary_relationships:
  31. primary_id = relationship.first_resource_id
  32. links.extend(Resource.query.filter_by(id=primary_id).all())
  33. return links
  34. else:
  35. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  36. if secondary_relationships:
  37. links = []
  38. for relationship in secondary_relationships:
  39. primary_id = relationship.first_resource_id
  40. links.extend(Resource.query.filter_by(id=primary_id).all())
  41. return links
  42. # function to add a relationship to a linked resource
  43. def add_linked_resource(resource_id, linked_resource_id):
  44. first_resource_id = resource_id
  45. second_resource_id = linked_resource_id
  46. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  47. # add the new relationship to the database
  48. db.session.add(new_relationship)
  49. db.session.commit()
  50. # function to delete a single resource
  51. def delete_resource(resource_id):
  52. deletion = Resource.query.get(resource_id)
  53. db.session.delete(deletion)
  54. db.session.commit()
  55. flash('Successfully deleted!')
  56. # function to delete a single relationship
  57. def delete_relationship(main_id, for_deletion_id):
  58. relation = Relationship.query.filter(((Relationship.first_resource_id == main_id) & (Relationship.second_resource_id == for_deletion_id)) | ((Relationship.first_resource_id == for_deletion_id) & (Relationship.second_resource_id == main_id))).first()
  59. deletion = Relationship.query.get(relation.id)
  60. db.session.delete(deletion)
  61. db.session.commit()