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.

resources.py 3.3KB

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