#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "chenwei"
# Date: 2018/11/15

import json
from utils.base import APIView


class StarListView(APIView):
    def get(self, request):
        page = int(request.GET.get('page', 1))
        limit = int(request.GET.get('limit', 10))
        filter = self.handle_filter(request.GET.get('filter', ""))
        try:
            data = self.rpc['venus/community/star/get'](offset=page, limit=limit, filters=filter).unwrap()
        except Exception as e:
            raise e
        return data


class StarUpdateOrCreate(APIView):
    def get(self, request):
        id = request.GET.get('id')
        try:
            data = self.rpc['venus/community/star/detail'](id=id).unwrap()
        except Exception as e:
            raise e
        return {'data': data}

    def post(self, request):
        data = {
            'gender': request.POST.get('gender'),
            'region': request.POST.get('region'),
            'is_online': request.POST.get('is_online'),
            'description': request.POST.get('description'),
            'avatar': request.POST.get('avatar')[:-2],
            'group_ids': list(set(json.loads(request.POST.get('group_ids', [])))),
        }
        try:
            self.rpc['venus/community/star/create'](data=data).unwrap()
        except Exception as e:
            raise e

        return {
            'message': '更新成功'
        }