/
views.py
36 lines (30 loc) · 1.12 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from .models import Task
from .serializers import TaskSerializer
from rest_framework import generics
from .utils import get_auth0_user_id_from_request
# Lists and Creates entries of Task.
class TaskList(generics.ListCreateAPIView):
"""
Lists and creates tasks.
"""
queryset = Task.objects.all()
serializer_class = TaskSerializer
def perform_create(self, serializer):
auth0_user_id = get_auth0_user_id_from_request(self.request)
# Set the user to the one in the token.
serializer.save(created_by=auth0_user_id)
def get_queryset(self):
"""
This view should return a list of all Tasks
for the currently authenticated user.
"""
auth0_user_id = get_auth0_user_id_from_request(self.request)
return Task.objects.filter(created_by=auth0_user_id)
# Return a single Task and allows its update and deletion.
class TaskDetail(generics.RetrieveUpdateDestroyAPIView):
"""
Returns a single Task and allows updates and deletion of a Task.
"""
queryset = Task.objects.all()
serializer_class = TaskSerializer
lookup_url_kwarg = 'task_id'