class BookSerializer(serializers.ModelSerializer):
    authors = AuthorSerializer(many=True)
    categories = CategorySerializer(many=True)

    class Meta:
        model = Books
        fields = ('id', 'book_name', 'published_date', 'authors', 'categories')
Пример #2
0
class BookSerializer(serializers.ModelSerializer):
    authors = AuthorSerializer(many=True)

    class Meta:
        model = Book
        fields = ['id', 'name', 'edition', 'publication_year', 'authors']

    def update(self, instance, data):
        authors_data = data.pop('authors')

        instance.name = data.get('name', instance.name)
        instance.edition = data.get('edition', instance.edition)
        instance.publication_year = data.get('publication_year',
                                             instance.publication_year)

        for author in authors_data:
            author_queryset = Author.objects.filter(
                name__exact=author.get('name', None))
            if author_queryset.exists():
                author = author_queryset.first()
            else:
                raise Http404
            instance.authors.clear()
            instance.authors.add(author)

        instance.save()
        return instance
Пример #3
0
class ToDoReadSerializer(ModelSerializer):
    project = ProjectSerializer(read_only=True)
    author = AuthorSerializer(read_only=True)

    class Meta:
        model = ToDo
        fields = '__all__'
Пример #4
0
class BookSerializer(serializers.HyperlinkedModelSerializer):
    id = HashidSerializerCharField(source_field='books.Book.id')
    author = AuthorSerializer()
    #author = serializers.PrimaryKeyRelatedField(pk_field=HashidSerializerCharField(source_field='authors.Author.user'), read_only=True)

    class Meta:
        model = Book
        fields = ('url', 'id', 'name', 'author',)
Пример #5
0
class PRSerializer(serializers.ModelSerializer):
    """
    General pupose
    """
    author = AuthorSerializer(read_only=True)

    class Meta:
        model = PR
        fields = ('id', 'merge', 'author', 'title', 'description', 'status')
Пример #6
0
class BookSerializer(serializers.ModelSerializer):
    book_images = BookImageSerializer(read_only=True, many=True)
    comment_book = CommentSerializer(read_only=True, many=True)
    author_book = AuthorSerializer(read_only=True, many=True)

    class Meta:
        model = Book
        fields = ('category', 'book_name', 'author', 'description',
                  'year_of_issue', 'price', 'rating', 'book', 'book_images',
                  'comment_book', 'author_book')
Пример #7
0
class CommitSerializer(serializers.ModelSerializer):
    """
    Geneneral purpose Commit Serializer
    """
    author = AuthorSerializer(read_only=True)
    branches = BranchSerializer(read_only=True)

    class Meta:
        model = Commit
        fields = ('message', 'author', 'branches', 'files_changed', 'timestamp')
Пример #8
0
class ListCommitSerializer(serializers.ModelSerializer):
    """
    List Commit Serializer without related fields
    """
    author = AuthorSerializer(read_only=True)
    branches = BranchSerializer(read_only=True)

    class Meta:
        model = Commit
        fields = ('message', 'author', 'timestamp', 'branches')
Пример #9
0
def get_authors_view(request, slug):

    try:
        author = Author.objects.get(slug=slug)
    except Author.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "GET":

        serializer = AuthorSerializer(author)
        return Response(serializer.data)
Пример #10
0
class BookSerializer(serializers.ModelSerializer):
    author = AuthorSerializer()
    genre = GenreSerializer(many=True)

    def create(self, validated_data):
        book = Book.objects.create(
            title = validated_data.get('title'),
            year = validated_data.get('year'),
            price = validated_data.get('price'))
        b_auth = validated_data.get('author').get('name')
        try:
            auth = Author.objects.get(name=b_auth)
        except Author.DoesNotExist:
            auth = Author.objects.create(name=b_auth)
        book.author = auth
        genres = validated_data.get('genre')
        for gnr in genres:
            name = gnr.get('name')
            try:
                genre = Genre.objects.get(name=name)
            except Genre.DoesNotExist:
                genre = Genre.objects.create(name=name)
            book.genre.add(genre)
        book.save()
        mail_to_admin.delay()
        return book

    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        b_auth = validated_data.get('author', instance.author).get('name')
        try:
            auth = Author.objects.get(name=b_auth)
        except Author.DoesNotExist:
            auth = Author.objects.create(name=b_auth)
        instance.author = auth
        genres = validated_data.get('genre', instance.genre)
        for gnr in instance.genre.all():
            instance.genre.remove(gnr)
        for gnr in genres:
            name = gnr.get('name')
            try:
                genre = Genre.objects.get(name=name)
            except Genre.DoesNotExist:
                genre = Genre.objects.create(name=name)
            instance.genre.add(genre)
        instance.year = validated_data.get('year', instance.year)
        instance.price = validated_data.get('price', instance.price)
        instance.save()
        return instance

    class Meta:
        model = Book
        fields = ['id', 'title', 'author', 'genre', 'year', 'price',]
Пример #11
0
class PublicationSerializer(serializers.ModelSerializer):
    authors = AuthorSerializer(many=True, read_only=True)

    class Meta:
        model = Publications
        fields = [
            'id', 'venue', 'year', 'volume', 'doi', 'title', 'abstract',
            'n_citations', 'semantic_scholar_id', 'authors'
        ]
        read_only = [
            'id', 'venue', 'year', 'volume', 'doi', 'title', 'abstract',
            'n_citations', 'semantic_scholar_id', 'authors'
        ]
Пример #12
0
class ArticleDetailSerializer(serializers.ModelSerializer):
    author = AuthorSerializer()
    subject = SubjectSerializer()

    class Meta:
        fields = (
            "id",
            "slug",
            "title",
            "hero_image",
            "author",
            "subject",
            "publish_date",
            "text",
        )
        model = Article
Пример #13
0
class ArticleListSerializer(serializers.ModelSerializer):
    text = serializers.ReadOnlyField(source="truncate_text")
    author = AuthorSerializer()
    subject = SubjectSerializer()

    class Meta:
        fields = (
            "id",
            "slug",
            "title",
            "hero_image",
            "author",
            "subject",
            "publish_date",
            "text",
        )
        model = Article
Пример #14
0
class ProjectSerializer(ModelSerializer):
    members = AuthorSerializer(many=True, read_only=True)

    class Meta:
        model = Project
        fields = '__all__'