Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
wraps.py54 linesDownload Raw Back to explore
1from functools import wraps2 3from flask_login import current_user4from flask_restful import Resource5from werkzeug.exceptions import NotFound6 7from controllers.console.wraps import account_initialization_required8from extensions.ext_database import db9from libs.login import login_required10from models import InstalledApp11 12 13def installed_app_required(view=None):14    def decorator(view):15        @wraps(view)16        def decorated(*args, **kwargs):17            if not kwargs.get("installed_app_id"):18                raise ValueError("missing installed_app_id in path parameters")19 20            installed_app_id = kwargs.get("installed_app_id")21            installed_app_id = str(installed_app_id)22 23            del kwargs["installed_app_id"]24 25            installed_app = (26                db.session.query(InstalledApp)27                .filter(28                    InstalledApp.id == str(installed_app_id), InstalledApp.tenant_id == current_user.current_tenant_id29                )30                .first()31            )32 33            if installed_app is None:34                raise NotFound("Installed app not found")35 36            if not installed_app.app:37                db.session.delete(installed_app)38                db.session.commit()39 40                raise NotFound("Installed app not found")41 42            return view(installed_app, *args, **kwargs)43 44        return decorated45 46    if view:47        return decorator(view)48    return decorator49 50 51class InstalledAppResource(Resource):52    # must be reversed if there are multiple decorators53    method_decorators = [installed_app_required, account_initialization_required, login_required]54