# Path: djangoblog/utils.py
# def cache_decorator(expiration=3 * 60):
#     def wrapper(func):
#         def news(*args, **kwargs):
#             try:
#                 view = args[0]
#                 key = view.get_cache_key()
#             except BaseException:
#                 key = None
#             if not key:
#                 unique_str = repr((func, args, kwargs))
# 
#                 m = sha256(unique_str.encode('utf-8'))
#                 key = m.hexdigest()
#             value = cache.get(key)
#             if value is not None:
#                 # logger.info('cache_decorator get cache:%s key:%s' % (func.__name__, key))
#                 if str(value) == '__default_cache_value__':
#                     return None
#                 else:
#                     return value
#             else:
#                 logger.info(
#                     'cache_decorator set cache:%s key:%s' %
#                     (func.__name__, key))
#                 value = func(*args, **kwargs)
#                 if value is None:
#                     cache.set(key, '__default_cache_value__', expiration)
#                 else:
#                     cache.set(key, value, expiration)
#                 return value
# 
#         return news
# 
#     return wrapper
# 
# Path: oauth/models.py
# class OAuthUser(models.Model):
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='用户',
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     openid = models.CharField(max_length=50)
#     nikename = models.CharField(max_length=50, verbose_name='昵称')
#     token = models.CharField(max_length=150, null=True, blank=True)
#     picture = models.CharField(max_length=350, blank=True, null=True)
#     type = models.CharField(blank=False, null=False, max_length=50)
#     email = models.CharField(max_length=50, null=True, blank=True)
#     matedata = models.TextField(null=True, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def __str__(self):
#         return self.nikename
# 
#     class Meta:
#         verbose_name = 'oauth用户'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']
# 
# class OAuthConfig(models.Model):
#     TYPE = (
#         ('weibo', '微博'),
#         ('google', '谷歌'),
#         ('github', 'GitHub'),
#         ('facebook', 'FaceBook'),
#         ('qq', 'QQ'),
#     )
#     type = models.CharField('类型', max_length=10, choices=TYPE, default='a')
#     appkey = models.CharField(max_length=200, verbose_name='AppKey')
#     appsecret = models.CharField(max_length=200, verbose_name='AppSecret')
#     callback_url = models.CharField(
#         max_length=200,
#         verbose_name='回调地址',
#         blank=False,
#         default='http://www.baidu.com')
#     is_enable = models.BooleanField(
#         '是否显示', default=True, blank=False, null=False)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def clean(self):
#         if OAuthConfig.objects.filter(
#                 type=self.type).exclude(
#             id=self.id).count():
#             raise ValidationError(_(self.type + '已经存在'))
# 
#     def __str__(self):
#         return self.type
# 
#     class Meta:
#         verbose_name = 'oauth配置'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']


import json
import logging
import urllib.parse
import requests
from abc import ABCMeta, abstractmethod
from djangoblog.utils import cache_decorator
from oauth.models import OAuthUser, OAuthConfig
        from urllib import parse
        else:
            raise OAuthAccessTokenException(rsp)

    def get_open_id(self):
        if self.is_access_token_set:
            params = {
                'access_token': self.access_token
            }
            rsp = self.do_get(self.OPEN_ID_URL, params)
            if rsp:
                rsp = rsp.replace(
                    'callback(', '').replace(
                    ')', '').replace(
                    ';', '')
                obj = json.loads(rsp)
                openid = str(obj['openid'])
                self.openid = openid
                return openid

    def get_oauth_userinfo(self):
        openid = self.get_open_id()
        if openid:
            params = {
                'access_token': self.access_token,
                'oauth_consumer_key': self.client_id,
                'openid': self.openid
            }
            rsp = self.do_get(self.API_URL, params)
            logger.info(rsp)
            obj = json.loads(rsp)

# Path: djangoblog/utils.py
# def cache_decorator(expiration=3 * 60):
#     def wrapper(func):
#         def news(*args, **kwargs):
#             try:
#                 view = args[0]
#                 key = view.get_cache_key()
#             except BaseException:
#                 key = None
#             if not key:
#                 unique_str = repr((func, args, kwargs))
# 
#                 m = sha256(unique_str.encode('utf-8'))
#                 key = m.hexdigest()
#             value = cache.get(key)
#             if value is not None:
#                 # logger.info('cache_decorator get cache:%s key:%s' % (func.__name__, key))
#                 if str(value) == '__default_cache_value__':
#                     return None
#                 else:
#                     return value
#             else:
#                 logger.info(
#                     'cache_decorator set cache:%s key:%s' %
#                     (func.__name__, key))
#                 value = func(*args, **kwargs)
#                 if value is None:
#                     cache.set(key, '__default_cache_value__', expiration)
#                 else:
#                     cache.set(key, value, expiration)
#                 return value
# 
#         return news
# 
#     return wrapper
# 
# Path: oauth/models.py
# class OAuthUser(models.Model):
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='用户',
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     openid = models.CharField(max_length=50)
#     nikename = models.CharField(max_length=50, verbose_name='昵称')
#     token = models.CharField(max_length=150, null=True, blank=True)
#     picture = models.CharField(max_length=350, blank=True, null=True)
#     type = models.CharField(blank=False, null=False, max_length=50)
#     email = models.CharField(max_length=50, null=True, blank=True)
#     matedata = models.TextField(null=True, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def __str__(self):
#         return self.nikename
# 
#     class Meta:
#         verbose_name = 'oauth用户'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']
# 
# class OAuthConfig(models.Model):
#     TYPE = (
#         ('weibo', '微博'),
#         ('google', '谷歌'),
#         ('github', 'GitHub'),
#         ('facebook', 'FaceBook'),
#         ('qq', 'QQ'),
#     )
#     type = models.CharField('类型', max_length=10, choices=TYPE, default='a')
#     appkey = models.CharField(max_length=200, verbose_name='AppKey')
#     appsecret = models.CharField(max_length=200, verbose_name='AppSecret')
#     callback_url = models.CharField(
#         max_length=200,
#         verbose_name='回调地址',
#         blank=False,
#         default='http://www.baidu.com')
#     is_enable = models.BooleanField(
#         '是否显示', default=True, blank=False, null=False)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def clean(self):
#         if OAuthConfig.objects.filter(
#                 type=self.type).exclude(
#             id=self.id).count():
#             raise ValidationError(_(self.type + '已经存在'))
# 
#     def __str__(self):
#         return self.type
# 
#     class Meta:
#         verbose_name = 'oauth配置'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']


# Path: oauth/oauthmanager.py
import json
import logging
import urllib.parse
import requests
from abc import ABCMeta, abstractmethod
from djangoblog.utils import cache_decorator
from oauth.models import OAuthUser, OAuthConfig
        from urllib import parse

        else:
            raise OAuthAccessTokenException(rsp)

    def get_open_id(self):
        if self.is_access_token_set:
            params = {
                'access_token': self.access_token
            }
            rsp = self.do_get(self.OPEN_ID_URL, params)
            if rsp:
                rsp = rsp.replace(
                    'callback(', '').replace(
                    ')', '').replace(
                    ';', '')
                obj = json.loads(rsp)
                openid = str(obj['openid'])
                self.openid = openid
                return openid

    def get_oauth_userinfo(self):
        openid = self.get_open_id()
        if openid:
            params = {
                'access_token': self.access_token,
                'oauth_consumer_key': self.client_id,
                'openid': self.openid
            }
            rsp = self.do_get(self.API_URL, params)
            logger.info(rsp)
            obj = json.loads(rsp)

            user = OAuthUser()