6
0

ListFiles.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 ListFiles(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.query.get(project_id)
  14. if project is None:
  15. return abort(404)
  16. # get count and files
  17. if collection_id is None:
  18. count = project.count_files()
  19. files = list(project.get_files(start, length))
  20. elif collection_id == 0:
  21. count = project.count_files_without_collection()
  22. files = list(project.files_without_collection(start, length))
  23. else:
  24. collection = project.collection(collection_id)
  25. if collection is None:
  26. return abort(404)
  27. count = collection.count_files()
  28. files = list(collection.files(start, length))
  29. # return files
  30. return jsonify({
  31. 'count': count,
  32. 'files': files
  33. })