ListCollections.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from flask import abort, jsonify
  2. from flask.views import View
  3. from pycs.database.Database import Database
  4. class ListCollections(View):
  5. """
  6. return a list of collections for a given project
  7. """
  8. # pylint: disable=arguments-differ
  9. methods = ['GET']
  10. def __init__(self, db: Database):
  11. # pylint: disable=invalid-name
  12. self.db = db
  13. def dispatch_request(self, project_id: int):
  14. # find project
  15. project = self.db.project(project_id)
  16. if project is None:
  17. return abort(404)
  18. # get collection list
  19. collections = project.collections.all()
  20. # disable autoselect if there are no elements in the collection
  21. found = False
  22. for collection in collections:
  23. if collection.autoselect:
  24. if found:
  25. collection.autoselect = False
  26. elif collection.count_files() == 0:
  27. collection.autoselect = False
  28. found = True
  29. # return files
  30. return jsonify(collections)