|
1
|
|
|
from django.shortcuts import render |
|
2
|
|
|
|
|
3
|
|
|
from rest_framework import filters |
|
4
|
|
|
from rest_framework import viewsets |
|
5
|
|
|
from rest_framework import status |
|
6
|
|
|
from rest_framework.authentication import TokenAuthentication |
|
7
|
|
|
from rest_framework.authtoken.serializers import AuthTokenSerializer |
|
8
|
|
|
from rest_framework.authtoken.views import ObtainAuthToken |
|
9
|
|
|
from rest_framework.permissions import ( |
|
10
|
|
|
IsAuthenticated, IsAuthenticatedOrReadOnly) |
|
11
|
|
|
from rest_framework.response import Response |
|
12
|
|
|
from rest_framework.views import APIView |
|
13
|
|
|
|
|
14
|
|
|
from . import models |
|
15
|
|
|
from . import permissions |
|
16
|
|
|
from . import serializers |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class UserProfileViewSet(viewsets.ModelViewSet): |
|
20
|
|
|
"""Handles creating, reading and updating a user in logic""" |
|
21
|
|
|
|
|
22
|
|
|
serializer_class = serializers.UserProfileSerializer |
|
23
|
|
|
queryset = models.UserProfile.objects.all() |
|
24
|
|
|
authentication_classes = (TokenAuthentication, ) |
|
25
|
|
|
permission_classes = (permissions.UpdateOwnProfile, ) |
|
26
|
|
|
filter_backends = (filters.SearchFilter, ) |
|
27
|
|
|
search_fields = ('name', 'email', ) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class LoginViewSet(viewsets.ViewSet): |
|
31
|
|
|
"""Validation users""" |
|
32
|
|
|
|
|
33
|
|
|
serializer_class = AuthTokenSerializer |
|
34
|
|
|
|
|
35
|
|
|
def create(self, request): |
|
36
|
|
|
"""Create unique token for each user""" |
|
37
|
|
|
|
|
38
|
|
|
return ObtainAuthToken().post(request) |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class UserProfileFeedViewSet(viewsets.ModelViewSet): |
|
42
|
|
|
"""Handles Feed Items.""" |
|
43
|
|
|
|
|
44
|
|
|
authentication_classes = (TokenAuthentication, ) |
|
45
|
|
|
serializer_class = serializers.ProfileFeedItemSerializer |
|
46
|
|
|
queryset = models.ProfileFeedItem.objects.all() |
|
47
|
|
|
permission_classes = (permissions.PostOwnStatus, IsAuthenticated) |
|
48
|
|
|
|
|
49
|
|
|
def perform_create(self, serializer): |
|
50
|
|
|
"""Sets the user profile to the logged in user.""" |
|
51
|
|
|
|
|
52
|
|
|
serializer.save(user_profile=self.request.user) |
|
53
|
|
|
|