ListProjectFiles.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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, project_id: int, start: int, length: int, collection_id: int = None):
  12. # find project
  13. project = Project.get_or_404(project_id)
  14. # get count and files
  15. if collection_id is not None:
  16. if collection_id == 0:
  17. count = project.count_files_without_collection()
  18. files = project.files_without_collection(start, length)
  19. else:
  20. collection = project.collection(collection_id)
  21. if collection is None:
  22. abort(404)
  23. count = collection.files.count()
  24. files = collection.get_files(start, length).all()
  25. else:
  26. count = project.files.count()
  27. files = project.get_files(start, length).all()
  28. # return files
  29. return jsonify({
  30. 'count': count,
  31. 'files': files
  32. })