|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import uuid_utils.compat as uuid |
| 4 | +from django.db import connection |
| 5 | +from django.db.models import QuerySet |
| 6 | +from django.utils.translation import gettext_lazy as _ |
| 7 | +from rest_framework import serializers |
| 8 | + |
| 9 | +from common.constants.permission_constants import Group |
| 10 | +from modules.api.module import ModuleCreateRequest |
| 11 | +from tools.models import ToolModule |
| 12 | + |
| 13 | + |
| 14 | +def get_module_type(source): |
| 15 | + if source == Group.TOOL.name: |
| 16 | + return ToolModule |
| 17 | + elif source == Group.APPLICATION.name: |
| 18 | + # todo app module |
| 19 | + return None |
| 20 | + elif source == Group.KNOWLEDGE.name: |
| 21 | + # todo knowledge module |
| 22 | + return None |
| 23 | + else: |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +class ModuleSerializer(serializers.Serializer): |
| 28 | + id = serializers.CharField(required=True, label=_('module id')) |
| 29 | + name = serializers.CharField(required=True, label=_('module name')) |
| 30 | + user_id = serializers.CharField(required=True, label=_('module user id')) |
| 31 | + workspace_id = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_('workspace id')) |
| 32 | + parent_id = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_('parent id')) |
| 33 | + |
| 34 | + class Create(serializers.Serializer): |
| 35 | + user_id = serializers.UUIDField(required=True, label=_('user id')) |
| 36 | + source = serializers.CharField(required=True, label=_('source')) |
| 37 | + |
| 38 | + def insert(self, instance, with_valid=True): |
| 39 | + if with_valid: |
| 40 | + self.is_valid(raise_exception=True) |
| 41 | + ModuleCreateRequest(data=instance).is_valid(raise_exception=True) |
| 42 | + |
| 43 | + workspace_id = self.data.get('workspace_id', 'default') |
| 44 | + parent_id = instance.get('parent_id', 'root') |
| 45 | + name = instance.get('name') |
| 46 | + |
| 47 | + Module = get_module_type(self.data.get('source')) |
| 48 | + if QuerySet(Module).filter(name=name, workspace_id=workspace_id, parent_id=parent_id).exists(): |
| 49 | + raise serializers.ValidationError(_('Module name already exists')) |
| 50 | + |
| 51 | + module = Module( |
| 52 | + id=uuid.uuid7(), |
| 53 | + name=instance.get('name'), |
| 54 | + user_id=self.data.get('user_id'), |
| 55 | + workspace_id=workspace_id, |
| 56 | + parent_id=parent_id |
| 57 | + ) |
| 58 | + module.save() |
| 59 | + return ModuleSerializer(module).data |
| 60 | + |
| 61 | + class Operate(serializers.Serializer): |
| 62 | + id = serializers.CharField(required=True, label=_('module id')) |
| 63 | + workspace_id = serializers.CharField(required=True, allow_null=True, allow_blank=True, label=_('workspace id')) |
| 64 | + source = serializers.CharField(required=True, label=_('source')) |
| 65 | + |
| 66 | + def edit(self, instance): |
| 67 | + self.is_valid(raise_exception=True) |
| 68 | + Module = get_module_type(self.data.get('source')) |
| 69 | + if not QuerySet(Module).filter(id=self.data.get('id')).exists(): |
| 70 | + raise serializers.ValidationError(_('Module does not exist')) |
| 71 | + |
| 72 | + edit_field_list = ['name'] |
| 73 | + edit_dict = {field: instance.get(field) for field in edit_field_list if ( |
| 74 | + field in instance and instance.get(field) is not None)} |
| 75 | + |
| 76 | + QuerySet(Module).filter(id=self.data.get('id')).update(**edit_dict) |
| 77 | + |
| 78 | + return self.one() |
| 79 | + |
| 80 | + def one(self): |
| 81 | + self.is_valid(raise_exception=True) |
| 82 | + Module = get_module_type(self.data.get('source')) |
| 83 | + module = QuerySet(Module).filter(id=self.data.get('id')).first() |
| 84 | + return ModuleSerializer(module).data |
| 85 | + |
| 86 | + |
| 87 | +class ModuleTreeSerializer(serializers.Serializer): |
| 88 | + workspace_id = serializers.CharField(required=True, allow_null=True, allow_blank=True, label=_('workspace id')) |
| 89 | + source = serializers.CharField(required=True, label=_('source')) |
| 90 | + |
| 91 | + def get_module_tree(self): |
| 92 | + self.is_valid(raise_exception=True) |
| 93 | + with connection.cursor() as cursor: |
| 94 | + query = """ |
| 95 | + WITH RECURSIVE module_tree AS (SELECT id, name, user_id, workspace_id, parent_id, 1 AS level \ |
| 96 | + FROM %s \ |
| 97 | + WHERE parent_id = 'NONE' \ |
| 98 | + AND workspace_id = %s \ |
| 99 | + UNION ALL \ |
| 100 | + SELECT m.id, \ |
| 101 | + m.name, \ |
| 102 | + m.user_id, \ |
| 103 | + m.workspace_id, \ |
| 104 | + m.parent_id, \ |
| 105 | + mt.level + 1 \ |
| 106 | + FROM your_app_module m \ |
| 107 | + JOIN module_tree mt ON m.parent_id = mt.id) |
| 108 | + SELECT * \ |
| 109 | + FROM module_tree |
| 110 | + ORDER BY level, name \ |
| 111 | + """ |
| 112 | + module_table = self.data.get('source').lower() + '_module' |
| 113 | + params = [module_table, self.data.get('workspace_id', 'default')] |
| 114 | + |
| 115 | + cursor.execute(query, params) |
| 116 | + columns = [col[0] for col in cursor.description] |
| 117 | + return [dict(zip(columns, row)) for row in cursor.fetchall()] |
0 commit comments