Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

relationships.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # @name: relationships.py
  2. # @creation_date: 2022-04-11
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: functions for relationships
  6. # @acknowledgements:
  7. from flask import Blueprint, render_template, request, flash, redirect, url_for
  8. from .models import Resource
  9. from .models import Relationship
  10. from werkzeug.exceptions import abort
  11. from . import db
  12. # function to retrieve linked resources
  13. def get_relationships(primary_id):
  14. primary_relationships = Relationship.query.filter_by(first_resource_id=primary_id).all()
  15. links = []
  16. if primary_relationships:
  17. links = []
  18. for relationship in primary_relationships:
  19. secondary_id = relationship.second_resource_id
  20. links.extend(Resource.query.filter_by(id=secondary_id).all())
  21. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  22. if secondary_relationships:
  23. for relationship in secondary_relationships:
  24. primary_id = relationship.first_resource_id
  25. links.extend(Resource.query.filter_by(id=primary_id).all())
  26. return links
  27. else:
  28. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  29. if secondary_relationships:
  30. links = []
  31. for relationship in secondary_relationships:
  32. primary_id = relationship.first_resource_id
  33. links.extend(Resource.query.filter_by(id=primary_id).all())
  34. return links
  35. # function to add a relationship to a linked resource
  36. def add_relationship(resource_id, linked_resource_id):
  37. first_resource_id = resource_id
  38. second_resource_id = linked_resource_id
  39. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  40. # add the new relationship to the database
  41. db.session.add(new_relationship)
  42. db.session.commit()
  43. # function to delete a single relationship
  44. def delete_relationship(main_id, for_deletion_id):
  45. 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()
  46. deletion = Relationship.query.get(relation.id)
  47. db.session.delete(deletion)
  48. db.session.commit()
  49. # logic for editing relationships
  50. def edit_relationships(resource_id, linked_resources, remove_linked_resources, existing_relationships):
  51. if linked_resources:
  52. for linked_resource in linked_resources:
  53. link = Resource.query.get(linked_resource)
  54. if existing_relationships and link not in existing_relationships:
  55. add_relationship(resource_id, linked_resource)
  56. elif not existing_relationships:
  57. add_relationship(resource_id, linked_resource)
  58. if remove_linked_resources:
  59. for remove_linked_resource in remove_linked_resources:
  60. delete_relationship(resource_id, remove_linked_resource)