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

41 lines
1.5KB

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