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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import markdown
  11. # function to retrieve linked resources
  12. def get_relationships(primary_id):
  13. primary_relationships = Relationship.query.filter_by(first_resource_id=primary_id).all()
  14. links = []
  15. if primary_relationships:
  16. links = []
  17. for relationship in primary_relationships:
  18. secondary_id = relationship.second_resource_id
  19. links.extend(Resource.query.filter_by(id=secondary_id).all())
  20. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  21. if secondary_relationships:
  22. for relationship in secondary_relationships:
  23. primary_id = relationship.first_resource_id
  24. links.extend(Resource.query.filter_by(id=primary_id).all())
  25. return links
  26. else:
  27. secondary_relationships = Relationship.query.filter_by(second_resource_id=primary_id).all()
  28. if secondary_relationships:
  29. links = []
  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. # function to append relationships to a resource object
  35. def append_relationships(resource):
  36. relationships = get_relationships(resource.id)
  37. if relationships:
  38. for relationship in relationships:
  39. if relationship.type == 'tool':
  40. if 'tools' not in resource.__dict__.keys():
  41. resource.__dict__['tools'] = []
  42. resource.__dict__['tools'].append(relationship)
  43. else:
  44. resource.__dict__['tools'].append(relationship)
  45. elif relationship.type == 'practice':
  46. if 'practices' not in resource.__dict__.keys():
  47. resource.__dict__['practices'] = []
  48. resource.__dict__['practices'].append(relationship)
  49. else:
  50. resource.__dict__['practices'].append(relationship)
  51. elif relationship.type == 'book':
  52. # render Markdown as HTML
  53. relationship.description = markdown.markdown(relationship.description)
  54. if 'books' not in resource.__dict__.keys():
  55. resource.__dict__['books'] = []
  56. resource.__dict__['books'].append(relationship)
  57. else:
  58. resource.__dict__['books'].append(relationship)
  59. return resource
  60. else:
  61. return resource
  62. # function to append relationships to a dictionary of resources
  63. def append_relationships_multiple(resources):
  64. for index, resource in enumerate(resources):
  65. resources[index] = append_relationships(resource)
  66. return resources
  67. # function to append relationships to a pagination object of resources
  68. def append_relationships_multiple_paginated(pagination):
  69. for item in (pagination.items):
  70. item = append_relationships(item)
  71. return pagination
  72. # function to add a relationship to a linked resource
  73. def add_relationship(resource_id, linked_resource_id):
  74. first_resource_id = resource_id
  75. second_resource_id = linked_resource_id
  76. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  77. # add the new relationship to the database
  78. db.session.add(new_relationship)
  79. db.session.commit()
  80. # function to delete a single relationship
  81. def delete_relationship(main_id, for_deletion_id):
  82. 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()
  83. deletion = Relationship.query.get(relation.id)
  84. db.session.delete(deletion)
  85. db.session.commit()
  86. # logic for editing relationships
  87. def edit_relationships(resource_id, linked_resources, remove_linked_resources, existing_relationships):
  88. if linked_resources:
  89. for linked_resource in linked_resources:
  90. link = Resource.query.get(linked_resource)
  91. if existing_relationships and link not in existing_relationships:
  92. add_relationship(resource_id, linked_resource)
  93. elif not existing_relationships:
  94. add_relationship(resource_id, linked_resource)
  95. if remove_linked_resources:
  96. for remove_linked_resource in remove_linked_resources:
  97. delete_relationship(resource_id, remove_linked_resource)