serializers.py 1.37 KB
Newer Older
1 2 3
# standard library imports
from __future__ import absolute_import, print_function, unicode_literals
# third party imports
4
from rest_framework import serializers
5 6
# imports from your apps
from trades.models import Listing
7 8


9 10 11 12 13 14
class ListingSerializer(serializers.HyperlinkedModelSerializer):

    url = serializers.HyperlinkedIdentityField(
        view_name = 'detail_listing',
        lookup_field = 'slug'
    )
15

16 17 18 19 20
    active = serializers.SerializerMethodField()
    final_price = serializers.SerializerMethodField()
    old = serializers.SerializerMethodField()
    num_bids = serializers.SerializerMethodField()
    num_flags = serializers.SerializerMethodField()
21

22
    def get_active(self, listing):
23 24
        return listing.active()

25
    def get_final_price(self, listing):
26 27
        return listing.final_price()

28
    def get_old(self, listing):
29 30
        return listing.old()

31 32 33 34 35 36
    def get_num_bids(self, listing):
        return listing.bid_set.all().count()

    def get_num_flags(self, listing):
        return listing.flag_set.all().count()

37 38
    class Meta:
        model = Listing
39 40
        fields = ('url', 'slug',
                  'title', 'author', 'isbn', 'year', 'edition', 'condition',
41
                  'access_code', 'course_abbr', 'description', 'price', 'photo',
42
                  'num_bids', 'num_flags', 'active', 'old',
43
                  'exchanged', 'cancelled', 'date_closed', 'final_price')