ListProjectFiles.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. # find project
  18. project = Project.get_or_404(project_id)
  19. # get count and files
  20. if collection_id is not None:
  21. if collection_id == 0:
  22. count = project.count_files_without_collection()
  23. files = project.files_without_collection(offset=start, limit=length)
  24. else:
  25. collection = project.collection(collection_id)
  26. if collection is None:
  27. abort(404)
  28. count = collection.files.count()
  29. files = collection.get_files(offset=start, limit=length).all()
  30. else:
  31. count = project.files.count()
  32. files = project.get_files(offset=start, limit=length).all()
  33. # return files
  34. return jsonify({
  35. 'count': count,
  36. 'files': files
  37. })