# coding=utf-8

from utils.custom_modelviewset import CustomModelViewSet
from rest_framework.decorators import action
from rest_framework import generics

from django.conf import settings
from .models import OperationHelp,OperationHelpUpfile
from utils.permission import isLogin
from .serializer import OperationHelpSerializer
from apps.tenant import tenant_log
from apps.log.models import BizLog
from utils.exceptions import CustomError
from .filters import OperationHelpFilter
from utils import response_ok, response_error
from django.db import transaction

class OperationHelpViewSet(CustomModelViewSet):
    permission_classes = [isLogin, ]
    queryset = OperationHelp.objects.filter()
    serializer_class = OperationHelpSerializer

    def filter_queryset(self, queryset):
        queryset = queryset.filter(tenant=self.request.user.employee.tenant)
        f = OperationHelpFilter(self.request.GET, queryset=queryset)
        return f.qs

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return response_ok(serializer.data)

    def perform_create(self, serializer):
        super(OperationHelpViewSet,self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        tenant_log(self.request.user.employee, BizLog.INSERT, u'添加运维帮助[%s]，id=%d' % (instance.title, instance.id),
                   validated_data)

    def perform_update(self, serializer):
        super(OperationHelpViewSet,self).perform_update(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        tenant_log(self.request.user.employee, BizLog.INSERT, u'修改运维帮助[%s]，id=%d' % (instance.title, instance.id),
                   validated_data)

    @action(methods=['post'], detail=False)
    def upload_image(self, request):
        file = self.request.FILES.get('file')
        user = self.request.user
        operation_help_img = OperationHelpUpfile.objects.create(tenant=user.employee.tenant)
        operation_help_img._add_img(file, user.id)
        image = operation_help_img.file_path
        if image:
            data={
                "src": '%s%s' % (settings.SERVER_DOMAIN, image)
            }
            return response_ok(data)
        return response_error('上传失败，请重新上传！')

    @action(methods=['post'], detail=False)
    def upload_radio(self, request):
        file = self.request.FILES.get('file')
        user = self.request.user
        operation_help_radio = OperationHelpUpfile.objects.create(tenant=user.employee.tenant)
        operation_help_radio._add_radio(file, user.id)
        radio_path = operation_help_radio.file_path
        if radio_path:
            data = {
                "src": '%s%s' % (settings.SERVER_DOMAIN, radio_path)
            }
            return response_ok(data)
        return response_error('上传失败，请重新上传！')

    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            if instance.tenant != request.user.employee.tenant:
                raise CustomError(u'禁止跨企业操作！')
            operation_help_files = OperationHelpUpfile.objects.filter(operation_help=instance)
            for operation_help_file in operation_help_files:
                operation_help_file.del_file()
            super(OperationHelpViewSet, self).destroy(self, request, *args, **kwargs)
            tenant_log(self.request.user.employee, BizLog.DELETE, u'删除运维帮助[%s]，id=%d' % (instance.title, instance.id))
        return response_ok()
