Blame view

src/core/pyros_django/api/views.py 6.12 KB
98621b46   Alexis Koralewski   add DRF, pyros ap...
1
2
3
4
5
6
7
8
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.decorators import api_view, permission_classes
from django.core.validators import ValidationError
from src.core.pyros_django.user_manager import views as user_views
c2589779   Alexis Koralewski   Adding API views ...
9
10
from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, SequenceSerializer, UserSerializer
from common.models import PyrosUser, Sequence, Album, Plan, UserLevel, SP_Period_User
98621b46   Alexis Koralewski   add DRF, pyros ap...
11
from routine_manager.functions import check_sequence_file_validity
c2589779   Alexis Koralewski   Adding API views ...
12
13
from rest_framework.request import Request

98621b46   Alexis Koralewski   add DRF, pyros ap...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Create your views here.


class Users(APIView):

    permission_classes = (IsAuthenticated,)  

    def get(self, request):
        content = {"message" : "Hello"}
        return Response(content)

    @api_view(["GET"])
    def user_logout(request):

        request.user.auth_token.delete()

        user_views.logout()

        return Response('User Logged out successfully')


class UserViewSet(viewsets.ModelViewSet):
    """
c2589779   Alexis Koralewski   Adding API views ...
37
    API endpoint that allows users to be viewed.
98621b46   Alexis Koralewski   add DRF, pyros ap...
38
39
40
41
    """
    queryset = PyrosUser.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [IsAuthenticated]
42bf33a2   Alexis Koralewski   renaming configpy...
42
    http_method_names = ["get"]
98621b46   Alexis Koralewski   add DRF, pyros ap...
43

c2589779   Alexis Koralewski   Adding API views ...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    def list(self, request):
        serializer_context = {
            'request': request,
        }
        queryset = None
        current_user = self.request.user
        user = self.request.user
        user_role = str(UserLevel.objects.get(priority=user.get_priority()).name)
        if user_role in ("Unit-PI","Unit-board","Admin"):
            queryset = PyrosUser.objects.all().order_by("-created")
        else:
            sp_of_current_user = user.get_scientific_program()
            pyros_users_with_roles = []
            for sp in sp_of_current_user:
                for sp_period in sp.SP_Periods.all():
                    for user in SP_Period_User.objects.filter(SP_Period=sp_period).exclude(user=current_user).values_list("user",flat=True):
                        pyros_users_with_roles.append(PyrosUser.objects.get(id=user))
                    pyros_users_with_roles.append(sp_period.scientific_program.sp_pi)
            admin_and_unit_users = PyrosUser.objects.filter(user_level__name__in=("Unit-PI","Unit-board","Admin")).distinct()
            queryset = pyros_users_with_roles + list(admin_and_unit_users)
        serializer = UserSerializer(queryset, context = serializer_context, many=True)
        return Response(serializer.data)
98621b46   Alexis Koralewski   add DRF, pyros ap...
66
67
class SequenceViewSet(viewsets.ModelViewSet):
    """
c2589779   Alexis Koralewski   Adding API views ...
68
    API endpoint that allows users to view their sequences.
98621b46   Alexis Koralewski   add DRF, pyros ap...
69
    """
c2589779   Alexis Koralewski   Adding API views ...
70
    queryset = Sequence.objects.all().order_by("-updated")
98621b46   Alexis Koralewski   add DRF, pyros ap...
71
72
    serializer_class = SequenceSerializer
    permission_classes = [IsAuthenticated]
42bf33a2   Alexis Koralewski   renaming configpy...
73
    http_method_names = ["get"]
98621b46   Alexis Koralewski   add DRF, pyros ap...
74
75
    def get_queryset(self):
        """
c2589779   Alexis Koralewski   Adding API views ...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        This view should return a list of all the sequences
        for the currently authenticated user.
        """
        user = self.request.user
        user_role = str(UserLevel.objects.get(priority=user.get_priority()).name)
        if user_role in ("Unit-PI","Unit-board","Admin"):
            return Sequence.objects.all().order_by("-updated")
        else:
            return Sequence.objects.filter(pyros_user=user).order_by("-updated")


class FullSequenceViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to view their sequences.
    """
    queryset = Sequence.objects.all().order_by("-updated")
    serializer_class = FullSequenceSerializer
    permission_classes = [IsAuthenticated]
    http_method_names = ["get"]
    def get_queryset(self):
        """
        This view should return a list of all the sequences
98621b46   Alexis Koralewski   add DRF, pyros ap...
98
99
100
        for the currently authenticated user.
        """
        user = self.request.user
c2589779   Alexis Koralewski   Adding API views ...
101
102
103
104
105
        user_role = str(UserLevel.objects.get(priority=user.get_priority()).name)
        if user_role in ("Unit-PI","Unit-board","Admin"):
            return Sequence.objects.all().order_by("-updated")
        else:
            return Sequence.objects.filter(pyros_user=user).order_by("-updated")
98621b46   Alexis Koralewski   add DRF, pyros ap...
106

c2589779   Alexis Koralewski   Adding API views ...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

class AlbumViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to view their sequences.
    """
    queryset = Album.objects.all().order_by("-updated")
    serializer_class = AlbumSerializer
    permission_classes = [IsAuthenticated]
    http_method_names = ["get"]
    def get_queryset(self):
        """
        This view should return a list of all the albums
        for the currently authenticated user.
        """
        user = self.request.user
        user_role = str(UserLevel.objects.get(priority=user.get_priority()).name)
        if user_role in ("Unit-PI","Unit-board","Admin"):
            sequences = Sequence.objects.all().order_by("-updated")
        else:
            sequences = Sequence.objects.filter(pyros_user=user).order_by("-updated")
        return Album.objects.filter(sequence__in=sequences).order_by("-updated")


class PlanViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to view their sequences.
    """
    queryset = Plan.objects.all().order_by("-updated")
    serializer_class = PlanSerializer
    permission_classes = [IsAuthenticated]
    http_method_names = ["get"]
    def get_queryset(self):
        """
        This view should return a list of all the plans
        for the currently authenticated user.
        """
        user = self.request.user
        user_role = str(UserLevel.objects.get(priority=user.get_priority()).name)
        if user_role in ("Unit-PI","Unit-board","Admin"):
            sequences = Sequence.objects.all().order_by("-updated")
        else:
            sequences = Sequence.objects.filter(pyros_user=user).order_by("-updated")
        albums = Album.objects.filter(sequence__in=sequences).order_by("-updated")
        return Plan.objects.filter(album__in=albums).order_by("-updated")
        
98621b46   Alexis Koralewski   add DRF, pyros ap...
152
153
154
155
156
157
@api_view(["PUT"])
def submit_sequence_with_json(request):
    sequence_json = request.data
    print(sequence_json)
    response = check_sequence_file_validity(sequence_json,request)
    return Response(response)