| 
									
										
										
										
											2024-11-24 13:28:46 +08:00
										 |  |  | from datetime import UTC, datetime | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-06 11:58:49 +08:00
										 |  |  | from flask_login import current_user | 
					
						
							|  |  |  | from flask_restful import Resource, marshal_with, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from werkzeug.exceptions import Forbidden, NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from constants.languages import supported_language | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console import api | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from controllers.console.app.wraps import get_app_model | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  | from controllers.console.wraps import account_initialization_required, setup_required | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  | from fields.app_fields import app_site_fields | 
					
						
							| 
									
										
										
										
											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 Site | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def parse_app_site_args(): | 
					
						
							|  |  |  |     parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |     parser.add_argument("title", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("icon_type", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("icon", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("icon_background", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("description", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("default_language", type=supported_language, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("chat_color_theme", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("chat_color_theme_inverted", type=bool, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("customize_domain", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("copyright", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("privacy_policy", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("custom_disclaimer", type=str, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument( | 
					
						
							|  |  |  |         "customize_token_strategy", type=str, choices=["must", "allow", "not_allow"], required=False, location="json" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     parser.add_argument("prompt_public", type=bool, required=False, location="json") | 
					
						
							|  |  |  |     parser.add_argument("show_workflow_steps", type=bool, required=False, location="json") | 
					
						
							| 
									
										
										
										
											2024-09-02 21:00:41 +09:00
										 |  |  |     parser.add_argument("use_icon_as_answer_icon", type=bool, required=False, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     return parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppSite(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @marshal_with(app_site_fields) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     def post(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parse_app_site_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 07:34:25 -05:00
										 |  |  |         # The role of the current user in the ta table must be editor, admin, or owner | 
					
						
							|  |  |  |         if not current_user.is_editor: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 10:32:20 +08:00
										 |  |  |         site = db.session.query(Site).filter(Site.app_id == app_model.id).first() | 
					
						
							|  |  |  |         if not site: | 
					
						
							|  |  |  |             raise NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for attr_name in [ | 
					
						
							|  |  |  |             "title", | 
					
						
							|  |  |  |             "icon_type", | 
					
						
							|  |  |  |             "icon", | 
					
						
							|  |  |  |             "icon_background", | 
					
						
							|  |  |  |             "description", | 
					
						
							|  |  |  |             "default_language", | 
					
						
							|  |  |  |             "chat_color_theme", | 
					
						
							|  |  |  |             "chat_color_theme_inverted", | 
					
						
							|  |  |  |             "customize_domain", | 
					
						
							|  |  |  |             "copyright", | 
					
						
							|  |  |  |             "privacy_policy", | 
					
						
							|  |  |  |             "custom_disclaimer", | 
					
						
							|  |  |  |             "customize_token_strategy", | 
					
						
							|  |  |  |             "prompt_public", | 
					
						
							|  |  |  |             "show_workflow_steps", | 
					
						
							|  |  |  |             "use_icon_as_answer_icon", | 
					
						
							|  |  |  |         ]: | 
					
						
							|  |  |  |             value = args.get(attr_name) | 
					
						
							|  |  |  |             if value is not None: | 
					
						
							|  |  |  |                 setattr(site, attr_name, value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         site.updated_by = current_user.id | 
					
						
							|  |  |  |         site.updated_at = datetime.now(UTC).replace(tzinfo=None) | 
					
						
							|  |  |  |         db.session.commit() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return site | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppSiteAccessTokenReset(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @marshal_with(app_site_fields) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     def post(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         # The role of the current user in the ta table must be admin or owner | 
					
						
							| 
									
										
										
										
											2024-01-26 12:47:42 +08:00
										 |  |  |         if not current_user.is_admin_or_owner: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         site = db.session.query(Site).filter(Site.app_id == app_model.id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not site: | 
					
						
							|  |  |  |             raise NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         site.code = Site.generate_code(16) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |         site.updated_by = current_user.id | 
					
						
							| 
									
										
										
										
											2024-11-24 13:28:46 +08:00
										 |  |  |         site.updated_at = datetime.now(UTC).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return site | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(AppSite, "/apps/<uuid:app_id>/site") | 
					
						
							|  |  |  | api.add_resource(AppSiteAccessTokenReset, "/apps/<uuid:app_id>/site/access-token-reset") |