You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 line
4.4KB

  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 append relationships to a resource object
  34. def append_relationships(resource):
  35. relationships = get_relationships(resource.id)
  36. if relationships:
  37. for relationship in relationships:
  38. if relationship.type == 'tool':
  39. if 'tools' not in resource.__dict__.keys():
  40. resource.__dict__['tools'] = relationship
  41. elif type(resource.__dict__['tools']) == list:
  42. resource.__dict__['tools'].append(relationship)
  43. else:
  44. resource.__dict__['tools'] = [resource.__dict__['tools'], relationship]
  45. elif relationship.type == 'practice':
  46. if 'practices' not in resource.__dict__.keys():
  47. resource.__dict__['practices'] = relationship
  48. elif type(resource.__dict__['practices']) == list:
  49. resource.__dict__['practices'].append(relationship)
  50. else:
  51. resource.__dict__['practices'] = [resource.__dict__['practices'], relationship]
  52. elif relationship.type == 'book':
  53. if 'books' not in resource.__dict__.keys():
  54. resource.__dict__['books'] = relationship
  55. elif type(resource.__dict__['books']) == list:
  56. resource.__dict__['books'].append(relationship)
  57. else:
  58. resource.__dict__['books'] = [resource.__dict__['books'], relationship]
  59. return resource
  60. else:
  61. return resource
  62. # function to add a relationship to a linked resource
  63. def add_relationship(resource_id, linked_resource_id):
  64. first_resource_id = resource_id
  65. second_resource_id = linked_resource_id
  66. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  67. # add the new relationship to the database
  68. db.session.add(new_relationship)
  69. db.session.commit()
  70. # function to delete a single relationship
  71. def delete_relationship(main_id, for_deletion_id):
  72. 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()
  73. deletion = Relationship.query.get(relation.id)
  74. db.session.delete(deletion)
  75. db.session.commit()
  76. # logic for editing relationships
  77. def edit_relationships(resource_id, linked_resources, remove_linked_resources, existing_relationships):
  78. if linked_resources:
  79. for linked_resource in linked_resources:
  80. link = Resource.query.get(linked_resource)
  81. if existing_relationships and link not in existing_relationships:
  82. add_relationship(resource_id, linked_resource)
  83. elif not existing_relationships:
  84. add_relationship(resource_id, linked_resource)
  85. if remove_linked_resources:
  86. for remove_linked_resource in remove_linked_resources:
  87. delete_relationship(resource_id, remove_linked_resource)