| 905 | |
| 906 | |
| 907 | class ApplicationOperateSerializer(serializers.Serializer): |
| 908 | application_id = serializers.UUIDField(required=True, label=_("Application ID")) |
| 909 | user_id = serializers.UUIDField(required=True, label=_("User ID")) |
| 910 | workspace_id = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_("Workspace ID")) |
| 911 | |
| 912 | def is_valid(self, *, raise_exception=False): |
| 913 | super().is_valid(raise_exception=True) |
| 914 | workspace_id = self.data.get("workspace_id") |
| 915 | query_set = QuerySet(Application).filter(id=self.data.get("application_id")) |
| 916 | if workspace_id: |
| 917 | query_set = query_set.filter(workspace_id=workspace_id) |
| 918 | if not query_set.exists(): |
| 919 | raise AppApiException(500, _("Application id does not exist")) |
| 920 | |
| 921 | def get_mcp_servers(self, instance, with_valid=True): |
| 922 | if with_valid: |
| 923 | self.is_valid(raise_exception=True) |
| 924 | McpServersSerializer(data=instance).is_valid(raise_exception=True) |
| 925 | servers = json.loads(instance.get("mcp_servers")) |
| 926 | for server, config in servers.items(): |
| 927 | if config.get("transport") not in ["sse", "streamable_http"]: |
| 928 | raise AppApiException(500, _("Only support transport=sse or transport=streamable_http")) |
| 929 | tools = [] |
| 930 | for server in servers: |
| 931 | tools += [ |
| 932 | { |
| 933 | "server": server, |
| 934 | "name": tool.name, |
| 935 | "description": tool.description, |
| 936 | "args_schema": tool.args_schema, |
| 937 | } |
| 938 | for tool in asyncio.run(get_mcp_tools({server: servers[server]})) |
| 939 | ] |
| 940 | return tools |
| 941 | |
| 942 | def delete(self, with_valid=True): |
| 943 | from trigger.handler.simple_tools import deploy |
| 944 | from trigger.serializers.trigger import TriggerModelSerializer |
| 945 | |
| 946 | if with_valid: |
| 947 | self.is_valid() |
| 948 | application_id = self.data.get("application_id") |
| 949 | QuerySet(ApplicationVersion).filter(application_id=application_id).delete() |
| 950 | QuerySet(ResourceMapping).filter(Q(target_id=application_id) | Q(source_id=application_id)).delete() |
| 951 | QuerySet(WorkspaceUserResourcePermission).filter(target=application_id).delete() |
| 952 | QuerySet(Application).filter(id=application_id).delete() |
| 953 | trigger_ids = list( |
| 954 | QuerySet(TriggerTask) |
| 955 | .filter(source_type="APPLICATION", source_id=application_id) |
| 956 | .values("trigger_id") |
| 957 | .distinct() |
| 958 | ) |
| 959 | QuerySet(TriggerTask).filter(source_type="APPLICATION", source_id=application_id).delete() |
| 960 | for trigger_id in trigger_ids: |
| 961 | trigger = Trigger.objects.filter(id=trigger_id["trigger_id"]).first() |
| 962 | if trigger and trigger.is_active: |
| 963 | deploy(TriggerModelSerializer(trigger).data, **{}) |
| 964 | # |
no outgoing calls
no test coverage detected