serializers.py 文件源码

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

项目:munch-core 作者: crunchmail 项目源码 文件源码
def validate(self, attrs):
        # this is defined as a pre_save because cant be checked in clean() as
        # there is not yet instance.message at this step.
        same_named = attrs['message'].attachments.filter(
            file__endswith='/{}'.format(attrs['file'].name))
        if same_named.exists():
            raise ValidationError((
                'A file with the same name already exists '
                'for this message : {}').format(same_named.first()))

        # Now check whether we would fit within the total allowed
        # attachment size for the message
        total_size = 0
        for attachment in attrs['message'].attachments.all():
            total_size += attachment.size
        total_size += attrs['file'].size
        if total_size > settings.CAMPAIGNS['ATTACHMENT_TOTAL_MAX_SIZE']:
            raise ValidationError(_(
                'Can not accept this file. Total attachment size for this '
                'message ({} bytes) would exceed the maximum allowed size of '
                '{} bytes'.format(
                    total_size, settings.CAMPAIGNS[
                        'ATTACHMENT_TOTAL_MAX_SIZE'])))
        return attrs
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号