6
0

GetPreviousAndNextFile.py 996 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from flask import jsonify
  2. from flask import request
  3. from flask.views import View
  4. from pycs.database.File import File
  5. class GetPreviousAndNextFile(View):
  6. """
  7. return the previous and the next file
  8. """
  9. # pylint: disable=arguments-differ
  10. methods = ['GET']
  11. def dispatch_request(self, file_id: int):
  12. # get file from database
  13. file = File.get_or_404(file_id)
  14. with_annotations = request.args.get("only_with_annotations")
  15. kwargs = dict(with_annotations=None)
  16. if with_annotations is not None:
  17. kwargs["with_annotations"] = with_annotations == "1"
  18. # get previous and next
  19. result = {
  20. 'current': file,
  21. 'previous': file.previous(**kwargs),
  22. 'next': file.next(**kwargs),
  23. 'previousInCollection': file.previous_in_collection(**kwargs),
  24. 'nextInCollection': file.next_in_collection(**kwargs)
  25. }
  26. # return data
  27. return jsonify(result)