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.

65 líneas
2.9KB

  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 .models import Resource
  8. from .models import Relationship
  9. from . import db
  10. # function to retrieve linked resources
  11. def get_relationships(primary_id):
  12. primary_relationships = Relationship.query.filter_by(first_resource_id=primary_id).all()
  13. links = []
  14. if primary_relationships:
  15. links = []
  16. for relationship in primary_relationships:
  17. secondary_id = relationship.second_resource_id
  18. links.extend(Resource.query.filter_by(id=secondary_id).all())
  19. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  20. if secondary_relationships:
  21. for relationship in secondary_relationships:
  22. primary_id = relationship.first_resource_id
  23. links.extend(Resource.query.filter_by(id=primary_id).all())
  24. return links
  25. else:
  26. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  27. if secondary_relationships:
  28. links = []
  29. for relationship in secondary_relationships:
  30. primary_id = relationship.first_resource_id
  31. links.extend(Resource.query.filter_by(id=primary_id).all())
  32. return links
  33. # function to add a relationship to a linked resource
  34. def add_relationship(resource_id, linked_resource_id):
  35. first_resource_id = resource_id
  36. second_resource_id = linked_resource_id
  37. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  38. # add the new relationship to the database
  39. db.session.add(new_relationship)
  40. db.session.commit()
  41. # function to delete a single relationship
  42. def delete_relationship(main_id, for_deletion_id):
  43. 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()
  44. deletion = Relationship.query.get(relation.id)
  45. db.session.delete(deletion)
  46. db.session.commit()
  47. # logic for editing relationships
  48. def edit_relationships(resource_id, linked_resources, remove_linked_resources, existing_relationships):
  49. if linked_resources:
  50. for linked_resource in linked_resources:
  51. link = Resource.query.get(linked_resource)
  52. if existing_relationships and link not in existing_relationships:
  53. add_relationship(resource_id, linked_resource)
  54. elif not existing_relationships:
  55. add_relationship(resource_id, linked_resource)
  56. if remove_linked_resources:
  57. for remove_linked_resource in remove_linked_resources:
  58. delete_relationship(resource_id, remove_linked_resource)