CreateLabel.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from flask import request, abort, make_response
  2. from flask.views import View
  3. from pycs.database.Database import Database
  4. from pycs.frontend.notifications.NotificationManager import NotificationManager
  5. class CreateLabel(View):
  6. """
  7. create a new label
  8. """
  9. # pylint: disable=arguments-differ
  10. methods = ['POST']
  11. def __init__(self, db: Database, nm: NotificationManager):
  12. # pylint: disable=invalid-name
  13. self.db = db
  14. self.nm = nm
  15. def dispatch_request(self, identifier):
  16. # extract request data
  17. data = request.get_json(force=True)
  18. if 'name' not in data:
  19. abort(400)
  20. # find project
  21. project = self.db.project(identifier)
  22. if project is None:
  23. abort(404)
  24. # start transaction
  25. with self.db:
  26. # insert label
  27. label, _ = project.create_label(data['name'])
  28. # send notification
  29. self.nm.create_label(label)
  30. # return success response
  31. return make_response()