user.py 文件源码

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

项目:jamdb 作者: CenterForOpenScience 项目源码 文件源码
def patch(self, handler):
        if not handler.payload:
            raise exceptions.BadRequest()

        if not handler.payload.attributes.get('id'):
            raise exceptions.BadRequest(detail='Id must be provided')

        error = None
        id = handler.payload.attributes.pop('id')

        try:
            doc = self.collection.read(id)
        except exceptions.NotFound as e:
            error = e

        permissions = handler.current_user.permissions | Permissions.get_permissions(handler.current_user, doc)
        if error or not (permissions & Permissions.UPDATE):
            return handler.write({
                'data': {
                    'id': id,
                    'type': 'suppressions',
                    'attributes': {}
                }
            })

        email = self.extract_email(doc=doc)
        headers = {'Authorization': 'Bearer {}'.format(self.sendgrid_key)}

        for group, subscribe in list(handler.payload.attributes.items()):
            if subscribe:
                async with aiohttp.post('https://api.sendgrid.com/v3/asm/groups/{}/suppressions'.format(group), headers=headers, data=json.dumps({'recipient_emails': [email]})) as response:
                    assert response.status == 201  # TODO Handle errors
            else:
                async with aiohttp.delete('https://api.sendgrid.com/v3/asm/groups/{}/suppressions/{}'.format(group, quote(email)), headers=headers) as response:
                    assert response.status == 204  # TODO Handle errors
            handler.payload.attributes[group] = bool(subscribe)

        return handler.write({
            'data': {
                'id': id,
                'type': 'supressions',
                'attributes': handler.payload.attributes
            }
        })
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号