| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from flask_restful import fields, marshal_with | 
					
						
							|  |  |  | from werkzeug.exceptions import Forbidden | 
					
						
							| 
									
										
										
										
											2023-12-13 20:21:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 16:51:43 +08:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.web import api | 
					
						
							|  |  |  | from controllers.web.wraps import WebApiResource | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  | from libs.helper import AppIconUrlField | 
					
						
							| 
									
										
										
										
											2024-04-18 17:33:32 +08:00
										 |  |  | from models.account import TenantStatus | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from models.model import Site | 
					
						
							| 
									
										
										
										
											2023-12-20 15:37:57 +08:00
										 |  |  | from services.feature_service import FeatureService | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppSiteApi(WebApiResource): | 
					
						
							|  |  |  |     """Resource for app sites.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     model_config_fields = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         "opening_statement": fields.String, | 
					
						
							|  |  |  |         "suggested_questions": fields.Raw(attribute="suggested_questions_list"), | 
					
						
							|  |  |  |         "suggested_questions_after_answer": fields.Raw(attribute="suggested_questions_after_answer_dict"), | 
					
						
							|  |  |  |         "more_like_this": fields.Raw(attribute="more_like_this_dict"), | 
					
						
							|  |  |  |         "model": fields.Raw(attribute="model_dict"), | 
					
						
							|  |  |  |         "user_input_form": fields.Raw(attribute="user_input_form_list"), | 
					
						
							|  |  |  |         "pre_prompt": fields.String, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     site_fields = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         "title": fields.String, | 
					
						
							|  |  |  |         "chat_color_theme": fields.String, | 
					
						
							|  |  |  |         "chat_color_theme_inverted": fields.Boolean, | 
					
						
							|  |  |  |         "icon_type": fields.String, | 
					
						
							|  |  |  |         "icon": fields.String, | 
					
						
							|  |  |  |         "icon_background": fields.String, | 
					
						
							|  |  |  |         "icon_url": AppIconUrlField, | 
					
						
							|  |  |  |         "description": fields.String, | 
					
						
							|  |  |  |         "copyright": fields.String, | 
					
						
							|  |  |  |         "privacy_policy": fields.String, | 
					
						
							|  |  |  |         "custom_disclaimer": fields.String, | 
					
						
							|  |  |  |         "default_language": fields.String, | 
					
						
							|  |  |  |         "prompt_public": fields.Boolean, | 
					
						
							|  |  |  |         "show_workflow_steps": fields.Boolean, | 
					
						
							| 
									
										
										
										
											2024-09-02 21:00:41 +09:00
										 |  |  |         "use_icon_as_answer_icon": fields.Boolean, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app_fields = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         "app_id": fields.String, | 
					
						
							|  |  |  |         "end_user_id": fields.String, | 
					
						
							|  |  |  |         "enable_site": fields.Boolean, | 
					
						
							|  |  |  |         "site": fields.Nested(site_fields), | 
					
						
							|  |  |  |         "model_config": fields.Nested(model_config_fields, allow_null=True), | 
					
						
							|  |  |  |         "plan": fields.String, | 
					
						
							|  |  |  |         "can_replace_logo": fields.Boolean, | 
					
						
							|  |  |  |         "custom_config": fields.Raw(attribute="custom_config"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @marshal_with(app_fields) | 
					
						
							|  |  |  |     def get(self, app_model, end_user): | 
					
						
							|  |  |  |         """Retrieve app site info.""" | 
					
						
							|  |  |  |         # get site | 
					
						
							|  |  |  |         site = db.session.query(Site).filter(Site.app_id == app_model.id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not site: | 
					
						
							|  |  |  |             raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 17:33:32 +08:00
										 |  |  |         if app_model.tenant.status == TenantStatus.ARCHIVE: | 
					
						
							|  |  |  |             raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 15:37:57 +08:00
										 |  |  |         can_replace_logo = FeatureService.get_features(app_model.tenant_id).can_replace_logo | 
					
						
							| 
									
										
										
										
											2023-12-13 20:21:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return AppSiteInfo(app_model.tenant, app_model, site, end_user.id, can_replace_logo) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(AppSiteApi, "/site") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppSiteInfo: | 
					
						
							|  |  |  |     """Class to store site information.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-13 20:21:39 +08:00
										 |  |  |     def __init__(self, tenant, app, site, end_user, can_replace_logo): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         """Initialize AppSiteInfo instance.""" | 
					
						
							|  |  |  |         self.app_id = app.id | 
					
						
							|  |  |  |         self.end_user_id = end_user | 
					
						
							|  |  |  |         self.enable_site = app.enable_site | 
					
						
							|  |  |  |         self.site = site | 
					
						
							|  |  |  |         self.model_config = None | 
					
						
							|  |  |  |         self.plan = tenant.plan | 
					
						
							| 
									
										
										
										
											2023-12-13 20:21:39 +08:00
										 |  |  |         self.can_replace_logo = can_replace_logo | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  |         if can_replace_logo: | 
					
						
							| 
									
										
										
										
											2024-07-12 16:51:43 +08:00
										 |  |  |             base_url = dify_config.FILES_URL | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             remove_webapp_brand = tenant.custom_config_dict.get("remove_webapp_brand", False) | 
					
						
							|  |  |  |             replace_webapp_logo = ( | 
					
						
							|  |  |  |                 f"{base_url}/files/workspaces/{tenant.id}/webapp-logo" | 
					
						
							|  |  |  |                 if tenant.custom_config_dict.get("replace_webapp_logo") | 
					
						
							|  |  |  |                 else None | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  |             self.custom_config = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 "remove_webapp_brand": remove_webapp_brand, | 
					
						
							|  |  |  |                 "replace_webapp_logo": replace_webapp_logo, | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  |             } |