# -*- coding:utf-8 -*-
# edit by fuzongfei
import base64
import datetime
# Create your views here.
import json

from django.http import Http404, HttpResponse
from django.utils import timezone
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework.exceptions import PermissionDenied
from rest_framework.generics import ListAPIView, GenericAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet

from libs import permissions
from libs.Pagination import Pagination
from libs.RenderColumns import render_dynamic_columns
from libs.response import JsonResponseV1
from sqlorders import models, serializers
from sqlorders.filters import SqlOrderListFilter, GetTasksListFilter


class GetDBEnvironment(ListAPIView):
    queryset = models.DbEnvironment.objects.all()
    serializer_class = serializers.DbEnvironmentSerializer

    # 获取工单环境
    def get(self, request, *args, **kwargs):
        serializer = self.get_serializer(self.get_queryset(), many=True)
        return JsonResponseV1(data=serializer.data)


class GetDbSchemas(APIView):
    # 获取指定环境指定用途的schemas列表
    def get(self, request):
        serializer = serializers.DbSchemasSerializer(data=request.query_params)
        if serializer.is_valid():
            return JsonResponseV1(data=serializer.query)
        return JsonResponseV1(message=serializer.errors, code='0001')


class IncepSyntaxCheckView(APIView):
    def post(self, request, *args, **kwargs):
        serializer = serializers.IncepSyntaxCheckSerializer(data=request.data)

        if serializer.is_valid():
            s, data = serializer.check()
            render_columns = [
                {'key': 'order_id', 'value': '序号'},
                {'key': 'stage', 'value': '阶段'},
                {'key': 'stage_status', 'value': '阶段状态'},
                {'key': 'error_level', 'value': '错误级别'},
                {'key': 'error_message', 'value': '错误信息', 'width': '35%'},
                {'key': 'sql', 'value': 'SQL内容', 'width': '25%', 'ellipsis': True},
                {'key': 'affected_rows', 'value': '影响/扫描行数'}
            ]
            columns = render_dynamic_columns(render_columns)
            message = '语法检查未发现异常，可以提交'
            if not s:
                message = '语法检查发现异常，详情请查看输出，更正后在提交'
            d = {
                'status': 0 if s else 1,
                'data': data
            }
            data = {'columns': columns, 'data': d}
            return JsonResponseV1(data=data, message=message)
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class SqlOrdersCommit(GenericAPIView):
    permission_classes = (permissions.CanCommitOrdersPermission,)
    serializer_class = serializers.SqlOrdersCommitSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(message="提交成功")
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class SqlOrdersList(ListAPIView):
    permission_classes = (permissions.CanViewOrdersPermission,)
    queryset = models.DbOrders.objects.all()
    serializer_class = serializers.SqlOrdersListSerializer
    pagination_class = Pagination
    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    filter_class = SqlOrderListFilter
    ordering = ['-created_at']
    search_fields = ['title', 'database', 'remark', 'applicant', 'progress', 'contents']

    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        serializer = self.get_serializer(page, many=True)
        render_columns = [
            {'key': 'progress', 'value': '进度', 'width': '8%'},
            {'key': 'applicant', 'value': '申请人'},
            {'key': 'department', 'value': '部门'},
            {'key': 'env_name', 'value': '环境'},
            {'key': 'escape_title', 'value': '标题', 'width': '18%', 'ellipsis': True},
            {'key': 'sql_type', 'value': '类型'},
            {'key': 'remark', 'value': '备注'},
            {'key': 'version', 'value': '版本'},
            {'key': 'host', 'value': '实例/库'},
            {'key': 'auditor', 'value': '审核人'},
            {'key': 'reviewer', 'value': '复核人'},
        ]
        columns = render_dynamic_columns(render_columns)
        data = {'columns': columns, 'data': serializer.data}
        return self.get_paginated_response(data)


class SqlOrdersDetail(ListAPIView):
    """SQL工单详情"""
    permission_classes = (permissions.CanViewOrdersPermission,)
    queryset = models.DbOrders.objects.all()
    serializer_class = serializers.SqlOrderDetailSerializer
    lookup_field = 'order_id'

    def get(self, request, *args, **kwargs):
        queryset = self.get_object()
        serializer = self.get_serializer(queryset, context={"request": request})
        return JsonResponseV1(data=serializer.data)


