import time
from utils.base import APIView
from gm_upload import upload, upload_file
from openpyxl import load_workbook


class VirtualVote(APIView):

    def post(self, request):
        
        xls_file = request.FILES.get('file')
        if not xls_file:
            return {'message': u'上传失败，请重新上传', 'code': 500}

        wb = load_workbook(xls_file)
        data = wb.get_sheet_by_name(wb.get_sheet_names()[0])

        columns = [item for item in data.columns]
        column_data = columns[0]

        column_data = [item.value for item in column_data]
        if '用户id' not in column_data:
            
            return {'message': u'文件数据格式错误，请确保内容中有”用户id“标题', 'code': 500}
        user_ids = []
        for item in column_data:
            try:
                user_id = int(item)
                user_ids.append(user_id)
            except:
                continue
        if not user_ids:
            return {'message': u'上传失败，文件第一列不存在user_id数据', 'code': 500}

        self.rpc['venus/sun/tools/virtual_vote'](user_ids=user_ids).unwrap()

        return {'message': u'上传成功', 'code': 200}
