Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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