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

from __future__ import division

from django.conf import settings
from django.shortcuts import render
from pywe_component import get_pre_auth_url
from pywe_component_preauthcode import component_preauthcode
from pywe_storage import RedisStorage

from utils.redis.connect import r


COMPONENT = settings.WECHAT.get('COMPONENT', {})


def preauth(request):
    return render(request, 'page/preauth/preauth.html', {
        'domain': settings.DOMAIN,
        'preauthurl': settings.DJANGO_DEBUG_PREAUTHURL or get_pre_auth_url(
            COMPONENT.get('appID'),
            pre_auth_code=component_preauthcode(appid=COMPONENT['appID'], secret=COMPONENT['appsecret'], storage=RedisStorage(r)),
            redirect_uri=settings.DJANGO_WE_COMPONENT_REDIRECT_URI,
            auth_type=3,
            mobi=request.weixin,
        ),
    })


def preauth_success(request):
    return render(request, 'page/preauth/preauth_success.html', {
        'domain': settings.DOMAIN,
        'authorizer_appid': settings.DJANGO_DEBUG_AUTHORIZER_APPID,
    })
