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.

75 lines
2.7KB

  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. # function to retrieve data about a single resource from the database
  18. def get_resource(resource_id):
  19. resource = Resource.query.filter_by(id=resource_id).first()
  20. if resource is None:
  21. abort(404)
  22. return resource
  23. # function to retrieve data about a resource and its relationships
  24. def get_full_resource(resource_id):
  25. resource = get_resource(resource_id)
  26. resource = append_relationships(resource)
  27. if resource.type == 'book':
  28. book_data = get_book_data(resource.isbn)
  29. if book_data:
  30. resource.__dict__.update(book_data)
  31. return resource
  32. # function to delete a single resource
  33. def delete_resource(resource_id):
  34. deletion = Resource.query.get(resource_id)
  35. db.session.delete(deletion)
  36. db.session.commit()
  37. flash('Successfully deleted!')
  38. # function to get filters for a specific field
  39. def get_filter_values(field, type):
  40. # get field values for filter
  41. field_filter = Resource.query.filter_by(type=type).with_entities(getattr(Resource, field))
  42. # turn SQLAlchemy object into list
  43. field_filter = [i for i, in field_filter]
  44. # split each element on '/' (useful for scriptingLanguage only)
  45. field_filter = [y for x in field_filter for y in x.split(' / ')]
  46. # consolidate duplicate values
  47. field_filter = list(dict.fromkeys(field_filter))
  48. # filter None values from list
  49. field_filter = filter(None, field_filter)
  50. # sort list by alphabetical order
  51. field_filter = sorted(field_filter)
  52. return field_filter
  53. # function to get book data including metadata and covers
  54. def get_book_data(isbn):
  55. try:
  56. book = meta(isbn)
  57. description = {'desc': desc(isbn)}
  58. book.update(description)
  59. # get highest-resolution book cover possible
  60. openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
  61. request = requests.get(openl_url)
  62. if request.status_code != 200:
  63. book.update(cover(isbn))
  64. else:
  65. book_cover = {'thumbnail': openl_url}
  66. book.update(book_cover)
  67. return book
  68. except:
  69. pass