Sith/api/views/launderette.py

130 lines
3.6 KiB
Python
Raw Normal View History

# -*- coding:utf-8 -*
#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith3
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.decorators import action
from launderette.models import Launderette, Machine, Token
from api.views import RightModelViewSet
2018-10-04 19:29:19 +00:00
class LaunderettePlaceSerializer(serializers.ModelSerializer):
machine_list = serializers.ListField(
2018-10-04 19:29:19 +00:00
child=serializers.IntegerField(), read_only=True
)
2018-10-04 19:29:19 +00:00
token_list = serializers.ListField(child=serializers.IntegerField(), read_only=True)
class Meta:
model = Launderette
2018-10-04 19:29:19 +00:00
fields = (
"id",
"name",
"counter",
"machine_list",
"token_list",
"get_absolute_url",
)
2018-10-04 19:29:19 +00:00
class LaunderetteMachineSerializer(serializers.ModelSerializer):
class Meta:
model = Machine
2018-10-04 19:29:19 +00:00
fields = ("id", "name", "type", "is_working", "launderette")
2018-10-04 19:29:19 +00:00
class LaunderetteTokenSerializer(serializers.ModelSerializer):
class Meta:
model = Token
2018-10-04 19:29:19 +00:00
fields = (
"id",
"name",
"type",
"launderette",
"borrow_date",
"user",
"is_avaliable",
)
class LaunderettePlaceViewSet(RightModelViewSet):
"""
2020-08-27 13:59:42 +00:00
Manage Launderette (api/v1/launderette/place/)
"""
serializer_class = LaunderettePlaceSerializer
queryset = Launderette.objects.all()
2018-10-04 19:29:19 +00:00
class LaunderetteMachineViewSet(RightModelViewSet):
"""
2020-08-27 13:59:42 +00:00
Manage Washing Machines (api/v1/launderette/machine/)
"""
serializer_class = LaunderetteMachineSerializer
queryset = Machine.objects.all()
class LaunderetteTokenViewSet(RightModelViewSet):
"""
2020-08-27 13:59:42 +00:00
Manage Launderette's tokens (api/v1/launderette/token/)
"""
serializer_class = LaunderetteTokenSerializer
queryset = Token.objects.all()
@action(detail=False)
def washing(self, request):
"""
2020-08-27 13:59:42 +00:00
Return all washing tokens (api/v1/launderette/token/washing)
"""
2018-10-04 19:29:19 +00:00
self.queryset = self.queryset.filter(type="WASHING")
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
@action(detail=False)
def drying(self, request):
"""
2020-08-27 13:59:42 +00:00
Return all drying tokens (api/v1/launderette/token/drying)
"""
2018-10-04 19:29:19 +00:00
self.queryset = self.queryset.filter(type="DRYING")
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
@action(detail=False)
def avaliable(self, request):
"""
2020-08-27 13:59:42 +00:00
Return all avaliable tokens (api/v1/launderette/token/avaliable)
"""
2018-10-04 19:29:19 +00:00
self.queryset = self.queryset.filter(
borrow_date__isnull=True, user__isnull=True
)
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
@action(detail=False)
def unavaliable(self, request):
"""
2020-08-27 13:59:42 +00:00
Return all unavaliable tokens (api/v1/launderette/token/unavaliable)
"""
2018-10-04 19:29:19 +00:00
self.queryset = self.queryset.filter(
borrow_date__isnull=False, user__isnull=False
)
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)