serializers.py 2.47 KB
Newer Older
1
2
3
from typing import Any

from django.db.models import Model
4
from django.utils.translation import ugettext_lazy as _
Gijs Hendriksen's avatar
Gijs Hendriksen committed
5
from rest_framework import serializers
6
from rest_framework.exceptions import ValidationError
Gijs Hendriksen's avatar
Gijs Hendriksen committed
7

8
9
from payments.api.fields import PaymentTypeField
from payments.models import Payment
Gijs Hendriksen's avatar
Gijs Hendriksen committed
10
from pizzas.models import Product, PizzaEvent, Order
11
from pizzas.services import can_change_order
Gijs Hendriksen's avatar
Gijs Hendriksen committed
12
13
14
15
16
17
18
19
20


class PizzaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('pk', 'name', 'description', 'price', 'available')


class PizzaEventSerializer(serializers.ModelSerializer):
21
22
23
    class Meta:
        model = PizzaEvent
        fields = ('start', 'end', 'event', 'title', 'is_admin')
24
25

    event = serializers.PrimaryKeyRelatedField(read_only=True)
26
    is_admin = serializers.SerializerMethodField('_is_admin')
27

28
29
30
    def _is_admin(self, instance):
        member = self.context['request'].member
        return can_change_order(member, instance)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
31
32
33
34
35


class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
36
37
38
39
        fields = ('pk', 'payment', 'product', 'name', 'member')
        read_only_fields = ('pk', 'payment', 'name', 'member')

    payment = PaymentTypeField(source='payment.type',
40
41
                               choices=Payment.PAYMENT_TYPE,
                               read_only=True)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
42
43
44
45
46


class AdminOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
47
48
49
50
        fields = ('pk', 'payment', 'product', 'name', 'member')

    payment = PaymentTypeField(source='payment.type',
                               choices=Payment.PAYMENT_TYPE)
51
52

    def validate(self, attrs):
53
        if attrs.get('member') and attrs.get('name'):
54
55
56
57
            raise ValidationError({
                'member': _('Either specify a member or a name'),
                'name': _('Either specify a member or a name'),
            })
58
        if not (attrs.get('member') or attrs.get('name')) and not self.partial:
59
60
            attrs['member'] = self.context['request'].member
        return super().validate(attrs)
61
62
63
64
65
66
67

    def update(self, instance: Model, validated_data: Any) -> Any:
        if validated_data.get(
            'payment', {}
        ).get('type', instance.payment.type) != instance.payment.type:
            instance.payment.type = validated_data['payment']['type']
            instance.payment.save()
68
69
        if 'payment' in validated_data:
            del validated_data['payment']
70
        return super().update(instance, validated_data)