6
0

RemoveLabel.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.database.Label import Label
  6. from pycs.frontend.notifications.NotificationManager import NotificationManager
  7. class RemoveLabel(View):
  8. """
  9. remove a label from database
  10. """
  11. # pylint: disable=arguments-differ
  12. methods = ['POST']
  13. def dispatch_request(self, project_id: int, label_id: int):
  14. # extract request data
  15. data = request.get_json(force=True)
  16. if not data.get('remove', False):
  17. abort(400, "\"remove\" command was not found!")
  18. # find project
  19. project = Project.query.get(project_id)
  20. if project is None:
  21. abort(404, f"Project with id {project_id} not found")
  22. # find label
  23. label = project.label(label_id)
  24. if label is None:
  25. abort(404, f"Label with id {label_id} not found")
  26. # start transaction
  27. with db.session.begin_nested():
  28. # remove children's parent entry
  29. for child in label.children:
  30. child.set_parent(None, commit=False)
  31. NotificationManager.edited("label", child.id, Label)
  32. # remove label
  33. label.remove()
  34. NotificationManager.removed("label", label.serialize())
  35. # return success response
  36. return make_response()