class OpSqlOrderView(ViewSet):
    """更新SQL工单状态，如：审核，关闭等"""
    permission_classes = (permissions.CanViewOrdersPermission,)

    def get_obj(self, pk):
        try:
            obj = models.DbOrders.objects.get(pk=pk)
            return obj
        except models.DbOrders.DoesNotExist:
            raise Http404

    def approve(self, request, pk):
        serializer = serializers.OpSqlOrderSerializer(instance=self.get_obj(pk),
                                                      data=request.data,
                                                      context={"request": request, "handler": "_approve"})

        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(data=serializer.data, message="操作成功")
        return JsonResponseV1(message=serializer.errors, code='0001')

    def feedback(self, request, pk):
        serializer = serializers.OpSqlOrderSerializer(instance=self.get_obj(pk),
                                                      data=request.data,
                                                      context={"request": request, "handler": "_feedback"})
        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(data=serializer.data, message="操作成功")
        return JsonResponseV1(message=serializer.errors, code='0001')

    def close(self, request, pk):
        serializer = serializers.OpSqlOrderSerializer(instance=self.get_obj(pk),
                                                      data=request.data,
                                                      context={"request": request, "handler": "_close"})
        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(data=serializer.data, message="操作成功")
        return JsonResponseV1(message=serializer.errors, code='0001')

    def review(self, request, pk):
        serializer = serializers.OpSqlOrderSerializer(instance=self.get_obj(pk),
                                                      data=request.data,
                                                      context={"request": request, "handler": "_review"})
        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(data=serializer.data, message="操作成功")
        return JsonResponseV1(message=serializer.errors, code='0001')


class GenerateTasksView(APIView):
    permission_classes = (permissions.CanExecuteOrdersPermission,)

    def post(self, request, *args, **kwargs):
        serializer = serializers.GenerateSqlOrdersTasksSerializer(data=request.data)

        if serializer.is_valid():
            data = serializer.save(request)
            return JsonResponseV1(data=data)
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class GetTaskIdView(APIView):
    def get(self, request, *args, **kwargs):
        """根据order id返回taskid"""
        order_id = kwargs.get('order_id')
        task_id = models.DbOrdersExecuteTasks.objects.filter(order_id=order_id).first().task_id
        return JsonResponseV1(data=task_id)


class GetTasksPreviewView(ListAPIView):
    permission_classes = (permissions.CanViewOrdersPermission,)
    queryset = models.DbOrdersExecuteTasks.objects.all()
    serializer_class = serializers.SqlOrdersTasksListSerializer
    pagination_class = Pagination
    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    filter_class = GetTasksListFilter
    search_fields = ['sql']
    ordering = ['created_time']

    def get(self, request, *args, **kwargs):
        task_id = kwargs.get('task_id')
        queryset = self.filter_queryset(self.get_queryset().filter(task_id=task_id))

        # 数据隐藏按钮打开了
        # 仅允许申请人、审核人、复核人和超权用户查看数据
        obj = models.DbOrders.objects.get(
            pk=models.DbOrdersExecuteTasks.objects.filter(task_id=task_id).first().order_id
        )
        if obj.is_hide == 'ON' and not request.user.is_superuser:
            allowed_view_users = [obj.applicant]
            allowed_view_users.extend([x['user'] for x in json.loads(obj.auditor)])
            allowed_view_users.extend([x['user'] for x in json.loads(obj.reviewer)])
            if request.user.username not in allowed_view_users:
                raise PermissionDenied(detail='您没有权限查看该工单的数据，5s后，自动跳转到工单列表页面')

        origin_queryset = self.queryset.filter(task_id=task_id)
        total = origin_queryset.count()
        progress_0 = origin_queryset.filter(progress=0).count()
        progress_1 = origin_queryset.filter(progress=1).count()
        progress_3 = origin_queryset.filter(progress=3).count()

        page = self.paginate_queryset(queryset)
        serializer = self.get_serializer(page, context={'request': request}, many=True)
        render_columns = [
            {'key': 'num', 'value': '序号'},  # 自定义num，前台显示序号使用
            {'key': 'applicant', 'value': '申请人'},
            {'key': 'sql', 'value': 'SQL', 'ellipsis': True, 'width': '50%'},
            {'key': 'progress', 'value': '进度'},
            {'key': 'result', 'value': '查看结果'},  # 自定义result
        ]
        columns = render_dynamic_columns(render_columns)
        data = {'columns': columns,
                'data': {'data': serializer.data,
                         'total': total,
                         'progress_0': progress_0,
                         'progress_1': progress_1,
                         'progress_3': progress_3}}
        return self.get_paginated_response(data)


