mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-16 11:13:23 +00:00
Merge remote-tracking branch 'origin/api'
This commit is contained in:
commit
e8713b3c22
@ -2,11 +2,24 @@ from rest_framework.response import Response
|
||||
from rest_framework import viewsets
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from rest_framework.decorators import detail_route
|
||||
from django.db.models.query import QuerySet
|
||||
|
||||
from core.views import can_view, can_edit
|
||||
|
||||
class RightManagedModelViewSet(viewsets.ModelViewSet):
|
||||
def check_if(obj, user, test):
|
||||
"""
|
||||
Detect if it's a single object or a queryset
|
||||
aply a given test on individual object and return global permission
|
||||
"""
|
||||
if (isinstance(obj, QuerySet)):
|
||||
for o in obj:
|
||||
if (test(o, user) is False):
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
return test(obj, user)
|
||||
|
||||
class ManageModelMixin:
|
||||
@detail_route()
|
||||
def id(self, request, pk=None):
|
||||
"""
|
||||
@ -16,19 +29,25 @@ class RightManagedModelViewSet(viewsets.ModelViewSet):
|
||||
serializer = self.get_serializer(self.queryset)
|
||||
return Response(serializer.data)
|
||||
|
||||
class RightModelViewSet(ManageModelMixin, viewsets.ModelViewSet):
|
||||
|
||||
def dispatch(self, request, *arg, **kwargs):
|
||||
res = super(RightManagedModelViewSet,
|
||||
res = super(RightModelViewSet,
|
||||
self).dispatch(request, *arg, **kwargs)
|
||||
obj = self.queryset
|
||||
user = self.request.user
|
||||
try:
|
||||
if (request.method == 'GET' and can_view(obj, user)):
|
||||
if (request.method == 'GET' and check_if(obj, user, can_view)):
|
||||
return res
|
||||
elif (request.method != 'GET' and can_edit(obj, user)):
|
||||
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 RightManagedModelViewSet
|
||||
|
||||
@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(RightManagedModelViewSet):
|
||||
"""
|
||||
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(RightManagedModelViewSet):
|
||||
"""
|
||||
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(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
|
||||
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)
|
||||
|
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()
|
39
api/views/counter.py
Normal file
39
api/views/counter.py
Normal file
@ -0,0 +1,39 @@
|
||||
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(),
|
||||
read_only=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Counter
|
||||
fields = ('id', 'name', 'type', 'club',
|
||||
'products', '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()
|
97
api/views/launderette.py
Normal file
97
api/views/launderette.py
Normal file
@ -0,0 +1,97 @@
|
||||
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(),
|
||||
read_only=True
|
||||
)
|
||||
token_list = serializers.ListField(
|
||||
child=serializers.IntegerField(),
|
||||
read_only=True
|
||||
)
|
||||
|
||||
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', 'type', '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