import json
from utils.base import APIView, get_offset_count
from utils.logger import error_logger


class BeautyListView(APIView):

    def get(self, request):
        offset, count = get_offset_count(request)
        parent_word_id = request.GET.get('parent_word_id', None)
        is_online = request.GET.get('is_online', None)

        data = self.rpc['venus/sun/beauty/list'](offset=offset, count=count, parent_word_id=parent_word_id, is_online=is_online).unwrap()
        return data


class BeautyCreateView(APIView):

    def post(self, request):
        url = request.POST.get('url', None)
        parent_word_id = request.POST.get('parent_word_id', None)

        if not url or not parent_word_id:
            return r'缺少参数'

        data = self.rpc['venus/sun/beauty/add'](url=url, parent_word_id=parent_word_id).unwrap()

        return data


class BeautyUpdateView(APIView):
    
    def post(self, request):
        id_ = request.POST.get('id', None)
        url = request.POST.get('url', None)
        parent_word_id = request.POST.get('parent_word_id', None)

        if not id_:
            return r'缺少参数'

        data = self.rpc['venus/sun/beauty/edit'](id_=id_, url=url, parent_word_id=parent_word_id).unwrap()
        if not data:
            return r'更新失败'

        return data


class BeautyDeleteView(APIView):
    
    def post(self, request):
        id_ = request.POST.get('id', None)

        if not id_:
            return r'缺少参数'

        data = self.rpc['venus/sun/beauty/delete'](id_=id_).unwrap()
        if not data:
            return r'操作失败'

        return data
