RemoveLabel.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 'remove' not in data or data['remove'] is not True:
  17. abort(400)
  18. # find project
  19. project = Project.query.get(project_id)
  20. if project is None:
  21. abort(404)
  22. # find label
  23. label = project.label(label_id)
  24. if label is None:
  25. abort(404)
  26. # find children
  27. children = label.children
  28. # start transaction
  29. with db.session.begin_nested():
  30. # remove children's parent entry
  31. for child in children:
  32. child.set_parent(None)
  33. NotificationManager.edited("label", child.id, Label)
  34. # remove label
  35. label.remove()
  36. NotificationManager.removed("label", label.serialize())
  37. # return success response
  38. return make_response()