# -*- coding: utf-8 -*-

from django_response import response
from logit import logit

from account.models import UserInfo
from utils.error.errno_utils import LensmanStatusCode, TokenStatusCode
from utils.redis.connect import r


@logit
def login_qrcode_api(request):
    lensman_type = int(request.POST.get('lensman_type', 0))
    unionid = request.POST.get('unionid', '')
    token = request.POST.get('token', '')

    if not r.token_exists(unionid, token):
        return response(TokenStatusCode.TOKEN_HAS_EXPIRED)

    # 用户校验
    try:
        user = UserInfo.objects.get(unionid=unionid, islensman=True, status=True)
    except UserInfo.DoesNotExist:
        return response(LensmanStatusCode.LENSMAN_NOT_FOUND)

    # 用户状态校验
    if lensman_type == UserInfo.COMMON:  # 普通摄影师校验
        if user.is_common_lensman and user.user_status != UserInfo.ACTIVATED:
            return response(LensmanStatusCode.LENSMAN_NOT_ACTIVATED)
    elif lensman_type == UserInfo.OUTTAKE:  # 花絮摄影师校验
        if user.is_outtake_lensman and user.outtake_status != UserInfo.ACTIVATED:
            return response(LensmanStatusCode.LENSMAN_NOT_ACTIVATED)

    return response(200, 'Lensman Login Success', u'摄影师登录成功', user.data)