Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

relationships.py 5.3KB

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