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.

50 lines
1.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. 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. # function to retrieve data about a single resource from the database
  13. def get_resource(resource_id):
  14. resource = Resource.query.filter_by(id=resource_id).first()
  15. if resource is None:
  16. abort(404)
  17. return resource
  18. # function to delete a single resource
  19. def delete_resource(resource_id):
  20. deletion = Resource.query.get(resource_id)
  21. db.session.delete(deletion)
  22. db.session.commit()
  23. flash('Successfully deleted!')
  24. # function to get filters for a specific field
  25. def get_filter_values(field):
  26. # get field values for filter
  27. field_filter = Resource.query.filter_by(type='tool').with_entities(getattr(Resource, field))
  28. # turn SQLAlchemy object into list
  29. field_filter = [i for i, in field_filter]
  30. # split each element on '/' (useful for scriptingLanguage only)
  31. field_filter = [y for x in field_filter for y in x.split(' / ')]
  32. # consolidate duplicate values
  33. field_filter = list(dict.fromkeys(field_filter))
  34. # filter None values from list
  35. field_filter = filter(None, field_filter)
  36. # sort list by alphabetical order
  37. field_filter = sorted(field_filter)
  38. return field_filter
  39. def get_book_data(isbn):
  40. try:
  41. book = meta(isbn)
  42. book.update(cover(isbn))
  43. return book
  44. except:
  45. pass