class GetTasksListView(ListAPIView):
    permission_classes = (permissions.CanViewOrdersPermission,)
    queryset = models.DbOrdersExecuteTasks.objects.all()
    serializer_class = serializers.SqlOrdersTasksListSerializer
    pagination_class = Pagination
    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    filter_class = GetTasksListFilter
    search_fields = ['sql']
    ordering = ['created_time']

    def get(self, request, *args, **kwargs):
        task_id = kwargs.get('task_id')
        queryset = self.filter_queryset(self.get_queryset().filter(task_id=task_id))

        # 数据隐藏按钮打开了
        # 仅允许申请人、审核人、复核人和超权用户查看数据
        obj = models.DbOrders.objects.get(
            pk=models.DbOrdersExecuteTasks.objects.filter(task_id=task_id).first().order_id
        )
        if obj.is_hide == 'ON' and not request.user.is_superuser:
            allowed_view_users = [obj.applicant]
            allowed_view_users.extend([x['user'] for x in json.loads(obj.auditor)])
            allowed_view_users.extend([x['user'] for x in json.loads(obj.reviewer)])
            if request.user.username not in allowed_view_users:
                raise PermissionDenied(detail='您没有权限查看该工单的数据，5s后，自动跳转到工单列表页面')

        page = self.paginate_queryset(queryset)
        serializer = self.get_serializer(page, context={'request': request}, many=True)
        render_columns = [
            {'key': 'num', 'value': '序号'},  # 自定义num，前台显示序号使用
            {'key': 'applicant', 'value': '申请人'},
            {'key': 'sql', 'value': 'SQL', 'ellipsis': True, 'width': '50%'},
            {'key': 'progress', 'value': '进度'},
            {'key': 'execute', 'value': '执行'},  # 自定义execute
            {'key': 'result', 'value': '查看结果'},  # 自定义result
        ]
        if queryset.exists():
            if queryset.first().sql_type == 'DDL':
                render_columns.insert(-1, {'key': 'ghost_pause', 'value': '暂停(gh-ost)'})
                render_columns.insert(-1, {'key': 'ghost_recovery', 'value': '恢复(gh-ost)'})
        columns = render_dynamic_columns(render_columns)
        data = {'columns': columns, 'data': serializer.data}
        return self.get_paginated_response(data)


class ExecuteSingleTaskView(APIView):
    permission_classes = (permissions.CanExecuteOrdersPermission,)

    def post(self, request, *args, **kwargs):
        serializer = serializers.ExecuteSingleTaskSerializer(data=request.data)

        if serializer.is_valid():
            serializer.execute(request)
            return JsonResponseV1(message="任务提交成功，请查看输出")
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class ExecuteMultiTasksView(APIView):
    permission_classes = (permissions.CanExecuteOrdersPermission,)

    def post(self, request, *args, **kwargs):
        serializer = serializers.ExecuteMultiTasksSerializer(data=request.data)

        if serializer.is_valid():
            serializer.execute(request)
            return JsonResponseV1(message="任务提交成功，请查看输出")
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class ThrottleTaskView(APIView):
    permission_classes = (permissions.CanExecuteOrdersPermission,)

    def post(self, request, *args, **kwargs):
        serializer = serializers.ThrottleTaskSerializer(data=request.data)

        if serializer.is_valid():
            message = serializer.execute(request)
            return JsonResponseV1(message=message)
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class GetTasksResultView(ListAPIView):
    """SQL工单详情"""
    permission_classes = (permissions.CanViewOrdersPermission,)
    queryset = models.DbOrdersExecuteTasks.objects.all()
    serializer_class = serializers.GetTasksResultSerializer
    lookup_field = 'id'

    def get(self, request, *args, **kwargs):
        queryset = self.get_object()
        serializer = self.get_serializer(queryset, context={"request": request})
        return JsonResponseV1(data=serializer.data)


class HookSqlOrdersView(APIView):
    permission_classes = (permissions.anyof(permissions.CanCommitOrdersPermission,
                                            permissions.CanViewOrdersPermission,
                                            permissions.CanExecuteOrdersPermission,
                                            permissions.CanAuditOrdersPermission),
                          )

    def post(self, request, *args, **kwargs):
        serializer = serializers.HookSqlOrdersSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(message="任务提交成功，请查看输出")
        return JsonResponseV1(message=serializer.errors, code='0001', flat=True)


