Ejemplo n.º 1
0
class RevenueSerializer(serializers.ModelSerializer):
    author = SmallUserSerializer(required=False, read_only=True)

    class Meta:
        model = Revenue
        fields = '__all__'
        read_only_fields = ['author']
Ejemplo n.º 2
0
class SmallRestaurantSerializer(serializers.ModelSerializer):
    author = SmallUserSerializer(required=False, read_only=True)

    class Meta:
        model = Restaurant
        fields = ['id', 'author', 'created', 'updated', 'name', 'country', 'street', 'city', 'zip', 'website', 'phone',
                  'email', 'image']
        read_only_fields = ['author']
Ejemplo n.º 3
0
class FeedSerializer(serializers.ModelSerializer):
    actor = SmallUserSerializer(read_only=True)
    action_object = GenericRelatedField(read_only=True)
    target = GenericRelatedField(read_only=True)

    class Meta:
        model = Action
        fields = ('actor', 'verb', 'action_object', 'target')
Ejemplo n.º 4
0
class SmallThreadSerializer(serializers.HyperlinkedModelSerializer):
    user = SmallUserSerializer()

    class Meta:
        model = Thread
        fields = ('id', 'name', 'user', 'created', 'edited', 'course', 'document', 'url')

        extra_kwargs = {
            'course': {'lookup_field': 'slug'},
        }
Ejemplo n.º 5
0
class ProposalSerializer(ModelSerializer):
    user = SmallUserSerializer(read_only=True)
    job = JobSerializer(read_only=True)

    class Meta:
        model = Proposal
        fields = '__all__'
        read_only_fields = ("created_at", "updated_at", "user", "id", "job")

    def create(self, validated_data):
        if 'request' in self.context:
            user = self.context['request'].user
            validated_data.update({user: user})
Ejemplo n.º 6
0
class ThreadSerializer(serializers.HyperlinkedModelSerializer):
    user = SmallUserSerializer()
    message_set = MessageSerializer(many=True)

    class Meta:
        model = Thread
        fields = ('id', 'name', 'user', 'created', 'edited', 'group',
                  'document', 'url', 'message_set')

        extra_kwargs = {
            'group': {
                'lookup_field': 'slug'
            },
        }
Ejemplo n.º 7
0
class ArticleSerializer(serializers.ModelSerializer):
    author = SmallUserSerializer(required=False, read_only=True)
    total_price = serializers.SerializerMethodField()
    date = serializers.SerializerMethodField()
    tag = TagSerializer(read_only=True)

    def get_total_price(self, obj):
        return obj.quantity * obj.price

    def get_date(self, obj):
        return obj.invoice.date

    class Meta:
        model = Article
        fields = ['id', 'created', 'updated', 'item', 'quantity', 'price', 'tag', 'invoice', 'author', 'date',
                  'total_price']
        read_only_fields = ['author']
Ejemplo n.º 8
0
class InvoiceSerializer(serializers.ModelSerializer):
    author = SmallUserSerializer(required=False, read_only=True)
    articles = ArticleSerializer(required=False, read_only=True, many=True)
    number_of_articles = serializers.SerializerMethodField()
    total_amount = serializers.SerializerMethodField('get_total_amount')

    def get_number_of_articles(self, obj):
        return obj.articles.all().count()

    def get_total_amount(self, obj):

        total_amount = 0
        for article in obj.articles.all():
            total_amount += article.price * article.quantity
        return total_amount

    class Meta:
        model = Invoice
        fields = ['id', 'created', 'updated', 'date', 'shop', 'restaurant', 'author', 'articles', 'number_of_articles', 'total_amount']
        read_only_fields = ['author']
Ejemplo n.º 9
0
class DocumentSerializer(serializers.HyperlinkedModelSerializer):
    tags = TagSerializer(many=True)
    user = SmallUserSerializer()

    has_perm = serializers.SerializerMethodField()

    def get_has_perm(self, document):
        user = self.context['request'].user
        return user.write_perm(obj=document)

    class Meta:
        model = Document
        fields = ('id', 'name', 'url', 'group', 'description', 'user', 'pages',
                  'date', 'views', 'downloads', 'state', 'md5', 'tags',
                  'has_perm')

        extra_kwargs = {
            'user': {
                'lookup_field': 'netid'
            },
            'group': {
                'lookup_field': 'slug'
            },
        }
