serializers.py 文件源码

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

项目:timed-backend 作者: adfinis-sygroup 项目源码 文件源码
def validate(self, data):
        """Validate the employment as a whole.

        Ensure the end date is after the start date and there is only one
        active employment per user and there are no overlapping employments.

        :throws: django.core.exceptions.ValidationError
        :return: validated data
        :rtype:  dict
        """
        instance = self.instance
        start_date = data.get('start_date', instance and instance.start_date)
        end_date = data.get('end_date', instance and instance.end_date)
        if end_date and start_date >= end_date:
            raise ValidationError(_(
                'The end date must be after the start date'
            ))

        user = data.get('user', instance and instance.user)
        employments = models.Employment.objects.filter(user=user)
        # end date not set means employment is ending today
        end_date = end_date or date.today()
        employments = employments.annotate(
            end=Coalesce('end_date', Value(date.today()))
        )
        if instance:
            employments = employments.exclude(id=instance.id)

        if any([
            e.start_date <= end_date and start_date <= e.end
            for e in employments
        ]):
            raise ValidationError(_(
                'A user can\'t have multiple employments at the same time'
            ))

        return data
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号