views.py 6.12 KB
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
from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, SequenceSerializer, UserSerializer
from common.models import PyrosUser, Sequence, Album, Plan, UserLevel, SP_Period_User
from routine_manager.functions import check_sequence_file_validity
from rest_framework.request import Request

# 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):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = PyrosUser.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [IsAuthenticated]
    http_method_names = ["get"]

    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)
class SequenceViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to view their sequences.
    """
    queryset = Sequence.objects.all().order_by("-updated")
    serializer_class = SequenceSerializer
    permission_classes = [IsAuthenticated]
    http_method_names = ["get"]
    def get_queryset(self):
        """
        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
        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 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")
        
@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)