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

91 lines
3.2KB

  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. book_data = get_book_data(resource.isbn)
  33. if book_data:
  34. resource.__dict__.update(book_data)
  35. return resource
  36. # function to retrieve data about a curated list of resources
  37. def get_curated_resources(resource_ids):
  38. resources = Resource.query.filter(Resource.id.in_(resource_ids)).order_by(func.random()).all()
  39. # append relationships to each resource
  40. append_relationships_multiple(resources)
  41. return resources
  42. # function to delete a single resource
  43. def delete_resource(resource_id):
  44. deletion = Resource.query.get(resource_id)
  45. db.session.delete(deletion)
  46. db.session.commit()
  47. flash('Successfully deleted!')
  48. # function to get filters for a specific field
  49. def get_filter_values(field, type):
  50. # get field values for filter
  51. field_filter = Resource.query.filter_by(type=type).with_entities(getattr(Resource, field))
  52. # turn SQLAlchemy object into list
  53. field_filter = [i for i, in field_filter]
  54. # split each element on '/' (useful for scriptingLanguage only)
  55. field_filter = [y for x in field_filter for y in x.split(' / ')]
  56. # consolidate duplicate values
  57. field_filter = list(dict.fromkeys(field_filter))
  58. # filter None values from list
  59. field_filter = filter(None, field_filter)
  60. # sort list by alphabetical order
  61. field_filter = sorted(field_filter)
  62. return field_filter
  63. # function to get book data including metadata and covers
  64. def get_book_data(isbn):
  65. try:
  66. book = meta(isbn)
  67. description = {'desc': desc(isbn)}
  68. book.update(description)
  69. #book = get_book_cover(book)
  70. return book
  71. except:
  72. pass
  73. # function to get book cover data
  74. def get_book_cover(book):
  75. # get highest-resolution book cover possible
  76. openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
  77. request = requests.get(openl_url)
  78. if request.status_code != 200:
  79. book.update(cover(isbn))
  80. else:
  81. book_cover = {'thumbnail': openl_url}
  82. book.update(book_cover)
  83. return book