api.py 2.08 KB
Newer Older
Sli's avatar
Sli committed
1
2
import datetime

Skia's avatar
Skia committed
3
from django.shortcuts import get_object_or_404
Sli's avatar
Sli committed
4
5
from rest_framework.response import Response
from rest_framework.decorators import api_view
Sli's avatar
Sli committed
6
7
8
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from rest_framework.decorators import list_route
Sli's avatar
Sli committed
9
10

from core.templatetags.renderer import markdown
Sli's avatar
Sli committed
11
from counter.models import Counter
Sli's avatar
Sli committed
12
from core.models import User, RealGroup
13
from club.models import Club
Skia's avatar
Skia committed
14
from api.views import serializers
15
from api.views import RightManagedModelViewSet
Sli's avatar
Sli committed
16
17
18
19
20
21
22
23

@api_view(['GET'])
def RenderMarkdown(request):
    """
        Render Markdown
    """
    if request.method == 'GET':
        return Response(markdown(request.GET['text']))
Sli's avatar
Sli committed
24
25


26
class CounterViewSet(RightManagedModelViewSet):
Sli's avatar
Sli committed
27
    """
28
        Manage Counters (api/v1/counter/)
Sli's avatar
Sli committed
29
30
    """

31
    serializer_class = serializers.CounterRead
Sli's avatar
Sli committed
32
33
34
35
36
    queryset = Counter.objects.all()

    @list_route()
    def bar(self, request):
        """
37
            Return all bars (api/v1/counter/bar/)
Sli's avatar
Sli committed
38
        """
39
        self.queryset = self.queryset.filter(type="BAR")
Sli's avatar
Sli committed
40
41
42
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

43
44
45
46

class UserViewSet(RightManagedModelViewSet):
    """
        Manage Users (api/v1/user/)
Sli's avatar
Sli committed
47
        Only show active users
48
49
50
    """

    serializer_class = serializers.UserRead
Sli's avatar
Sli committed
51
52
53
54
55
56
57
58
59
60
61
    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)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


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
Sli's avatar
Sli committed
78
    queryset = RealGroup.objects.all()