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ů.

před 1 rokem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. # render Markdown as HTML
  47. relationship.description = markdown.markdown(relationship.description)
  48. if 'practices' not in resource.__dict__.keys():
  49. resource.__dict__['practices'] = []
  50. resource.__dict__['practices'].append(relationship)
  51. else:
  52. resource.__dict__['practices'].append(relationship)
  53. elif relationship.type == 'book':
  54. # render Markdown as HTML
  55. relationship.description = markdown.markdown(relationship.description)
  56. if 'books' not in resource.__dict__.keys():
  57. resource.__dict__['books'] = []
  58. resource.__dict__['books'].append(relationship)
  59. else:
  60. resource.__dict__['books'].append(relationship)
  61. return resource
  62. else:
  63. return resource
  64. # function to append relationships to a dictionary of resources
  65. def append_relationships_multiple(resources):
  66. for index, resource in enumerate(resources):
  67. resources[index] = append_relationships(resource)
  68. return resources
  69. # function to append relationships to a pagination object of resources
  70. def append_relationships_multiple_paginated(pagination):
  71. for item in (pagination.items):
  72. item = append_relationships(item)
  73. return pagination
  74. # function to add a relationship to a linked resource
  75. def add_relationship(resource_id, linked_resource_id):
  76. first_resource_id = resource_id
  77. second_resource_id = linked_resource_id
  78. new_relationship = Relationship(first_resource_id=first_resource_id, second_resource_id=second_resource_id)
  79. # add the new relationship to the database
  80. db.session.add(new_relationship)
  81. db.session.commit()
  82. # function to delete a single relationship
  83. def delete_relationship(main_id, for_deletion_id):
  84. 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()
  85. deletion = Relationship.query.get(relation.id)
  86. db.session.delete(deletion)
  87. db.session.commit()
  88. # logic for editing relationships
  89. def edit_relationships(resource_id, linked_resources, remove_linked_resources, existing_relationships):
  90. if linked_resources:
  91. for linked_resource in linked_resources:
  92. link = Resource.query.get(linked_resource)
  93. if existing_relationships and link not in existing_relationships:
  94. add_relationship(resource_id, linked_resource)
  95. elif not existing_relationships:
  96. add_relationship(resource_id, linked_resource)
  97. if remove_linked_resources:
  98. for remove_linked_resource in remove_linked_resources:
  99. delete_relationship(resource_id, remove_linked_resource)