class DownloadExportFilesView(APIView):
    """下载导出文件"""
    permission_classes = (permissions.CanViewOrdersPermission,)

    def get(self, request, base64_filename):
        file_name = base64.b64decode(base64_filename).decode()

        if not models.DbExportFiles.objects.filter(file_name=file_name).exists():
            raise Http404

        obj = models.DbExportFiles.objects.get(file_name=file_name)
        if not models.DbOrdersExecuteTasks.objects.get(pk=obj.task_id).applicant == request.user.username:
            raise PermissionDenied(detail='您没有权限')

        fsock = open(f"media/{obj.files}", 'rb')
        response = HttpResponse(fsock, content_type="application/zip")
        response['Content-Disposition'] = f'attachment; filename={file_name}'
        return response


class ReleaseVersionsGet(APIView):
    """获取上线版本号，提交工单使用"""

    def get(self, request):
        before_30_days = (timezone.now() - datetime.timedelta(days=30))
        queryset = models.ReleaseVersions.objects.filter(
            expire_time__gte=before_30_days
        ).values('id', 'version', 'expire_time').order_by('-created_at')
        for row in queryset:
            row['disabled'] = 0
            if row['expire_time'] < datetime.datetime.date(timezone.now()):
                row['disabled'] = 1
        return JsonResponseV1(data=queryset)


class ReleaseVersionsList(ListAPIView):
    """获取上线版本号列表，管理上线版本号使用"""
    permission_classes = (permissions.CanViewVersionPermission,)
    queryset = models.ReleaseVersions.objects.all()
    serializer_class = serializers.ReleaseVersionsListSerializer
    pagination_class = Pagination
    filter_backends = [filters.SearchFilter, filters.OrderingFilter]
    search_fields = ['username', 'version', 'expire_time']
    ordering = ['-created_at']

    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        serializer = self.get_serializer(page, many=True)
        render_columns = [
            {'key': 'version', 'value': '版本'},
            {'key': 'username', 'value': '创建人'},
            {'key': 'expire_time', 'value': '截止日期'},
            {'key': 'created_at', 'value': '创建时间'},
            {'key': 'key', 'value': '操作'},
            {'key': 'id', 'value': '详情'},
        ]
        columns = render_dynamic_columns(render_columns)
        data = {'columns': columns, 'data': serializer.data}
        return self.get_paginated_response(data)


class ReleaseVersionsCreate(CreateAPIView):
    """创建版本"""
    permission_classes = (permissions.CanCreateVersionsPermission,)
    serializer_class = serializers.ReleaseVersionsCreateSerializer

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            self.perform_create(serializer)
            return JsonResponseV1(message="创建成功")
        return JsonResponseV1(code='0001', message=serializer.errors, flat=True)


class ReleaseVersionsUpdate(UpdateAPIView):
    """更新版本号，该类只更新单条记录"""
    permission_classes = (permissions.CanUpdateVersionsPermission,)

    def put(self, request, *args, **kwargs):
        serializer = serializers.ReleaseVersionsSerializer(
            instance=models.ReleaseVersions.objects.get(pk=kwargs['key']),  # 返回单条记录
            data=request.data
        )
        if serializer.is_valid():
            serializer.save()
            return JsonResponseV1(message="更新成功")
        return JsonResponseV1(code='0001', message=serializer.errors, flat=True)


class ReleaseVersionsDelete(DestroyAPIView):
    """删除版本"""
    permission_classes = (permissions.CanDeleteVersionsPermission,)
    queryset = models.ReleaseVersions.objects.all()
    lookup_field = 'id'  # 默认为主键，可不写

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return JsonResponseV1(message="删除成功")


class ReleaseVersionsView(APIView):
    """获取指定版本内工单在所有环境的进度"""

    def get(self, request, *args, **kwargs):
        # 获取版本对应的主键
        version = kwargs.get('version')
        version_id = models.ReleaseVersions.objects.get(version=version).pk
        # 获取环境，行转为动态列
        obj = models.DbEnvironment.objects.values('id', 'name')
        row2columns = ''
        for row in obj:
            row2columns += f"max(if(env_id={row['id']}, progress, -1)) as {row['name']},"
        # 获取任务下所有工单分别在各个环境中的状态，此处的环境为动态环境
        # id没有实际意义
        query = f"select " + row2columns + \
                f"substring(MD5(RAND()),1,20) as id,title as escape_title,order_id, applicant " \
                f"from yasql_dborders where version_id='{version_id}' group by escape_title,order_id,applicant"
        rawquery = models.DbOrders.objects.raw(query)
        # 获取环境列名
        dynamic_columns = list(rawquery.columns)[:-4]
        data = []
        for row in rawquery:
            columns = {
                'id': row.id,
                'escape_title': row.escape_title,
                'order_id': row.order_id,
                'applicant': row.applicant,
            }
            for col in dynamic_columns:
                columns[col] = getattr(row, col)
            data.append(columns)

        render_columns = [
            {'key': 'escape_title', 'ellipsis': True, 'value': '标题'},
            {'key': 'applicant', 'value': '申请人'},
        ]
        render_columns.extend([{'key': x, 'value': x} for x in dynamic_columns])
        columns = render_dynamic_columns(render_columns)
        data = {'columns': columns, 'data': data}
        return JsonResponseV1(data=data)

