def validate_friend(self, value):
        friend = get_author(value.get('uuid'), value.get('node'))

        if not friend:
            raise serializers.ValidationError("Invalid or Unknown friend in 'query: friendrequest'")

        return friend
    def create(self, validated_data):
        """
        Creates and return a new `Comment` instance, given the validated data.
        """

        # Pop nested relationships, we need to handle them separately
        author_data = validated_data.pop('author', None)
        validated_data['author'] = get_author(author_data.get('uuid'), author_data.get('node'))

        return super(CommentSerializer, self).create(validated_data)
    def update(self, instance, validated_data):
        """
        Updates and returns an instance of the `Comment` Model with validated data
        """

        # Pop nested relationships, we need to handle them separately
        author_data = validated_data.pop('author', None)
        pubDate = validated_data.get('pubDate', None)

        # Only Update comments if they are newer
        if pubDate and pubDate > instance.pubDate:
            # Set the author
            validated_data['author'] = get_author(author_data.get('uuid'), author_data.get('node'))
            # Call Super to update the Comment instance
            instance = super(CommentSerializer, self).update(instance, validated_data)

        return instance
Пример #4
0
    def create(self, validated_data):
        """
        Creates and return a new `Post` instance, given the validated data.
        """

        # Pop nested relationships, we need to handle them separately
        author_data = validated_data.pop('author')
        comment_data = validated_data.pop('comments')
        categories_data = validated_data.pop('categories')
        source_node = validated_data.pop('node', None)

        # Get the Author
        author = get_author(author_data.get('uuid'), author_data.get('node'))

        # Create the post
        post = Post.objects.create(author=author, **validated_data)

        # Append source
        if source_node is not None:
            post.source = source_node.host
            post.save()

        # Add the categories
        for category in categories_data:
            post.categories.add(category)

        # Create the comments
        for comment_json in comment_data:
            # Have to rename the vars.... this is stupid...
            comment_json = self.repackage_comment(comment_json)

            comment = Comment.objects.filter(guid=comment_json.get('id')).first()
            if comment is None:
                serializer = CommentSerializer(data=comment_json)
                serializer.is_valid(raise_exception=True)
                serializer.save(post=post)

        return post