mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-22 15:51:19 +00:00
Splited api across sevral files, deactivated readonly
This commit is contained in:
parent
cc619daf45
commit
6c107ed3d4
@ -29,19 +29,25 @@ class ManageModelMixin:
|
||||
serializer = self.get_serializer(self.queryset)
|
||||
return Response(serializer.data)
|
||||
|
||||
class RightReadOnlyModelViewSet(ManageModelMixin, viewsets.ReadOnlyModelViewSet):
|
||||
class RightModelViewSet(ManageModelMixin, viewsets.ModelViewSet):
|
||||
|
||||
def dispatch(self, request, *arg, **kwargs):
|
||||
res = super(RightReadOnlyModelViewSet,
|
||||
res = super(RightModelViewSet,
|
||||
self).dispatch(request, *arg, **kwargs)
|
||||
obj = self.queryset
|
||||
user = self.request.user
|
||||
try:
|
||||
if (check_if(obj, user, can_view)):
|
||||
if (request.method == 'GET' and check_if(obj, user, can_view)):
|
||||
return res
|
||||
if (request.method != 'GET' and check_if(obj, user, can_edit)):
|
||||
return res
|
||||
except: pass # To prevent bug with Anonymous user
|
||||
raise PermissionDenied
|
||||
|
||||
|
||||
from .api import *
|
||||
from .serializers import *
|
||||
from .counter import *
|
||||
from .user import *
|
||||
from .club import *
|
||||
from .group import *
|
||||
from .launderette import *
|
126
api/views/api.py
126
api/views/api.py
@ -1,20 +1,8 @@
|
||||
import datetime
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import detail_route
|
||||
from rest_framework.decorators import list_route
|
||||
|
||||
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 RightReadOnlyModelViewSet
|
||||
|
||||
@api_view(['GET'])
|
||||
def RenderMarkdown(request):
|
||||
@ -24,117 +12,3 @@ def RenderMarkdown(request):
|
||||
if request.method == 'GET':
|
||||
return Response(markdown(request.GET['text']))
|
||||
|
||||
|
||||
class CounterViewSet(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
Manage Counters (api/v1/counter/)
|
||||
"""
|
||||
|
||||
serializer_class = serializers.CounterRead
|
||||
queryset = Counter.objects.all()
|
||||
|
||||
@list_route()
|
||||
def bar(self, request):
|
||||
"""
|
||||
Return all bars (api/v1/counter/bar/)
|
||||
"""
|
||||
self.queryset = self.queryset.filter(type="BAR")
|
||||
serializer = self.get_serializer(self.queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class UserViewSet(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
Manage Users (api/v1/user/)
|
||||
Only show active users
|
||||
"""
|
||||
|
||||
serializer_class = serializers.UserRead
|
||||
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(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
Manage Clubs (api/v1/club/)
|
||||
"""
|
||||
|
||||
serializer_class = serializers.ClubRead
|
||||
queryset = Club.objects.all()
|
||||
|
||||
class GroupViewSet(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
Manage Groups (api/v1/group/)
|
||||
"""
|
||||
|
||||
serializer_class = serializers.GroupRead
|
||||
queryset = RealGroup.objects.all()
|
||||
|
||||
class LaunderettePlaceViewSet(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
Manage Launderette (api/v1/launderette/place/)
|
||||
"""
|
||||
|
||||
serializer_class = serializers.LaunderettePlaceRead
|
||||
queryset = Launderette.objects.all()
|
||||
|
||||
class LaunderetteMachineViewSet(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
Manage Washing Machines (api/v1/launderette/machine/)
|
||||
"""
|
||||
|
||||
serializer_class = serializers.LaunderetteMachineRead
|
||||
queryset = Machine.objects.all()
|
||||
|
||||
class LaunderetteTokenViewSet(RightReadOnlyModelViewSet):
|
||||
"""
|
||||
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)
|
||||
|
21
api/views/club.py
Normal file
21
api/views/club.py
Normal file
@ -0,0 +1,21 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from club.models import Club
|
||||
|
||||
from api.views import RightModelViewSet
|
||||
|
||||
|
||||
class ClubSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Club
|
||||
fields = ('id', 'name', 'unix_name', 'address', 'members')
|
||||
|
||||
|
||||
class ClubViewSet(RightModelViewSet):
|
||||
"""
|
||||
Manage Clubs (api/v1/club/)
|
||||
"""
|
||||
|
||||
serializer_class = ClubSerializer
|
||||
queryset = Club.objects.all()
|
37
api/views/counter.py
Normal file
37
api/views/counter.py
Normal file
@ -0,0 +1,37 @@
|
||||
from rest_framework import serializers
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import list_route
|
||||
|
||||
from counter.models import Counter
|
||||
|
||||
from api.views import RightModelViewSet
|
||||
|
||||
|
||||
class CounterSerializer(serializers.ModelSerializer):
|
||||
|
||||
is_open = serializers.BooleanField(read_only=True)
|
||||
barman_list = serializers.ListField(
|
||||
child=serializers.IntegerField()
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Counter
|
||||
fields = ('id', 'name', 'type', 'is_open', 'barman_list')
|
||||
|
||||
|
||||
class CounterViewSet(RightModelViewSet):
|
||||
"""
|
||||
Manage Counters (api/v1/counter/)
|
||||
"""
|
||||
|
||||
serializer_class = CounterSerializer
|
||||
queryset = Counter.objects.all()
|
||||
|
||||
@list_route()
|
||||
def bar(self, request):
|
||||
"""
|
||||
Return all bars (api/v1/counter/bar/)
|
||||
"""
|
||||
self.queryset = self.queryset.filter(type="BAR")
|
||||
serializer = self.get_serializer(self.queryset, many=True)
|
||||
return Response(serializer.data)
|
20
api/views/group.py
Normal file
20
api/views/group.py
Normal file
@ -0,0 +1,20 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.models import RealGroup
|
||||
|
||||
from api.views import RightModelViewSet
|
||||
|
||||
|
||||
class GroupSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = RealGroup
|
||||
|
||||
|
||||
class GroupViewSet(RightModelViewSet):
|
||||
"""
|
||||
Manage Groups (api/v1/group/)
|
||||
"""
|
||||
|
||||
serializer_class = GroupSerializer
|
||||
queryset = RealGroup.objects.all()
|
95
api/views/launderette.py
Normal file
95
api/views/launderette.py
Normal file
@ -0,0 +1,95 @@
|
||||
from rest_framework import serializers
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import list_route
|
||||
|
||||
from launderette.models import Launderette, Machine, Token
|
||||
|
||||
from api.views import RightModelViewSet
|
||||
|
||||
class LaunderettePlaceSerializer(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 LaunderetteMachineSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Machine
|
||||
fields = ('id', 'name', 'is_working', 'launderette')
|
||||
|
||||
class LaunderetteTokenSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Token
|
||||
fields = ('id', 'name', 'type', 'launderette', 'borrow_date',
|
||||
'user', 'is_avaliable')
|
||||
|
||||
class LaunderettePlaceViewSet(RightModelViewSet):
|
||||
"""
|
||||
Manage Launderette (api/v1/launderette/place/)
|
||||
"""
|
||||
|
||||
serializer_class = LaunderettePlaceSerializer
|
||||
queryset = Launderette.objects.all()
|
||||
|
||||
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()
|
||||
|
||||
@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)
|
@ -1,66 +0,0 @@
|
||||
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
|
||||
|
||||
|
||||
class CounterRead(serializers.ModelSerializer):
|
||||
|
||||
is_open = serializers.BooleanField(read_only=True)
|
||||
barman_list = serializers.ListField(
|
||||
child=serializers.IntegerField()
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Counter
|
||||
fields = ('id', 'name', 'type', 'is_open', 'barman_list')
|
||||
|
||||
|
||||
class UserRead(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id', 'first_name', 'last_name', 'email',
|
||||
'date_of_birth', 'nick_name', 'is_active', 'date_joined')
|
||||
|
||||
|
||||
class ClubRead(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Club
|
||||
fields = ('id', 'name', 'unix_name', 'address', 'members')
|
||||
|
||||
|
||||
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')
|
37
api/views/user.py
Normal file
37
api/views/user.py
Normal file
@ -0,0 +1,37 @@
|
||||
import datetime
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import list_route
|
||||
|
||||
from core.models import User
|
||||
|
||||
from api.views import RightModelViewSet
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id', 'first_name', 'last_name', 'email',
|
||||
'date_of_birth', 'nick_name', 'is_active', 'date_joined')
|
||||
|
||||
|
||||
class UserViewSet(RightModelViewSet):
|
||||
"""
|
||||
Manage Users (api/v1/user/)
|
||||
Only show active users
|
||||
"""
|
||||
|
||||
serializer_class = UserSerializer
|
||||
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)
|
Loading…
Reference in New Issue
Block a user