6
0

ListProjectFiles.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from flask import abort
  2. from flask import jsonify
  3. from flask import request
  4. from flask.views import View
  5. from pycs.database.Project import Project
  6. class ListProjectFiles(View):
  7. """
  8. return a list of files for a given project
  9. """
  10. # pylint: disable=arguments-differ
  11. methods = ['GET']
  12. def dispatch_request(self,
  13. user: str,
  14. project_id: int,
  15. start: int = 0,
  16. length: int = -1,
  17. collection_id: int = None):
  18. # pylint: disable=unused-argument
  19. # find project
  20. project = Project.get_or_404(project_id)
  21. # get count and files
  22. if collection_id is not None:
  23. if collection_id == 0:
  24. count = project.count_files_without_collection()
  25. files = project.files_without_collection(offset=start, limit=length)
  26. else:
  27. collection = project.collection(collection_id)
  28. if collection is None:
  29. abort(404)
  30. count = collection.files.count()
  31. files = collection.get_files(offset=start, limit=length).all()
  32. else:
  33. with_annotations = request.args.get("only_with_annotations")
  34. kwargs = dict(with_annotations=None)
  35. if with_annotations is not None:
  36. kwargs["with_annotations"] = with_annotations == "1"
  37. # first get all files without specific limit
  38. files = project.get_files(**kwargs)
  39. # get the count of those
  40. count = files.count()
  41. # finally, limit to the desired offset and number of files
  42. files = files.offset(start).limit(length).all()
  43. # return files
  44. return jsonify({
  45. 'count': count,
  46. 'files': files
  47. })