launderette.py 4.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#

25
26
from rest_framework import serializers
from rest_framework.response import Response
27
from rest_framework.decorators import action
28
29
30
31
32

from launderette.models import Launderette, Machine, Token

from api.views import RightModelViewSet

Sli's avatar
Sli committed
33

34
35
36
class LaunderettePlaceSerializer(serializers.ModelSerializer):

    machine_list = serializers.ListField(
Sli's avatar
Sli committed
37
        child=serializers.IntegerField(), read_only=True
38
    )
Sli's avatar
Sli committed
39
    token_list = serializers.ListField(child=serializers.IntegerField(), read_only=True)
40
41
42

    class Meta:
        model = Launderette
Sli's avatar
Sli committed
43
44
45
46
47
48
49
50
        fields = (
            "id",
            "name",
            "counter",
            "machine_list",
            "token_list",
            "get_absolute_url",
        )
51
52


Sli's avatar
Sli committed
53
class LaunderetteMachineSerializer(serializers.ModelSerializer):
54
55
    class Meta:
        model = Machine
Sli's avatar
Sli committed
56
        fields = ("id", "name", "type", "is_working", "launderette")
57
58


Sli's avatar
Sli committed
59
class LaunderetteTokenSerializer(serializers.ModelSerializer):
60
61
    class Meta:
        model = Token
Sli's avatar
Sli committed
62
63
64
65
66
67
68
69
70
71
        fields = (
            "id",
            "name",
            "type",
            "launderette",
            "borrow_date",
            "user",
            "is_avaliable",
        )

72
73
74
75
76
77
78
79
80

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

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

Sli's avatar
Sli committed
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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()

99
    @action(detail=False)
100
101
102
103
    def washing(self, request):
        """
            Return all washing tokens (api/v1/launderette/token/washing)
        """
Sli's avatar
Sli committed
104
        self.queryset = self.queryset.filter(type="WASHING")
105
106
107
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

108
    @action(detail=False)
109
110
111
112
    def drying(self, request):
        """
            Return all drying tokens (api/v1/launderette/token/drying)
        """
Sli's avatar
Sli committed
113
        self.queryset = self.queryset.filter(type="DRYING")
114
115
116
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

117
    @action(detail=False)
118
119
120
121
    def avaliable(self, request):
        """
            Return all avaliable tokens (api/v1/launderette/token/avaliable)
        """
Sli's avatar
Sli committed
122
123
124
        self.queryset = self.queryset.filter(
            borrow_date__isnull=True, user__isnull=True
        )
125
126
127
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

128
    @action(detail=False)
129
130
131
132
    def unavaliable(self, request):
        """
            Return all unavaliable tokens (api/v1/launderette/token/unavaliable)
        """
Sli's avatar
Sli committed
133
134
135
        self.queryset = self.queryset.filter(
            borrow_date__isnull=False, user__isnull=False
        )
136
137
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)