diff --git a/api/urls.py b/api/urls.py index 9561153b..408cc562 100644 --- a/api/urls.py +++ b/api/urls.py @@ -10,6 +10,14 @@ router.register(r'user', UserViewSet, base_name='api_user') router.register(r'club', ClubViewSet, base_name='api_club') router.register(r'group', GroupViewSet, base_name='api_group') +# Launderette +router.register(r'launderette/place', LaunderettePlaceViewSet, + base_name='api_launderette_place') +router.register(r'launderette/machine', LaunderetteMachineViewSet, + base_name='api_launderette_machine') +router.register(r'launderette/token', LaunderetteTokenViewSet, + base_name='api_launderette_token') + urlpatterns = [ # API diff --git a/api/views/api.py b/api/views/api.py index 230c11ef..6a0aa594 100644 --- a/api/views/api.py +++ b/api/views/api.py @@ -11,6 +11,8 @@ from core.templatetags.renderer import markdown from counter.models import Counter from core.models import User, RealGroup from club.models import Club +from launderette.models import Launderette, Machine, Token + from api.views import serializers from api.views import RightManagedModelViewSet @@ -76,3 +78,63 @@ class GroupViewSet(RightManagedModelViewSet): serializer_class = serializers.GroupRead queryset = RealGroup.objects.all() + +class LaunderettePlaceViewSet(RightManagedModelViewSet): + """ + Manage Launderette (api/v1/launderette/place/) + """ + + serializer_class = serializers.LaunderettePlaceRead + queryset = Launderette.objects.all() + +class LaunderetteMachineViewSet(RightManagedModelViewSet): + """ + Manage Washing Machines (api/v1/launderette/machine/) + """ + + serializer_class = serializers.LaunderetteMachineRead + queryset = Machine.objects.all() + +class LaunderetteTokenViewSet(RightManagedModelViewSet): + """ + Manage Launderette's tokens (api/v1/launderette/token/) + """ + + serializer_class = serializers.LaunderetteTokenRead + 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) diff --git a/api/views/serializers.py b/api/views/serializers.py index dea04475..ccbc6fa4 100644 --- a/api/views/serializers.py +++ b/api/views/serializers.py @@ -1,6 +1,7 @@ from rest_framework import serializers from counter.models import Counter from core.models import User, RealGroup +from launderette.models import Launderette, Machine, Token from club.models import Club @@ -8,8 +9,8 @@ class CounterRead(serializers.ModelSerializer): is_open = serializers.BooleanField(read_only=True) barman_list = serializers.ListField( - child=serializers.IntegerField() - ) + child=serializers.IntegerField() + ) class Meta: model = Counter @@ -35,3 +36,31 @@ class GroupRead(serializers.ModelSerializer): class Meta: model = RealGroup + + +class LaunderettePlaceRead(serializers.ModelSerializer): + + machine_list = serializers.ListField( + child=serializers.IntegerField() + ) + token_list = serializers.ListField( + child=serializers.IntegerField() + ) + + class Meta: + model = Launderette + fields = ('id', 'name', 'counter', 'machine_list', + 'token_list', 'get_absolute_url') + +class LaunderetteMachineRead(serializers.ModelSerializer): + + class Meta: + model = Machine + fields = ('id', 'name', 'is_working', 'launderette') + +class LaunderetteTokenRead(serializers.ModelSerializer): + + class Meta: + model = Token + fields = ('id', 'name', 'type', 'launderette', 'borrow_date', + 'user', 'is_avaliable') diff --git a/launderette/models.py b/launderette/models.py index 8b7cdbfa..b3bcf8f8 100644 --- a/launderette/models.py +++ b/launderette/models.py @@ -38,6 +38,18 @@ class Launderette(models.Model): def get_absolute_url(self): return reverse('launderette:launderette_list') + def get_machine_list(self): + return Machine.objects.filter(launderette_id=self.id) + + def machine_list(self): + return [m.id for m in self.get_machine_list()] + + def get_token_list(self): + return Token.objects.filter(launderette_id=self.id) + + def token_list(self): + return [t.id for t in self.get_token_list()] + class Machine(models.Model): name = models.CharField(_('name'), max_length=30) launderette = models.ForeignKey(Launderette, related_name='machines', verbose_name=_('launderette')) @@ -90,6 +102,12 @@ class Token(models.Model): def __str__(self): return self.__class__._meta.verbose_name + " " + self.get_type_display() + " #" + self.name + " (" + self.launderette.name + ")" + def is_avaliable(self): + if not self.borrow_date and not self.user: + return True + else: + return False + class Slot(models.Model): start_date = models.DateTimeField(_('start date')) type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)