Started launderette API

This commit is contained in:
Antoine Bartuccio 2016-08-08 00:33:02 +02:00
parent bfa966e3d8
commit cefb3828bc
4 changed files with 119 additions and 2 deletions

View File

@ -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'club', ClubViewSet, base_name='api_club')
router.register(r'group', GroupViewSet, base_name='api_group') 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 = [ urlpatterns = [
# API # API

View File

@ -11,6 +11,8 @@ from core.templatetags.renderer import markdown
from counter.models import Counter from counter.models import Counter
from core.models import User, RealGroup from core.models import User, RealGroup
from club.models import Club from club.models import Club
from launderette.models import Launderette, Machine, Token
from api.views import serializers from api.views import serializers
from api.views import RightManagedModelViewSet from api.views import RightManagedModelViewSet
@ -76,3 +78,63 @@ class GroupViewSet(RightManagedModelViewSet):
serializer_class = serializers.GroupRead serializer_class = serializers.GroupRead
queryset = RealGroup.objects.all() 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)

View File

@ -1,6 +1,7 @@
from rest_framework import serializers from rest_framework import serializers
from counter.models import Counter from counter.models import Counter
from core.models import User, RealGroup from core.models import User, RealGroup
from launderette.models import Launderette, Machine, Token
from club.models import Club from club.models import Club
@ -35,3 +36,31 @@ class GroupRead(serializers.ModelSerializer):
class Meta: class Meta:
model = RealGroup 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')

View File

@ -38,6 +38,18 @@ class Launderette(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('launderette:launderette_list') 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): class Machine(models.Model):
name = models.CharField(_('name'), max_length=30) name = models.CharField(_('name'), max_length=30)
launderette = models.ForeignKey(Launderette, related_name='machines', verbose_name=_('launderette')) launderette = models.ForeignKey(Launderette, related_name='machines', verbose_name=_('launderette'))
@ -90,6 +102,12 @@ class Token(models.Model):
def __str__(self): def __str__(self):
return self.__class__._meta.verbose_name + " " + self.get_type_display() + " #" + self.name + " (" + self.launderette.name + ")" 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): class Slot(models.Model):
start_date = models.DateTimeField(_('start date')) start_date = models.DateTimeField(_('start date'))
type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES) type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)