ListProjectFiles.py 1.4 KB

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