wraps.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from functools import wraps
  2. from flask_login import current_user
  3. from flask_restful import Resource
  4. from werkzeug.exceptions import NotFound
  5. from controllers.console.wraps import account_initialization_required
  6. from extensions.ext_database import db
  7. from libs.login import login_required
  8. from models import InstalledApp
  9. def installed_app_required(view=None):
  10. def decorator(view):
  11. @wraps(view)
  12. def decorated(*args, **kwargs):
  13. if not kwargs.get("installed_app_id"):
  14. raise ValueError("missing installed_app_id in path parameters")
  15. installed_app_id = kwargs.get("installed_app_id")
  16. installed_app_id = str(installed_app_id)
  17. del kwargs["installed_app_id"]
  18. installed_app = (
  19. db.session.query(InstalledApp)
  20. .filter(
  21. InstalledApp.id == str(installed_app_id), InstalledApp.tenant_id == current_user.current_tenant_id
  22. )
  23. .first()
  24. )
  25. if installed_app is None:
  26. raise NotFound("Installed app not found")
  27. if not installed_app.app:
  28. db.session.delete(installed_app)
  29. db.session.commit()
  30. raise NotFound("Installed app not found")
  31. return view(installed_app, *args, **kwargs)
  32. return decorated
  33. if view:
  34. return decorator(view)
  35. return decorator
  36. class InstalledAppResource(Resource):
  37. # must be reversed if there are multiple decorators
  38. method_decorators = [installed_app_required, account_initialization_required, login_required]