RemoveFile.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from os import remove
  2. from flask import make_response, request, abort
  3. from flask.views import View
  4. from pycs.database.Database import Database
  5. from pycs.frontend.notifications.NotificationManager import NotificationManager
  6. class RemoveFile(View):
  7. """
  8. remove a given file
  9. """
  10. # pylint: disable=arguments-differ
  11. methods = ['POST']
  12. def __init__(self, db: Database, nm: NotificationManager):
  13. # pylint: disable=invalid-name
  14. self.db = db
  15. self.nm = nm
  16. def dispatch_request(self, identifier):
  17. # extract request data
  18. data = request.get_json(force=True)
  19. if 'remove' not in data or data['remove'] is not True:
  20. return abort(400)
  21. # start transaction
  22. with self.db:
  23. # find file
  24. file = self.db.file(identifier)
  25. if file is None:
  26. return abort(400)
  27. # check if project uses an external data directory
  28. project = file.project()
  29. if project.external_data:
  30. return abort(400)
  31. # remove file from database
  32. file.remove()
  33. # remove file from folder
  34. remove(file.path)
  35. # TODO remove temp files
  36. # send notification
  37. self.nm.remove_file(file)
  38. return make_response()