models.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Instagram 作者: Fastcampus-WPS-5th 项目源码 文件源码
def get_or_create_facebook_user(self, user_info):
        username = '{}_{}_{}'.format(
            self.model.USER_TYPE_FACEBOOK,
            settings.FACEBOOK_APP_ID,
            user_info['id']
        )
        user, user_created = self.get_or_create(
            username=username,
            user_type=self.model.USER_TYPE_FACEBOOK,
            defaults={
                'last_name': user_info.get('last_name', ''),
                'first_name': user_info.get('first_name', ''),
                'email': user_info.get('email', ''),
            }
        )
        # ??? ?? ????? ?? ??? ???? ???
        if user_created and user_info.get('picture'):
            # https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/17190366_634177590104859_7473999312831140266_n.jpg?oh=4c68d08b4066747c1c7c7bcb32670b6a&oe=59DA648E
            # ??? ??? URL
            url_picture = user_info['picture']['data']['url']
            # ?????? ????, ????? ???? ????
            p = re.compile(r'.*\.([^?]+)')
            file_ext = re.search(p, url_picture).group(1)
            file_name = '{}.{}'.format(
                user.pk,
                file_ext
            )
            # Python tempfile
            # https://docs.python.org/3/library/tempfile.html
            # ?????? ????? ????
            temp_file = NamedTemporaryFile()
            # ??? ??? URL? ?? get?? (??? ????)
            response = requests.get(url_picture)
            # ?? ??? temp_file? ??
            temp_file.write(response.content)
            # ImageField? save()???? ???? ?? ??????? ??? ??? ??? ??
            # ???? ???? ??? ?? <??pk.????????> ? ??
            user.img_profile.save(file_name, temp_file)
        return user
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号