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.

99 lines
3.5KB

  1. # @name: resources.py
  2. # @creation_date: 2022-02-23
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: functions for resources
  6. # @acknowledgements:
  7. # isbntools: https://isbntools.readthedocs.io/en/latest/info.html
  8. # regex for URLs: https://gist.github.com/gruber/249502
  9. from flask import Blueprint, render_template, request, flash, redirect, url_for
  10. from .models import Resource
  11. from werkzeug.exceptions import abort
  12. from . import db
  13. from .relationships import *
  14. from isbntools.app import *
  15. import requests
  16. import re
  17. from sqlalchemy.sql import func
  18. import markdown
  19. # function to retrieve data about a single resource from the database
  20. def get_resource(resource_id):
  21. resource = Resource.query.filter_by(id=resource_id).first()
  22. if resource is None:
  23. abort(404)
  24. return resource
  25. # function to retrieve data about a resource and its relationships
  26. def get_full_resource(resource_id):
  27. resource = get_resource(resource_id)
  28. resource = append_relationships(resource)
  29. if resource.type == 'book':
  30. # render Markdown as HTML
  31. resource.description = markdown.markdown(resource.description)
  32. # get additional book metadata
  33. book_data = get_book_data(resource.isbn)
  34. if book_data:
  35. resource.__dict__.update(book_data)
  36. return resource
  37. # function to get practice from Markdown file
  38. def get_practice_markdown(practice_name):
  39. with open(f'content/practices/{practice_name}.md', 'r') as f:
  40. practice_text = f.read()
  41. practice_text = markdown.markdown(practice_text)
  42. return practice_text
  43. # function to retrieve data about a curated list of resources
  44. def get_curated_resources(resource_ids):
  45. resources = Resource.query.filter(Resource.id.in_(resource_ids)).order_by(func.random()).all()
  46. # append relationships to each resource
  47. append_relationships_multiple(resources)
  48. return resources
  49. # function to delete a single resource
  50. def delete_resource(resource_id):
  51. deletion = Resource.query.get(resource_id)
  52. db.session.delete(deletion)
  53. db.session.commit()
  54. flash('Successfully deleted!')
  55. # function to get filters for a specific field
  56. def get_filter_values(field, type):
  57. # get field values for filter
  58. field_filter = Resource.query.filter_by(type=type).with_entities(getattr(Resource, field))
  59. # turn SQLAlchemy object into list
  60. field_filter = [i for i, in field_filter]
  61. # split each element on '/' (useful for scriptingLanguage only)
  62. field_filter = [y for x in field_filter for y in x.split(' / ')]
  63. # consolidate duplicate values
  64. field_filter = list(dict.fromkeys(field_filter))
  65. # filter None values from list
  66. field_filter = filter(None, field_filter)
  67. # sort list by alphabetical order
  68. field_filter = sorted(field_filter)
  69. return field_filter
  70. # function to get book data including metadata and covers
  71. def get_book_data(isbn):
  72. try:
  73. book = meta(isbn)
  74. description = {'desc': desc(isbn)}
  75. book.update(description)
  76. #book = get_book_cover(book)
  77. return book
  78. except:
  79. pass
  80. # function to get book cover data
  81. def get_book_cover(book):
  82. # get highest-resolution book cover possible
  83. openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
  84. request = requests.get(openl_url)
  85. if request.status_code != 200:
  86. book.update(cover(isbn))
  87. else:
  88. book_cover = {'thumbnail': openl_url}
  89. book.update(book_cover)
  90. return book