Sith/api/views/api.py

141 lines
4.1 KiB
Python
Raw Normal View History

2016-08-07 20:57:38 +00:00
import datetime
2016-08-06 10:37:36 +00:00
from django.shortcuts import get_object_or_404
2016-08-04 22:50:48 +00:00
from rest_framework.response import Response
from rest_framework.decorators import api_view
2016-08-05 18:01:23 +00:00
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from rest_framework.decorators import list_route
2016-08-04 22:50:48 +00:00
from core.templatetags.renderer import markdown
2016-08-05 18:01:23 +00:00
from counter.models import Counter
2016-08-07 20:57:38 +00:00
from core.models import User, RealGroup
from club.models import Club
2016-08-07 22:33:02 +00:00
from launderette.models import Launderette, Machine, Token
2016-08-06 10:37:36 +00:00
from api.views import serializers
from api.views import RightManagedModelViewSet
2016-08-04 22:50:48 +00:00
@api_view(['GET'])
def RenderMarkdown(request):
"""
Render Markdown
"""
if request.method == 'GET':
return Response(markdown(request.GET['text']))
2016-08-05 18:01:23 +00:00
class CounterViewSet(RightManagedModelViewSet):
2016-08-05 18:01:23 +00:00
"""
Manage Counters (api/v1/counter/)
2016-08-05 18:01:23 +00:00
"""
serializer_class = serializers.CounterRead
2016-08-05 18:01:23 +00:00
queryset = Counter.objects.all()
@list_route()
def bar(self, request):
"""
Return all bars (api/v1/counter/bar/)
2016-08-05 18:01:23 +00:00
"""
self.queryset = self.queryset.filter(type="BAR")
2016-08-05 18:01:23 +00:00
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
class UserViewSet(RightManagedModelViewSet):
"""
Manage Users (api/v1/user/)
2016-08-07 20:57:38 +00:00
Only show active users
"""
serializer_class = serializers.UserRead
2016-08-07 20:57:38 +00:00
queryset = User.objects.filter(is_active=True)
@list_route()
def birthday(self, request):
"""
Return all users born today (api/v1/user/birstdays)
"""
date = datetime.datetime.today()
self.queryset = self.queryset.filter(date_of_birth=date)
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
class ClubViewSet(RightManagedModelViewSet):
"""
Manage Clubs (api/v1/club/)
"""
serializer_class = serializers.ClubRead
queryset = Club.objects.all()
class GroupViewSet(RightManagedModelViewSet):
"""
Manage Groups (api/v1/group/)
"""
serializer_class = serializers.GroupRead
2016-08-07 20:57:38 +00:00
queryset = RealGroup.objects.all()
2016-08-07 22:33:02 +00:00
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)