views.py 2.74 KB
Newer Older
1
2
3
"""
go/views.py

David Haynes's avatar
David Haynes committed
4
5
6
The functions that handle a request to a given URL. Get some data, manipulate
it, and return a rendered template.
"""
David Haynes's avatar
David Haynes committed
7
from django.shortcuts import get_object_or_404, redirect
8
from rest_framework import viewsets, permissions
David Haynes's avatar
David Haynes committed
9
10
11
12
13
from rest_framework.authentication import TokenAuthentication, SessionAuthentication
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated
14
15
16
17
from rest_framework.authtoken.views import ObtainAuthToken

from .serializers import URLSerializer
from .models import URL
David Haynes's avatar
David Haynes committed
18

19

David Haynes's avatar
David Haynes committed
20
class URLPermission(permissions.BasePermission):
David Haynes's avatar
David Haynes committed
21
22
    """Custom permission check on URL model operations."""

David Haynes's avatar
David Haynes committed
23
    message = "You do not have the necessary approvals to perform that action."
David Haynes's avatar
David Haynes committed
24

David Haynes's avatar
David Haynes committed
25
    def has_permission(self, request, view):
26
        return True
27

David Haynes's avatar
David Haynes committed
28
    def has_object_permission(self, request, view, obj):
29
        return obj.owner == request.user.registereduser
30

David Haynes's avatar
David Haynes committed
31

David Haynes's avatar
David Haynes committed
32
class URLViewSet(viewsets.ModelViewSet):
33
    """
David Haynes's avatar
David Haynes committed
34
    API endpoint that handles creation/read/update/deletion of URL objects.
35
    """
David Haynes's avatar
David Haynes committed
36

37
    authentication_classes = (TokenAuthentication, SessionAuthentication)
David Haynes's avatar
David Haynes committed
38
    serializer_class = URLSerializer
David Haynes's avatar
David Haynes committed
39
40
    permission_classes = (URLPermission, IsAuthenticated)
    lookup_field = "short"
David Haynes's avatar
David Haynes committed
41
42

    def get_queryset(self):
43
        return URL.objects.filter(owner=self.request.user.registereduser)
David Haynes's avatar
David Haynes committed
44
45
46

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user.registereduser)
David Haynes's avatar
David Haynes committed
47
48
49
50
51
52
53
54
55
56


class CustomAuthToken(ObtainAuthToken):
    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        token, created = Token.objects.get_or_create(user=request.user)
        return Response({"token": token.key})

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

class GetSessionInfo(APIView):
    """Handy endpoint to return current user session status & information to the frontend."""

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        token, created = Token.objects.get_or_create(user=request.user)
        session_info = {
            "username": request.user.username,
            # "full_name": f"{request.user.get_full_name}",
            "last_login": request.user.last_login,
            "is_authenticated": request.user.is_authenticated,
            "token": token.key,
        }
        return Response(session_info)
David Haynes's avatar
David Haynes committed
74
75
76
77
78
79
80
81
82
83


def redirection(request, short):
    """
    This view redirects a user based on the short URL they requested.
    """
    # Get the URL object that relates to the requested Go link
    url = get_object_or_404(URL, short__iexact=short)

    return redirect(url.destination)