ListLabelTree.py 884 B

1234567891011121314151617181920212223242526272829303132333435
  1. from flask import jsonify
  2. from flask.views import View
  3. from pycs.database.Project import Project
  4. class ListLabelTree(View):
  5. """
  6. return a list of labels for a given project
  7. """
  8. # pylint: disable=arguments-differ
  9. methods = ['GET']
  10. def dispatch_request(self, user: str, project_id):
  11. # pylint: disable=unused-argument
  12. # find project
  13. project = Project.get_or_404(project_id)
  14. labels = [lab.serialize() for lab in project.labels.all()]
  15. lookup = {}
  16. for label in labels:
  17. label["children"] = []
  18. lookup[label["id"]] = label
  19. tree = []
  20. for label in labels:
  21. parent_id = label["parent_id"]
  22. if parent_id is None:
  23. tree.append(label)
  24. else:
  25. lookup[parent_id]["children"].append(label)
  26. return jsonify(tree)