| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | from functools import wraps | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | from flask_login import current_user  # type: ignore | 
					
						
							|  |  |  | from flask_restful import Resource  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from werkzeug.exceptions import NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from controllers.console.wraps import account_initialization_required | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from libs.login import login_required | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from models import InstalledApp | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def installed_app_required(view=None): | 
					
						
							|  |  |  |     def decorator(view): | 
					
						
							|  |  |  |         @wraps(view) | 
					
						
							|  |  |  |         def decorated(*args, **kwargs): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             if not kwargs.get("installed_app_id"): | 
					
						
							|  |  |  |                 raise ValueError("missing installed_app_id in path parameters") | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             installed_app_id = kwargs.get("installed_app_id") | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  |             installed_app_id = str(installed_app_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             del kwargs["installed_app_id"] | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             installed_app = ( | 
					
						
							|  |  |  |                 db.session.query(InstalledApp) | 
					
						
							|  |  |  |                 .filter( | 
					
						
							|  |  |  |                     InstalledApp.id == str(installed_app_id), InstalledApp.tenant_id == current_user.current_tenant_id | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 .first() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if installed_app is None: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 raise NotFound("Installed app not found") | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if not installed_app.app: | 
					
						
							|  |  |  |                 db.session.delete(installed_app) | 
					
						
							|  |  |  |                 db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 raise NotFound("Installed app not found") | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return view(installed_app, *args, **kwargs) | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-25 15:54:45 +08:00
										 |  |  |         return decorated | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if view: | 
					
						
							|  |  |  |         return decorator(view) | 
					
						
							|  |  |  |     return decorator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InstalledAppResource(Resource): | 
					
						
							|  |  |  |     # must be reversed if there are multiple decorators | 
					
						
							|  |  |  |     method_decorators = [installed_app_required, account_initialization_required, login_required] |