ListLabelTree.py 831 B

12345678910111213141516171819202122232425262728293031323334
  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, project_id):
  11. # find project
  12. project = Project.get_or_404(project_id)
  13. labels = [lab.serialize() for lab in project.labels.all()]
  14. lookup = {}
  15. for label in labels:
  16. label["children"] = []
  17. lookup[label["id"]] = label
  18. tree = []
  19. for label in labels:
  20. parent_id = label["parent_id"]
  21. if parent_id is None:
  22. tree.append(label)
  23. else:
  24. lookup[parent_id]["children"].append(label)
  25. return jsonify(tree)