# coding=utf-8

from utils.custom_modelviewset import CustomModelViewSet
from utils.permission import IsAdministratorUser
from utils import response_ok
from apps.log.models import BizLog
from apps.admin import admin_log

from .serializers import WechatTpSerializer
from apps.WechatTp.models import WechatTp


class WechatTpViewSet(CustomModelViewSet):
    permission_classes = [IsAdministratorUser, ]
    queryset = WechatTp.objects.filter()
    serializer_class = WechatTpSerializer

    def list(self, request, *args, **kwargs):
        instance = WechatTp.objects.filter().first()
        if not instance:
            return response_ok()
        serializer = self.get_serializer(instance)
        return response_ok(serializer.data)

    def perform_create(self, serializer):
        super(WechatTpViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        admin_log(self.request.user, BizLog.INSERT, u'添加/修改用户第三方平台信息，id=%d' % instance.id, validated_data)
