RemoveLabel.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from flask import request, abort, make_response
  2. from flask.views import View
  3. from pycs import db
  4. from pycs.database.Project import Project
  5. from pycs.frontend.notifications.NotificationManager import NotificationManager
  6. class RemoveLabel(View):
  7. """
  8. remove a label from database
  9. """
  10. # pylint: disable=arguments-differ
  11. methods = ['POST']
  12. def __init__(self, nm: NotificationManager):
  13. # pylint: disable=invalid-name
  14. self.nm = nm
  15. def dispatch_request(self, project_id: int, label_id: int):
  16. # extract request data
  17. data = request.get_json(force=True)
  18. if 'remove' not in data or data['remove'] is not True:
  19. abort(400)
  20. # find project
  21. project = Project.query.get(project_id)
  22. if project is None:
  23. abort(404)
  24. # find label
  25. label = project.label(label_id)
  26. if label is None:
  27. abort(404)
  28. # find children
  29. children = label.children
  30. # start transaction
  31. with db.session.begin_nested():
  32. # remove children's parent entry
  33. for child in children:
  34. child.set_parent(None)
  35. self.nm.edit_label(child.id)
  36. # remove label
  37. label.remove()
  38. self.nm.remove_label(label.serialize())
  39. # return success response
  40. return make_response()