Ejemplo n.º 10
0
class DocumentSerializer(serializers.HyperlinkedModelSerializer):
    tags = TagSerializer(read_only=True, many=True)

    user = SmallUserSerializer(read_only=True)

    user_vote = serializers.SerializerMethodField(read_only=True)
    has_perm = serializers.SerializerMethodField(read_only=True)
    file_size = serializers.SerializerMethodField(read_only=True)

    original_url = serializers.HyperlinkedIdentityField(
        view_name='document-original',
    )

    pdf_url = serializers.HyperlinkedIdentityField(
        view_name='document-pdf',
    )

    class Meta:
        model = Document

        read_only_fields = (
            'course', 'date', 'downloads', 'file_size',
            'file_type', 'has_perm', 'id', 'is_processing',
            'is_ready', 'is_unconvertible', 'md5', 'pages', 'state',
            'url', 'user', 'user_vote', 'views', 'votes',
            'original_url', 'pdf_url', 'imported',
        )
        writable_fields = (
            'description', 'name', 'tags'
        )

        fields = writable_fields + read_only_fields

        extra_kwargs = {
            'user': {'lookup_field': 'netid'},
            'course': {'lookup_field': 'slug'},
        }

    def get_user_vote(self, document):
        user = self.context['request'].user
        # We do the filtering in python as this method is called from REST with all the necessary
        #   data already prefetched. Using self.vote_set.filter() would lead to another roundtrip
        #   to the database for each document. Thats bad.
        users_vote = None
        for vote in document.vote_set.all():
            if vote.user == user:
                users_vote = vote
                break

        if users_vote is None:
            return 0
        elif users_vote.vote_type == Vote.UPVOTE:
            return 1
        elif users_vote.vote_type == Vote.DOWNVOTE:
            return -1
        else:
            raise NotImplemented("Vote not of known type.")

    def get_has_perm(self, document):
        user = self.context['request'].user
        return user.write_perm(obj=document)

    def get_file_size(self, document):
        try:
            return document.original.size
        except (FileNotFoundError, NoSuchKey):
            return None
Ejemplo n.º 11
0
class VeryShortThreadSerializer(PolymorphicSerializer):
    user = SmallUserSerializer()

    class Meta:
        model = Thread
        fields = ('id', 'name', 'user', 'obj_type')
Ejemplo n.º 12
0
class VeryShortMessageSerializer(PolymorphicSerializer):
    user = SmallUserSerializer()

    class Meta:
        model = Message
        fields = ('id', 'user', 'thread', 'text', 'obj_type')
Ejemplo n.º 13
0
class MessageSerializer(serializers.HyperlinkedModelSerializer):
    user = SmallUserSerializer()

    class Meta:
        model = Message
        fields = ('id', 'user', 'created', 'edited', 'thread', 'text')
Ejemplo n.º 14
0
class DocumentSerializer(serializers.HyperlinkedModelSerializer):
    tags = TagSerializer(read_only=True, many=True)

    user = SmallUserSerializer(read_only=True)

    user_vote = serializers.SerializerMethodField(read_only=True)
    has_perm = serializers.SerializerMethodField(read_only=True)
    file_size = serializers.SerializerMethodField(read_only=True)

    original_url = serializers.HyperlinkedIdentityField(
        view_name="document-original", )

    pdf_url = serializers.HyperlinkedIdentityField(view_name="document-pdf", )

    class Meta:
        model = Document

        read_only_fields = (
            "course",
            "date",
            "downloads",
            "file_size",
            "file_type",
            "has_perm",
            "id",
            "is_processing",
            "is_ready",
            "is_unconvertible",
            "md5",
            "pages",
            "state",
            "url",
            "user",
            "user_vote",
            "views",
            "votes",
            "original_url",
            "pdf_url",
            "imported",
        )
        writable_fields = ("description", "name", "tags")

        fields = writable_fields + read_only_fields

        extra_kwargs = {
            "user": {
                "lookup_field": "netid"
            },
            "course": {
                "lookup_field": "slug"
            },
        }

    def get_user_vote(self, document: Document) -> int:
        user = self.context["request"].user
        # We do the filtering in python as this method is called from REST with all the necessary
        #   data already prefetched. Using self.vote_set.filter() would lead to another roundtrip
        #   to the database for each document. Thats bad.
        users_vote = None
        for vote in document.vote_set.all():
            if vote.user == user:
                users_vote = vote
                break

        if users_vote is None:
            return 0
        elif users_vote.vote_type == Vote.VoteType.UPVOTE:
            return 1
        elif users_vote.vote_type == Vote.VoteType.DOWNVOTE:
            return -1
        else:
            raise NotImplementedError("Vote not of known type.")

    def get_has_perm(self, document: Document) -> bool:
        user = self.context["request"].user
        return user.write_perm(obj=document)

    def get_file_size(self, document) -> int | None:
        try:
            return document.original.size
        except FileNotFoundError:
            return None