Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

resources.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. from flask import Blueprint, render_template, request, flash, redirect, url_for
  8. from .models import Resource
  9. from werkzeug.exceptions import abort
  10. from . import db
  11. from isbntools.app import *
  12. import requests
  13. # function to retrieve data about a single resource from the database
  14. def get_resource(resource_id):
  15. resource = Resource.query.filter_by(id=resource_id).first()
  16. if resource is None:
  17. abort(404)
  18. return resource
  19. # function to delete a single resource
  20. def delete_resource(resource_id):
  21. deletion = Resource.query.get(resource_id)
  22. db.session.delete(deletion)
  23. db.session.commit()
  24. flash('Successfully deleted!')
  25. # function to get filters for a specific field
  26. def get_filter_values(field, type):
  27. # get field values for filter
  28. field_filter = Resource.query.filter_by(type=type).with_entities(getattr(Resource, field))
  29. # turn SQLAlchemy object into list
  30. field_filter = [i for i, in field_filter]
  31. # split each element on '/' (useful for scriptingLanguage only)
  32. field_filter = [y for x in field_filter for y in x.split(' / ')]
  33. # consolidate duplicate values
  34. field_filter = list(dict.fromkeys(field_filter))
  35. # filter None values from list
  36. field_filter = filter(None, field_filter)
  37. # sort list by alphabetical order
  38. field_filter = sorted(field_filter)
  39. return field_filter
  40. def get_book_data(isbn):
  41. try:
  42. book = meta(isbn)
  43. description = {'desc': desc(isbn)}
  44. book.update(description)
  45. # get highest-resolution book cover possible
  46. openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
  47. request = requests.get(openl_url)
  48. if request.status_code != 200:
  49. book.update(cover(isbn))
  50. else:
  51. book_cover = {'thumbnail': openl_url}
  52. book.update(book_cover)
  53. return book
  54. except:
  55. pass