launderette.py 2.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.decorators import list_route

from launderette.models import Launderette, Machine, Token

from api.views import RightModelViewSet

class LaunderettePlaceSerializer(serializers.ModelSerializer):

    machine_list = serializers.ListField(
Sli's avatar
Sli committed
12
13
        child=serializers.IntegerField(),
        read_only=True
14
15
    )
    token_list = serializers.ListField(
Sli's avatar
Sli committed
16
17
        child=serializers.IntegerField(),
        read_only=True
18
19
20
21
22
23
24
25
26
27
28
    )

    class Meta:
        model = Launderette
        fields = ('id', 'name', 'counter', 'machine_list',
                  'token_list', 'get_absolute_url')

class LaunderetteMachineSerializer(serializers.ModelSerializer):

    class Meta:
        model = Machine
Sli's avatar
Sli committed
29
        fields = ('id', 'name', 'type', 'is_working', 'launderette')
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

class LaunderetteTokenSerializer(serializers.ModelSerializer):

    class Meta:
        model = Token
        fields = ('id', 'name', 'type', 'launderette', 'borrow_date',
                  'user', 'is_avaliable')

class LaunderettePlaceViewSet(RightModelViewSet):
    """
        Manage Launderette (api/v1/launderette/place/)
    """

    serializer_class = LaunderettePlaceSerializer
    queryset = Launderette.objects.all()

class LaunderetteMachineViewSet(RightModelViewSet):
    """
        Manage Washing Machines (api/v1/launderette/machine/)
    """

    serializer_class = LaunderetteMachineSerializer
    queryset = Machine.objects.all()


class LaunderetteTokenViewSet(RightModelViewSet):
    """
        Manage Launderette's tokens (api/v1/launderette/token/)
    """

    serializer_class = LaunderetteTokenSerializer
    queryset = Token.objects.all()

    @list_route()
    def washing(self, request):
        """
            Return all washing tokens (api/v1/launderette/token/washing)
        """
        self.queryset = self.queryset.filter(type='WASHING')
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

    @list_route()
    def drying(self, request):
        """
            Return all drying tokens (api/v1/launderette/token/drying)
        """
        self.queryset = self.queryset.filter(type='DRYING')
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

    @list_route()
    def avaliable(self, request):
        """
            Return all avaliable tokens (api/v1/launderette/token/avaliable)
        """
        self.queryset = self.queryset.filter(borrow_date__isnull=True, user__isnull=True)
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

    @list_route()
    def unavaliable(self, request):
        """
            Return all unavaliable tokens (api/v1/launderette/token/unavaliable)
        """
        self.queryset = self.queryset.filter(borrow_date__isnull=False, user__isnull=False)
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)