예제 #1
0
def retrieve_survey(request, uuid):
    """
    return a single survey.
    """
    try:
        survey = Survey.objects.get(pk=uuid)
    except (Survey.DoesNotExist, ValueError):
        return Response({"error": "Survey not found"},
                        status=status.HTTP_404_NOT_FOUND)

    if survey.is_private:
        if request.user.is_authenticated:
            if request.user == survey.admin or request.user in survey.users.all(
            ):
                serializer = SurveySerializer(survey,
                                              context={'request': request})
                return Response(serializer.data)
            else:
                return Response({"error": "This is a private survey."},
                                status=status.HTTP_403_FORBIDDEN)
        else:
            return Response({"error": "Please login."},
                            status=status.HTTP_401_UNAUTHORIZED)
    else:
        serializer = SurveySerializer(survey, context={'request': request})
        return Response(serializer.data)
예제 #2
0
def create_survey(request):
    if request.user.is_authenticated:
        serializer = SurveySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(admin=request.user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response({"error": "please login"}, status.HTTP_401_UNAUTHORIZED)
예제 #3
0
def create_survey(request):
    if request.user.is_authenticated:
        serializer = SurveySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(admin=request.user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response({"error": "please login"},
                        status.HTTP_401_UNAUTHORIZED)
예제 #4
0
def create_survey(request):
    if request.method == 'POST':
        try:
            user = User.objects.get(username=request.user.username)
        except User.DoesNotExist:
            return Response({"error": "user not found"}, status.HTTP_404_NOT_FOUND)

        serializer = SurveySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #5
0
def update_survey(request, uuid):
    if request.user.is_authenticated:
        try:
            survey = Survey.objects.get(pk=uuid)
        except Survey.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.user == survey.admin:
            serializer = SurveySerializer(survey, data=request.data, context={'request': request})
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_200_OK)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response({"error": "un-authorized"}, status=status.HTTP_403_FORBIDDEN)
    else:
        return Response({"error": "user not found"}, status=status.HTTP_404_NOT_FOUND)
예제 #6
0
def update_survey(request, slug):
    """
    """
    if request.method == 'PUT':
        try:
            user = User.objects.get(username=request.user.username)
        except User.DoesNotExist:
            return Response({"error": "user not found"}, status=status.HTTP_404_NOT_FOUND)

        try:
            survey = Survey.objects.get(slug=slug)
        except Survey.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if user.is_staff or user is survey.owner or user in [sa.admin for sa in survey.surveyadmin_set.all()]:
            serializer = SurveySerializer(survey, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_200_OK)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response({"error": "un-authorized"}, status=status.HTTP_401_UNAUTHORIZED)
예제 #7
0
def update_survey(request, uuid):
    if request.user.is_authenticated:
        try:
            survey = Survey.objects.get(pk=uuid)
        except Survey.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.user == survey.admin:
            serializer = SurveySerializer(survey,
                                          data=request.data,
                                          context={'request': request})
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_200_OK)
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response({"error": "un-authorized"},
                            status=status.HTTP_403_FORBIDDEN)
    else:
        return Response({"error": "user not found"},
                        status=status.HTTP_404_NOT_FOUND)
예제 #8
0
def list_surveys(request):
    """
    return a list of surveys.
    """
    if request.user.is_authenticated:
        public_surveys = Q(is_private=False)
        private_surveys = Q(is_private=True,
                            pk__in=request.user.assessment_user_surveys.all())
        admin_surveys = Q(admin=request.user)
        surveys = Survey.objects.filter(public_surveys | private_surveys
                                        | admin_surveys)
    else:
        surveys = Survey.objects.filter(is_private=False)
    serializer = SurveySerializer(surveys,
                                  many=True,
                                  context={'request': request})
    return Response(serializer.data, status=status.HTTP_200_OK)