Esempio n. 1
0
    def create(self, validated_data):
        user = self.context.get('request').user
        image = None

        if validated_data.get('url') is not None:
            url = validated_data.get('url')
        elif validated_data.get('image') is not None:
            image = validated_data.get('image')
            image.open()
            try:
                url = get_instance_url_from_image(image)
            except Exception as e:
                raise serializers.ValidationError(
                    "No Open Badges data could be extracted from image: "
                    + e.message
                )
        elif validated_data.get('assertion') is not None:
            url = get_instance_url_from_assertion(
                validated_data.get('assertion')
            )

        try:
            rbi = RemoteBadgeInstance(url)
        except DjangoValidationError as e:
            raise serializers.ValidationError(e.message)

        abi = AnalyzedBadgeInstance(rbi, recipient_ids=[id.email for id in user.emailaddress_set.all()])
        if len(
            [x for x in abi.non_component_errors if x[0] == 'error.recipient']
        ) != 0:
            raise serializers.ValidationError(RECIPIENT_ERROR)

        if not abi.is_valid():
            badgr_log.debug(
                "Invalid earned badge uploaded by %s with instance_url %s and errors %s" %
                (user.username, abi.instance_url, abi.all_errors())
            )
            raise serializers.ValidationError(abi.all_errors())
        else:
            instance_kwargs = {'recipient_user': user}
            if image is not None:
                instance_kwargs['image'] = image

            new_instance = StoredBadgeInstance.from_analyzed_instance(
                abi, **instance_kwargs
            )

            return new_instance
Esempio n. 2
0
    def create(self, validated_data):
        user = self.context.get('request').user
        image = None

        if validated_data.get('url') is not None:
            url = validated_data.get('url')
        elif validated_data.get('image') is not None:
            image = validated_data.get('image')
            image.open()
            try:
                url = get_instance_url_from_image(image)
            except Exception as e:
                raise serializers.ValidationError(
                    "No Open Badges data could be extracted from image: " +
                    e.message)
        elif validated_data.get('assertion') is not None:
            url = get_instance_url_from_assertion(
                validated_data.get('assertion'))

        try:
            rbi = RemoteBadgeInstance(url)
        except DjangoValidationError as e:
            raise serializers.ValidationError(e.message)

        abi = AnalyzedBadgeInstance(
            rbi,
            recipient_ids=[id.email for id in user.emailaddress_set.all()])
        if len(
            [x for x in abi.non_component_errors if x[0] == 'error.recipient'
             ]) != 0:
            raise serializers.ValidationError(RECIPIENT_ERROR)

        if not abi.is_valid():
            badgr_log.debug(
                "Invalid earned badge uploaded by %s with instance_url %s and errors %s"
                % (user.username, abi.instance_url, abi.all_errors()))
            raise serializers.ValidationError(abi.all_errors())
        else:
            instance_kwargs = {'recipient_user': user}
            if image is not None:
                instance_kwargs['image'] = image

            new_instance = StoredBadgeInstance.from_analyzed_instance(
                abi, **instance_kwargs)

            return new_instance
Esempio n. 3
0
    def create(self, validated_data):
        user = self.context.get('request').user
        image = None

        if validated_data.get('url') is not None:
            url = validated_data.get('url')
        elif validated_data.get('image') is not None:
            image = validated_data.get('image')
            image.open()
            url = get_instance_url_from_image(image)
        elif validated_data.get('assertion') is not None:
            url = get_instance_url_from_assertion(
                validated_data.get('assertion'))

        try:
            rbi = RemoteBadgeInstance(url)
        except DjangoValidationError as e:
            raise e

        abi = AnalyzedBadgeInstance(
            rbi,
            recipient_ids=[id.email for id in user.emailaddress_set.all()])
        if len(
            [x for x in abi.non_component_errors if x[0] == 'error.recipient'
             ]) != 0:
            raise serializers.ValidationError(RECIPIENT_ERROR)

        if not abi.is_valid():
            raise serializers.ValidationError(abi.all_errors())
        else:
            instance_kwargs = {'recipient_user': user}
            if image is not None:
                instance_kwargs['image'] = image

            new_instance = StoredBadgeInstance.from_analyzed_instance(
                abi, **instance_kwargs)

            return new_instance
Esempio n. 4
0
    def create(self, validated_data):
        user = self.context.get('request').user
        image = None

        if validated_data.get('url') is not None:
            url = validated_data.get('url')
        elif validated_data.get('image') is not None:
            image = validated_data.get('image')
            image.open()
            url = get_instance_url_from_image(image)
        elif validated_data.get('assertion') is not None:
            url = get_instance_url_from_assertion(
                validated_data.get('assertion')
            )

        try:
            rbi = RemoteBadgeInstance(url)
        except DjangoValidationError as e:
            raise e

        abi = AnalyzedBadgeInstance(rbi, recipient_ids=[id.email for id in user.emailaddress_set.all()])
        if len(
            [x for x in abi.non_component_errors if x[0] == 'error.recipient']
        ) != 0:
            raise serializers.ValidationError(RECIPIENT_ERROR)

        if not abi.is_valid():
            raise serializers.ValidationError(abi.all_errors())
        else:
            instance_kwargs = {'recipient_user': user}
            if image is not None:
                instance_kwargs['image'] = image

            new_instance = StoredBadgeInstance.from_analyzed_instance(
                abi, **instance_kwargs
            )

            return new_instance