美文网首页
第十一天

第十一天

作者: tomtiddler | 来源:发表于2018-09-12 01:29 被阅读0次

    drf的缓存设置

    django本身是支持缓存的,drf的缓存是在django缓存上的二次开发。
    安装drf extensions第三方库,安装完成之后就能直接使用了

    官网示例,记得将该mixins设置在第一个,个人猜测,是需要先检查缓存,再进行其他操作,所以先编译缓存。
    from myapps.serializers import UserSerializer
    from rest_framework_extensions.cache.mixins import CacheResponseMixin
    
    class UserViewSet(CacheResponseMixin, viewsets.ModelViewSet):
        serializer_class = UserSerializer
    

    setting中过期时间设置

    # rest_framework_extensions相关配置
    REST_FRAMEWORK_EXTENSIONS = { 
        'DEFAULT_CACHE_RESPONSE_TIMEOUT': 5,
    }
    

    用户个人操作数据,不建议缓存。公共数据,可以进行缓存。
    更多详情查看官方文档,主要是setting详情。
    官方文档http://chibisov.github.io/drf-extensions/docs/#caching

    drf配置redis缓存

    redis配置的好处:可视化查看缓存、
    pip install django-redis(django操作redis的库)
    官方文档:http://niwinz.github.io/django-redis/latest/
    中文文档:https://django-redis-chs.readthedocs.io/zh_CN/latest/
    setting中

    # 配置redis缓存
    CACHES = {
        "default": {
            "BACKEND": "django_redis.cache.RedisCache",
            "LOCATION": "redis://127.0.0.1:6379",
            "OPTIONS": {
                "CLIENT_CLASS": "django_redis.client.DefaultClient",
            }
        }
    }
    
    rest framework extensions文檔中以下函數生成key-value中的key
    DefaultKeyConstructor
    

    限速 throttle

    取ip的代码可以查看源码了解。在throttle中。drf自带的。

    # 限速策略配置,也可以在类内配置
        'DEFAULT_THROTTLE_CLASSES': (
            'rest_framework.throttling.AnonRateThrottle',  # 未登录请求,通过ip判断
            'rest_framework.throttling.UserRateThrottle'  # 登录用户请求
        ),
        'DEFAULT_THROTTLE_RATES': {
            'anon': '100/day',
            'user': '1000/day'
        }
    
    # from rest_framework.throttling import UserRateThrottle, AnonRateThrottle
        # throttle_classes = (UserRateThrottle, AnonRateThrottle)
    

    第三方登录

    第三方登录开发模式及auto2.0简介

    微博登录
    第三方登录就是跳转到第三方的登录页面,只能通过第三方,不能自己设置第三方帐号的登录页面。防止其用户信息泄漏。

    Alt text
    专门用于第三方登录的协议 auth2.0
    进入微博开放平台->
    创建应用->
    回调url必须通过设置页面进行设置然后传参,支付宝可以直接通过url传参。
    需要用到的微博接口:
    接口 说明
    OAuth2/authorize 请求用户授权Token
    OAuth2/access_token 获取授权过的Access Token

    微博获取权限的接口文档:
    http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E
    微博api(文档中有这个选项)
    http://open.weibo.com/wiki/2/users/show
    以上这些只是介绍了如何获取微博的用户权限以及信息,并未介绍如何在自己的应用中注册用户(通过获取的用户信息新建用户),所以只完成了一半,源码如下:
    微博获取用户信息分为以下三步。
    get_code-> get token -> get user_show

    # -*- coding: utf-8 -*-
    __author__ = 'tomtiddler'
    
    '''此文件仅用于测试,不用于生产过程'''
    
    import requests
    
    def get_auth_url():
        weibo_auth_url = "https://api.weibo.com/oauth2/authorize"
        redirect_url = "http://127.0.0.1/complete/weibo/"
        auth_url = weibo_auth_url+"?client_id={client_id}&redirect_uri={redirect_url}".\
            format(client_id=2473478422, redirect_url=redirect_url)
    
        print(auth_url)
    
    def get_access_token(code):
        access_token_url = "https://api.weibo.com/oauth2/access_token"
        re_dict = requests.post(access_token_url, data={
            "client_id": 2473478422,
            "client_secret": "b1380a5ad6afcdfda02a35adc880ca20",
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": "http://127.0.0.1/complete/weibo/",
        })
        pass
        # b'{"access_token":"2.00NODyRDQj95hCab6215930dxxbwOE","remind_in":"157679999","expires_in":157679999,"uid":"3013908301","isRealName":"true"}'
    
    def get_user_info(access_token="", uid=""):
        user_url = "https://api.weibo.com/2/users/show.json?access_token={access_token}&uid={uid}".format(access_token=access_token, uid=uid)
    
        print(user_url)
    
    if __name__ == "__main__":
        # get_auth_url()
        # http://127.0.0.1/complete/weibo/?code=7bee2c5f37ab843efa3da97dc647ac59
        # get_access_token("aa61f05322c79bcfc45c4fcadbc129cf")
        get_user_info(access_token="2.00NODyRDQj95hCab6215930dxxbwOE", uid="3013908301")
    
    

    相关文章

      网友评论

          本文标题:第十一天

          本文链接:https://www.haomeiwen.com/subject/hyxugftx.html