views.py 6.27 KB
Newer Older
1
2
from django.contrib.auth import get_user_model
from django.db.models import Q
asmundh's avatar
asmundh committed
3
from rest_framework import mixins, generics
4
from rest_framework.parsers import MultiPartParser, FormParser
asmundh's avatar
asmundh committed
5
from rest_framework import permissions
6
7
8
9
10
from rest_framework.permissions import (
    IsAuthenticatedOrReadOnly,
)
from workouts.mixins import CreateListModelMixin
from workouts.permissions import IsOwner, IsReadOnly
asmundh's avatar
asmundh committed
11
12
13
14
15
16
17
18
19
20
from users.serializers import (
    UserSerializer,
    OfferSerializer,
    AthleteFileSerializer,
    UserPutSerializer,
    UserGetSerializer,
)
from users.models import Offer, AthleteFile
from users.permissions import IsCurrentUser, IsAthlete, IsCoach

21

asmundh's avatar
asmundh committed
22
23
24
25
26
27
28
29
30
31
32
33
34
class UserList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
    serializer_class = UserSerializer
    users = []
    admins = []

    def get(self, request, *args, **kwargs):
        self.serializer_class = UserGetSerializer
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def get_queryset(self):
35
        query_set = get_user_model().objects.all()
asmundh's avatar
asmundh committed
36
37
38
39
40

        if self.request.user:
            # Return the currently logged in user
            status = self.request.query_params.get("user", None)
            if status and status == "current":
41
                query_set = get_user_model().objects.filter(pk=self.request.user.pk)
asmundh's avatar
asmundh committed
42

43
        return query_set
asmundh's avatar
asmundh committed
44
45
46


class UserDetail(
47
48
49
50
51
52
        mixins.RetrieveModelMixin,
        mixins.UpdateModelMixin,
        mixins.DestroyModelMixin,
        generics.GenericAPIView
    ):

asmundh's avatar
asmundh committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    lookup_field_options = ["pk", "username"]
    serializer_class = UserSerializer
    queryset = get_user_model().objects.all()
    permission_classes = [permissions.IsAuthenticated & (IsCurrentUser | IsReadOnly)]

    def get_object(self):
        for field in self.lookup_field_options:
            if field in self.kwargs:
                self.lookup_field = field
                break

        return super().get_object()

    def get(self, request, *args, **kwargs):
        self.serializer_class = UserGetSerializer
        return self.retrieve(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        self.serializer_class = UserPutSerializer
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)


class OfferList(
82
83
84
85
        mixins.ListModelMixin,
        mixins.CreateModelMixin,
        generics.GenericAPIView
    ):
asmundh's avatar
asmundh committed
86
87
88
89
90
91
92
93
94
95
96
97
    permission_classes = [IsAuthenticatedOrReadOnly]
    serializer_class = OfferSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

98
99
    @staticmethod
    def filter_on_status(query_set, query_params):
100
        status = query_params.get("status", None)
101
        if status is not None:
102
103
104
            query_set = query_set.filter(status=status)
        return query_set

105
106
    @staticmethod
    def filter_on_category(query_set, query_params, user):
107
108
109
110
111
112
113
114
        category = query_params.get("category", None)
        if category is not None and query_params is not None:
            if category == "sent":
                query_set = query_set.filter(owner=user)
            elif category == "received":
                query_set = query_set.filter(recipient=user)
        return query_set

asmundh's avatar
asmundh committed
115
116
117
118
    def get_queryset(self):
        result = Offer.objects.none()

        if self.request.user:
119
            query_set = Offer.objects.filter(
asmundh's avatar
asmundh committed
120
121
                Q(owner=self.request.user) | Q(recipient=self.request.user)
            ).distinct()
122
123
            query_params = self.request.query_params
            user = self.request.user
asmundh's avatar
asmundh committed
124

125
            query_set = self.filter_on_status(query_set, query_params)
126
            query_set = self.filter_on_category(query_set, query_params, user)
127
128
129
            return query_set

        return result
asmundh's avatar
asmundh committed
130
131
132


class OfferDetail(
133
134
135
136
137
        mixins.RetrieveModelMixin,
        mixins.UpdateModelMixin,
        mixins.DestroyModelMixin,
        generics.GenericAPIView,
    ):
asmundh's avatar
asmundh committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    permission_classes = [IsAuthenticatedOrReadOnly]
    queryset = Offer.objects.all()
    serializer_class = OfferSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)


class AthleteFileList(
156
157
158
159
160
        mixins.ListModelMixin,
        mixins.CreateModelMixin,
        CreateListModelMixin,
        generics.GenericAPIView,
    ):
asmundh's avatar
asmundh committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    queryset = AthleteFile.objects.all()
    serializer_class = AthleteFileSerializer
    permission_classes = [permissions.IsAuthenticated & (IsAthlete | IsCoach)]
    parser_classes = [MultiPartParser, FormParser]

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

    def get_queryset(self):
Håvard Farestveit's avatar
Håvard Farestveit committed
176
        query_set = AthleteFile.objects.none()
asmundh's avatar
asmundh committed
177
178

        if self.request.user:
Håvard Farestveit's avatar
Håvard Farestveit committed
179
            query_set = AthleteFile.objects.filter(
asmundh's avatar
asmundh committed
180
181
182
                Q(athlete=self.request.user) | Q(owner=self.request.user)
            ).distinct()

Håvard Farestveit's avatar
Håvard Farestveit committed
183
        return query_set
asmundh's avatar
asmundh committed
184
185
186


class AthleteFileDetail(
187
188
189
190
191
        mixins.RetrieveModelMixin,
        mixins.UpdateModelMixin,
        mixins.DestroyModelMixin,
        generics.GenericAPIView,
    ):
asmundh's avatar
asmundh committed
192
193
194
195
196
197
198
199
200
    queryset = AthleteFile.objects.all()
    serializer_class = AthleteFileSerializer
    permission_classes = [permissions.IsAuthenticated & (IsAthlete | IsOwner)]

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)