[((868, 902), 'sqlorders.models.DbEnvironment.objects.all', 'models.DbEnvironment.objects.all', ([], {}), '()\n', (900, 902), False, 'from sqlorders import models, serializers\n'), ((3250, 3279), 'sqlorders.models.DbOrders.objects.all', 'models.DbOrders.objects.all', ([], {}), '()\n', (3277, 3279), False, 'from sqlorders import models, serializers\n'), ((4754, 4783), 'sqlorders.models.DbOrders.objects.all', 'models.DbOrders.objects.all', ([], {}), '()\n', (4781, 4783), False, 'from sqlorders import models, serializers\n'), ((8270, 8311), 'sqlorders.models.DbOrdersExecuteTasks.objects.all', 'models.DbOrdersExecuteTasks.objects.all', ([], {}), '()\n', (8309, 8311), False, 'from sqlorders import models, serializers\n'), ((10671, 10712), 'sqlorders.models.DbOrdersExecuteTasks.objects.all', 'models.DbOrdersExecuteTasks.objects.all', ([], {}), '()\n', (10710, 10712), False, 'from sqlorders import models, serializers\n'), ((14235, 14276), 'sqlorders.models.DbOrdersExecuteTasks.objects.all', 'models.DbOrdersExecuteTasks.objects.all', ([], {}), '()\n', (14274, 14276), False, 'from sqlorders import models, serializers\n'), ((16798, 16834), 'sqlorders.models.ReleaseVersions.objects.all', 'models.ReleaseVersions.objects.all', ([], {}), '()\n', (16832, 16834), False, 'from sqlorders import models, serializers\n'), ((19021, 19057), 'sqlorders.models.ReleaseVersions.objects.all', 'models.ReleaseVersions.objects.all', ([], {}), '()\n', (19055, 19057), False, 'from sqlorders import models, serializers\n'), ((1109, 1145), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data'}), '(data=serializer.data)\n', (1123, 1145), False, 'from libs.response import JsonResponseV1\n'), ((1253, 1311), 'sqlorders.serializers.DbSchemasSerializer', 'serializers.DbSchemasSerializer', ([], {'data': 'request.query_params'}), '(data=request.query_params)\n', (1284, 1311), False, 'from sqlorders import models, serializers\n'), ((1418, 1472), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""'}), "(message=serializer.errors, code='0001')\n", (1432, 1472), False, 'from libs.response import JsonResponseV1\n'), ((1579, 1636), 'sqlorders.serializers.IncepSyntaxCheckSerializer', 'serializers.IncepSyntaxCheckSerializer', ([], {'data': 'request.data'}), '(data=request.data)\n', (1617, 1636), False, 'from sqlorders import models, serializers\n'), ((2598, 2663), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (2612, 2663), False, 'from libs.response import JsonResponseV1\n'), ((3069, 3134), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (3083, 3134), False, 'from libs.response import JsonResponseV1\n'), ((4470, 4508), 'libs.RenderColumns.render_dynamic_columns', 'render_dynamic_columns', (['render_columns'], {}), '(render_columns)\n', (4492, 4508), False, 'from libs.RenderColumns import render_dynamic_columns\n'), ((5053, 5089), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data'}), '(data=serializer.data)\n', (5067, 5089), False, 'from libs.response import JsonResponseV1\n'), ((5854, 5908), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""'}), "(message=serializer.errors, code='0001')\n", (5868, 5908), False, 'from libs.response import JsonResponseV1\n'), ((6360, 6414), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""'}), "(message=serializer.errors, code='0001')\n", (6374, 6414), False, 'from libs.response import JsonResponseV1\n'), ((6860, 6914), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""'}), "(message=serializer.errors, code='0001')\n", (6874, 6914), False, 'from libs.response import JsonResponseV1\n'), ((7362, 7416), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""'}), "(message=serializer.errors, code='0001')\n", (7376, 7416), False, 'from libs.response import JsonResponseV1\n'), ((7588, 7651), 'sqlorders.serializers.GenerateSqlOrdersTasksSerializer', 'serializers.GenerateSqlOrdersTasksSerializer', ([], {'data': 'request.data'}), '(data=request.data)\n', (7632, 7651), False, 'from sqlorders import models, serializers\n'), ((7791, 7856), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (7805, 7856), False, 'from libs.response import JsonResponseV1\n'), ((8120, 8148), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'task_id'}), '(data=task_id)\n', (8134, 8148), False, 'from libs.response import JsonResponseV1\n'), ((10184, 10222), 'libs.RenderColumns.render_dynamic_columns', 'render_dynamic_columns', (['render_columns'], {}), '(render_columns)\n', (10206, 10222), False, 'from libs.RenderColumns import render_dynamic_columns\n'), ((12611, 12649), 'libs.RenderColumns.render_dynamic_columns', 'render_dynamic_columns', (['render_columns'], {}), '(render_columns)\n', (12633, 12649), False, 'from libs.RenderColumns import render_dynamic_columns\n'), ((12935, 12993), 'sqlorders.serializers.ExecuteSingleTaskSerializer', 'serializers.ExecuteSingleTaskSerializer', ([], {'data': 'request.data'}), '(data=request.data)\n', (12974, 12993), False, 'from sqlorders import models, serializers\n'), ((13142, 13207), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (13156, 13207), False, 'from libs.response import JsonResponseV1\n'), ((13383, 13441), 'sqlorders.serializers.ExecuteMultiTasksSerializer', 'serializers.ExecuteMultiTasksSerializer', ([], {'data': 'request.data'}), '(data=request.data)\n', (13422, 13441), False, 'from sqlorders import models, serializers\n'), ((13590, 13655), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (13604, 13655), False, 'from libs.response import JsonResponseV1\n'), ((13826, 13879), 'sqlorders.serializers.ThrottleTaskSerializer', 'serializers.ThrottleTaskSerializer', ([], {'data': 'request.data'}), '(data=request.data)\n', (13860, 13879), False, 'from sqlorders import models, serializers\n'), ((14031, 14096), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (14045, 14096), False, 'from libs.response import JsonResponseV1\n'), ((14540, 14576), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data'}), '(data=serializer.data)\n', (14554, 14576), False, 'from libs.response import JsonResponseV1\n'), ((14639, 14819), 'libs.permissions.anyof', 'permissions.anyof', (['permissions.CanCommitOrdersPermission', 'permissions.CanViewOrdersPermission', 'permissions.CanExecuteOrdersPermission', 'permissions.CanAuditOrdersPermission'], {}), '(permissions.CanCommitOrdersPermission, permissions.\n    CanViewOrdersPermission, permissions.CanExecuteOrdersPermission,\n    permissions.CanAuditOrdersPermission)\n', (14656, 14819), False, 'from libs import permissions\n'), ((15040, 15094), 'sqlorders.serializers.HookSqlOrdersSerializer', 'serializers.HookSqlOrdersSerializer', ([], {'data': 'request.data'}), '(data=request.data)\n', (15075, 15094), False, 'from sqlorders import models, serializers\n'), ((15233, 15298), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'serializer.errors', 'code': '"""0001"""', 'flat': '(True)'}), "(message=serializer.errors, code='0001', flat=True)\n", (15247, 15298), False, 'from libs.response import JsonResponseV1\n'), ((15655, 15708), 'sqlorders.models.DbExportFiles.objects.get', 'models.DbExportFiles.objects.get', ([], {'file_name': 'file_name'}), '(file_name=file_name)\n', (15687, 15708), False, 'from sqlorders import models, serializers\n'), ((15936, 15987), 'django.http.HttpResponse', 'HttpResponse', (['fsock'], {'content_type': '"""application/zip"""'}), "(fsock, content_type='application/zip')\n", (15948, 15987), False, 'from django.http import Http404, HttpResponse\n'), ((16616, 16645), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'queryset'}), '(data=queryset)\n', (16630, 16645), False, 'from libs.response import JsonResponseV1\n'), ((17647, 17685), 'libs.RenderColumns.render_dynamic_columns', 'render_dynamic_columns', (['render_columns'], {}), '(render_columns)\n', (17669, 17685), False, 'from libs.RenderColumns import render_dynamic_columns\n'), ((18244, 18309), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'code': '"""0001"""', 'message': 'serializer.errors', 'flat': '(True)'}), "(code='0001', message=serializer.errors, flat=True)\n", (18258, 18309), False, 'from libs.response import JsonResponseV1\n'), ((18810, 18875), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'code': '"""0001"""', 'message': 'serializer.errors', 'flat': '(True)'}), "(code='0001', message=serializer.errors, flat=True)\n", (18824, 18875), False, 'from libs.response import JsonResponseV1\n'), ((19236, 19266), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""删除成功"""'}), "(message='删除成功')\n", (19250, 19266), False, 'from libs.response import JsonResponseV1\n'), ((19551, 19600), 'sqlorders.models.DbEnvironment.objects.values', 'models.DbEnvironment.objects.values', (['"""id"""', '"""name"""'], {}), "('id', 'name')\n", (19586, 19600), False, 'from sqlorders import models, serializers\n'), ((20075, 20109), 'sqlorders.models.DbOrders.objects.raw', 'models.DbOrders.objects.raw', (['query'], {}), '(query)\n', (20102, 20109), False, 'from sqlorders import models, serializers\n'), ((20811, 20849), 'libs.RenderColumns.render_dynamic_columns', 'render_dynamic_columns', (['render_columns'], {}), '(render_columns)\n', (20833, 20849), False, 'from libs.RenderColumns import render_dynamic_columns\n'), ((20915, 20940), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'data'}), '(data=data)\n', (20929, 20940), False, 'from libs.response import JsonResponseV1\n'), ((1365, 1402), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.query'}), '(data=serializer.query)\n', (1379, 1402), False, 'from libs.response import JsonResponseV1\n'), ((2216, 2254), 'libs.RenderColumns.render_dynamic_columns', 'render_dynamic_columns', (['render_columns'], {}), '(render_columns)\n', (2238, 2254), False, 'from libs.RenderColumns import render_dynamic_columns\n'), ((2540, 2582), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'data', 'message': 'message'}), '(data=data, message=message)\n', (2554, 2582), False, 'from libs.response import JsonResponseV1\n'), ((3023, 3053), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""提交成功"""'}), "(message='提交成功')\n", (3037, 3053), False, 'from libs.response import JsonResponseV1\n'), ((5275, 5309), 'sqlorders.models.DbOrders.objects.get', 'models.DbOrders.objects.get', ([], {'pk': 'pk'}), '(pk=pk)\n', (5302, 5309), False, 'from sqlorders import models, serializers\n'), ((5786, 5838), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data', 'message': '"""操作成功"""'}), "(data=serializer.data, message='操作成功')\n", (5800, 5838), False, 'from libs.response import JsonResponseV1\n'), ((6292, 6344), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data', 'message': '"""操作成功"""'}), "(data=serializer.data, message='操作成功')\n", (6306, 6344), False, 'from libs.response import JsonResponseV1\n'), ((6792, 6844), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data', 'message': '"""操作成功"""'}), "(data=serializer.data, message='操作成功')\n", (6806, 6844), False, 'from libs.response import JsonResponseV1\n'), ((7294, 7346), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'serializer.data', 'message': '"""操作成功"""'}), "(data=serializer.data, message='操作成功')\n", (7308, 7346), False, 'from libs.response import JsonResponseV1\n'), ((7750, 7775), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'data': 'data'}), '(data=data)\n', (7764, 7775), False, 'from libs.response import JsonResponseV1\n'), ((13088, 13126), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""任务提交成功，请查看输出"""'}), "(message='任务提交成功，请查看输出')\n", (13102, 13126), False, 'from libs.response import JsonResponseV1\n'), ((13536, 13574), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""任务提交成功，请查看输出"""'}), "(message='任务提交成功，请查看输出')\n", (13550, 13574), False, 'from libs.response import JsonResponseV1\n'), ((13984, 14015), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': 'message'}), '(message=message)\n', (13998, 14015), False, 'from libs.response import JsonResponseV1\n'), ((15179, 15217), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""任务提交成功，请查看输出"""'}), "(message='任务提交成功，请查看输出')\n", (15193, 15217), False, 'from libs.response import JsonResponseV1\n'), ((15834, 15866), 'rest_framework.exceptions.PermissionDenied', 'PermissionDenied', ([], {'detail': '"""您没有权限"""'}), "(detail='您没有权限')\n", (15850, 15866), False, 'from rest_framework.exceptions import PermissionDenied\n'), ((16207, 16221), 'django.utils.timezone.now', 'timezone.now', ([], {}), '()\n', (16219, 16221), False, 'from django.utils import timezone\n'), ((16224, 16251), 'datetime.timedelta', 'datetime.timedelta', ([], {'days': '(30)'}), '(days=30)\n', (16242, 16251), False, 'import datetime\n'), ((18198, 18228), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""创建成功"""'}), "(message='创建成功')\n", (18212, 18228), False, 'from libs.response import JsonResponseV1\n'), ((18764, 18794), 'libs.response.JsonResponseV1', 'JsonResponseV1', ([], {'message': '"""更新成功"""'}), "(message='更新成功')\n", (18778, 18794), False, 'from libs.response import JsonResponseV1\n'), ((19460, 19511), 'sqlorders.models.ReleaseVersions.objects.get', 'models.ReleaseVersions.objects.get', ([], {'version': 'version'}), '(version=version)\n', (19494, 19511), False, 'from sqlorders import models, serializers\n'), ((9339, 9395), 'rest_framework.exceptions.PermissionDenied', 'PermissionDenied', ([], {'detail': '"""您没有权限查看该工单的数据，5s后，自动跳转到工单列表页面"""'}), "(detail='您没有权限查看该工单的数据，5s后，自动跳转到工单列表页面')\n", (9355, 9395), False, 'from rest_framework.exceptions import PermissionDenied\n'), ((11740, 11796), 'rest_framework.exceptions.PermissionDenied', 'PermissionDenied', ([], {'detail': '"""您没有权限查看该工单的数据，5s后，自动跳转到工单列表页面"""'}), "(detail='您没有权限查看该工单的数据，5s后，自动跳转到工单列表页面')\n", (11756, 11796), False, 'from rest_framework.exceptions import PermissionDenied\n'), ((15488, 15521), 'base64.b64decode', 'base64.b64decode', (['base64_filename'], {}), '(base64_filename)\n', (15504, 15521), False, 'import base64\n'), ((18577, 18629), 'sqlorders.models.ReleaseVersions.objects.get', 'models.ReleaseVersions.objects.get', ([], {'pk': "kwargs['key']"}), "(pk=kwargs['key'])\n", (18611, 18629), False, 'from sqlorders import models, serializers\n'), ((8027, 8088), 'sqlorders.models.DbOrdersExecuteTasks.objects.filter', 'models.DbOrdersExecuteTasks.objects.filter', ([], {'order_id': 'order_id'}), '(order_id=order_id)\n', (8069, 8088), False, 'from sqlorders import models, serializers\n'), ((15547, 15603), 'sqlorders.models.DbExportFiles.objects.filter', 'models.DbExportFiles.objects.filter', ([], {'file_name': 'file_name'}), '(file_name=file_name)\n', (15582, 15603), False, 'from sqlorders import models, serializers\n'), ((15724, 15779), 'sqlorders.models.DbOrdersExecuteTasks.objects.get', 'models.DbOrdersExecuteTasks.objects.get', ([], {'pk': 'obj.task_id'}), '(pk=obj.task_id)\n', (15763, 15779), False, 'from sqlorders import models, serializers\n'), ((16548, 16562), 'django.utils.timezone.now', 'timezone.now', ([], {}), '()\n', (16560, 16562), False, 'from django.utils import timezone\n'), ((9142, 9165), 'json.loads', 'json.loads', (['obj.auditor'], {}), '(obj.auditor)\n', (9152, 9165), False, 'import json\n'), ((9226, 9250), 'json.loads', 'json.loads', (['obj.reviewer'], {}), '(obj.reviewer)\n', (9236, 9250), False, 'import json\n'), ((11543, 11566), 'json.loads', 'json.loads', (['obj.auditor'], {}), '(obj.auditor)\n', (11553, 11566), False, 'import json\n'), ((11627, 11651), 'json.loads', 'json.loads', (['obj.reviewer'], {}), '(obj.reviewer)\n', (11637, 11651), False, 'import json\n'), ((16272, 16342), 'sqlorders.models.ReleaseVersions.objects.filter', 'models.ReleaseVersions.objects.filter', ([], {'expire_time__gte': 'before_30_days'}), '(expire_time__gte=before_30_days)\n', (16309, 16342), False, 'from sqlorders import models, serializers\n'), ((8882, 8941), 'sqlorders.models.DbOrdersExecuteTasks.objects.filter', 'models.DbOrdersExecuteTasks.objects.filter', ([], {'task_id': 'task_id'}), '(task_id=task_id)\n', (8924, 8941), False, 'from sqlorders import models, serializers\n'), ((11283, 11342), 'sqlorders.models.DbOrdersExecuteTasks.objects.filter', 'models.DbOrdersExecuteTasks.objects.filter', ([], {'task_id': 'task_id'}), '(task_id=task_id)\n', (11325, 11342), False, 'from sqlorders import models, serializers\n')]