ListProjectFiles.py 1.3 KB

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