コード例 #1
0
def update_category(request):
    token = request.META['HTTP_AUTHORIZATION'].split(' ')
    if "name" not in request.data:
        return JsonResponse({"error": "Enter a valid category name"},
                            status=status.HTTP_400_BAD_REQUEST)
    if "newname" not in request.data:
        return JsonResponse({"error": "Enter a valid category name"},
                            status=status.HTTP_400_BAD_REQUEST)
    try:
        user = Token.objects.get(key=token[1]).user
    except Token.DoesNotExist:
        return JsonResponse({"error": "Invalid User token "},
                            status=status.HTTP_401_UNAUTHORIZED)
    categoryData = Category.objects.filter(name=request.data['name'])
    if len(categoryData) == 0:
        return JsonResponse({"error": "There is no category with this name"},
                            status=status.HTTP_401_UNAUTHORIZED)
    cat = CategorySerializer(categoryData[0], many=False).data
    if cat['user'] != user.id:
        return JsonResponse(
            {
                "error":
                "Cannot delete the category .It can only be deleted by the user who created"
            },
            status=status.HTTP_401_UNAUTHORIZED)
    cat = Category.objects.get(name=request.data['name'])
    cat.name = request.data['newname']
    cat.save()
    categoryData = Category.objects.get(name=request.data['name'])
    Question.objects.filter(category=categoryData).update(category=cat)
    UserGames.objects.filter(category=categoryData).update(category=cat)
    Game.objects.filter(category=categoryData).update(category=cat)
    Category.objects.filter(name=request.data['name']).delete()
    return JsonResponse({"category": "Category name updated sucessfully"},
                        status=status.HTTP_201_CREATED)
コード例 #2
0
def update_question(request):
    token = request.META['HTTP_AUTHORIZATION'].split(' ')
    try:
        user = Token.objects.get(key=token[1]).user
    except Token.DoesNotExist:
        return JsonResponse({"error": "Invalid token"},
                            status=status.HTTP_401_UNAUTHORIZED)
    question = request.data.get('Question')
    questionData = Question.objects.filter(id=question['id'])

    if len(questionData) == 0:
        return JsonResponse({"error": "There is no question with this id"},
                            status=status.HTTP_401_UNAUTHORIZED)
    ques = QuestionSerializer(questionData[0], many=False).data
    print(ques)
    if ques['user'] != user.id:
        return JsonResponse(
            {
                "error":
                "Cannot update the question .The questions can only be updated by the user who created"
            },
            status=status.HTTP_401_UNAUTHORIZED)

    cats = Category.objects.filter(name=question['category'])
    if len(cats) == 0:
        return JsonResponse({"error": "Enter valid category"},
                            status=status.HTTP_400_BAD_REQUEST)
    try:
        index = question['options'].index(question['answer'])
    except Exception as error:
        return JsonResponse({"error": "There is no option with the answer "},
                            status=status.HTTP_400_BAD_REQUEST)

    category = CategorySerializer(cats, many=True).data
    options = question['options']
    if len(options) != 4:
        return JsonResponse({"error": "There should be exaclty 4 options"},
                            status=status.HTTP_400_BAD_REQUEST)
    optionsList = []
    for option in options:
        optionData = Options.objects.filter(option=option)
        if len(optionData) == 0:
            option = {'option': option}
            serializer = OptionsSerializer(data=option, many=False)
            if serializer.is_valid():
                serializer.save()
        else:
            serializer = OptionsSerializer(optionData[0], many=False)
        optionsList.append(serializer.data['option'])

    questionData = Question.objects.get(id=question['id'])
    questionData.question = question['question']
    questionData.category = Category.objects.get(name=category[0]['name'])
    questionData.options.set(optionsList)
    questionData.user = user
    questionData.answer = Options.objects.get(option=optionsList[index])
    questionData.save()
    serializer = QuestionSerializer(questionData)
    return JsonResponse({"data": serializer.data}, status=status.HTTP_200_OK)
コード例 #3
0
def get_all_category(request):
    try:
        data = Category.objects.all()
        question = CategorySerializer(data, many=True).data
        return JsonResponse({"data": question}, status=status.HTTP_200_OK)
    except Exception as error:
        return JsonResponse({"error": error},
                            status=status.HTTP_400_BAD_REQUEST)
コード例 #4
0
ファイル: views.py プロジェクト: hngi/quickthink-be
def create_category(request):
    if request.method == "POST":
        if 'name' not in request.data:
            return JsonResponse({
                "error": "Enter category name"
            }, status=status.HTTP_400_BAD_REQUEST)
        token = request.META['HTTP_AUTHORIZATION'].split(' ')
        try:
            user = Token.objects.get(key=token[1]).user
        except Token.DoesNotExist:
            return JsonResponse(
                {"error": "Invalid token"}, status=status.HTTP_401_UNAUTHORIZED
            )
        data = {
            'name': request.data['name'],
            'user': user.id
        }
        if "isGeneral" in request.data:
            data['isGeneral'] = request.data['isGeneral']
            if request.data['isGeneral']:
                pc = Category.objects.filter(isGeneral=request.data['isGeneral'])
                if len(pc) != 0:
                    return JsonResponse({
                        "error": "You cannot have more than 1 general categories"
                    }, status=status.HTTP_400_BAD_REQUEST)
        if "isSubCategory" in request.data:
            data['isSubCategory'] = request.data['isSubCategory']
            if "parentCategory" in request.data:
                data['parentCategory'] = request.data['parentCategory']
                pc = Category.objects.filter(name=request.data['parentCategory'])
                if len(pc) == 0:
                    return JsonResponse({
                        "error": "Enter valid  parentCategory"
                    }, status=status.HTTP_400_BAD_REQUEST)
            else:
                return JsonResponse({
                    "error": "Enter parent category"
                }, status=status.HTTP_400_BAD_REQUEST)
        createcategory = CategorySerializer(data=data)
        if createcategory.is_valid():
            createcategory.save()
            return Response(createcategory.data, status=status.HTTP_201_CREATED)
        return Response(createcategory.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #5
0
def create_category(request):
    if request.method == "POST":
        if 'name' not in request.data:
            return JsonResponse({"error": "Enter category name"},
                                status=status.HTTP_400_BAD_REQUEST)
        token = request.META['HTTP_AUTHORIZATION'].split(' ')
        try:
            user = Token.objects.get(key=token[1]).user
        except Token.DoesNotExist:
            return JsonResponse({"error": "Invalid token"},
                                status=status.HTTP_401_UNAUTHORIZED)
        data = {'name': request.data['name'], 'user': user.id}
        createcategory = CategorySerializer(data=data)
        if createcategory.is_valid():
            createcategory.save()
            return Response(createcategory.data,
                            status=status.HTTP_201_CREATED)
        return Response(createcategory.errors,
                        status=status.HTTP_400_BAD_REQUEST)
コード例 #6
0
def create_question(request):
    token = request.META['HTTP_AUTHORIZATION'].split(' ')
    try:
        user = Token.objects.get(key=token[1]).user
    except Token.DoesNotExist:
        return JsonResponse(
            {"error": "Invalid token"}, status=status.HTTP_401_UNAUTHORIZED
        )
    question = request.data.get('Question')
    cats = Category.objects.filter(name=question['category'])
    if len(cats) == 0:
        return JsonResponse(
            {"error": "Enter valid category"}, status=status.HTTP_400_BAD_REQUEST
        )
    try:
        index = question['options'].index(question['answer'])
    except Exception as error:
        return JsonResponse(
            {"error": "There is no option with the answer "}, status=status.HTTP_400_BAD_REQUEST
        )
    category = CategorySerializer(cats, many=True).data
    options = question['options']
    if len(options) != 4:
        return JsonResponse(
            {"error": "There should be exaclty 4 options"}, status=status.HTTP_400_BAD_REQUEST
        )
    optionsList = []
    for option in options:
        optionData = Options.objects.filter(option=option)
        if len(optionData) == 0:
            option = {
                'option': option
            }
            serializer = OptionsSerializer(data=option, many=False)
            if serializer.is_valid():
                serializer.save()
        else:
            serializer = OptionsSerializer(optionData[0], many=False)
        optionsList.append(serializer.data['option'])
    question = {
        "question": question['question'],
        "category": category[0]['name'],
        "options": optionsList,
        "user": user.id,
        "answer": optionsList[index]
    }
    serializer = QuestionSerializer(data=question)
    if serializer.is_valid():
        serializer.save()
        return JsonResponse({
            "data": serializer.data
        }, status=status.HTTP_200_OK)
    return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #7
0
def get_all_category_user(request):
    try:
        token = request.META['HTTP_AUTHORIZATION'].split(' ')
        try:
            user = Token.objects.get(key=token[1]).user
        except Token.DoesNotExist:
            return JsonResponse({"error": "Invalid token"},
                                status=status.HTTP_401_UNAUTHORIZED)
        data = Category.objects.filter(user=user.id)
        question = CategorySerializer(data, many=True).data
        return JsonResponse({"data": question}, status=status.HTTP_200_OK)
    except Exception as error:
        return JsonResponse({"error": error},
                            status=status.HTTP_400_BAD_REQUEST)
コード例 #8
0
def delete_category(request):
    token = request.META['HTTP_AUTHORIZATION'].split(' ')
    if "name" not in request.data:
        return JsonResponse({"error": "Enter a valid category name"},
                            status=status.HTTP_400_BAD_REQUEST)
    try:
        user = Token.objects.get(key=token[1]).user
    except Token.DoesNotExist:
        return JsonResponse({"error": "Invalid User token "},
                            status=status.HTTP_401_UNAUTHORIZED)
    categoryData = Category.objects.filter(name=request.data['name'])
    if len(categoryData) == 0:
        return JsonResponse({"error": "There is no category with this name"},
                            status=status.HTTP_401_UNAUTHORIZED)
    cat = CategorySerializer(categoryData[0], many=False).data
    if cat['user'] != user.id:
        return JsonResponse(
            {
                "error":
                "Cannot delete the category .It can only be deleted by the user who created"
            },
            status=status.HTTP_401_UNAUTHORIZED)
    categoryData = Category.objects.get(name=request.data['name'])
    question = Question.objects.filter(category=categoryData)
    if len(question) != 0:
        return JsonResponse(
            {
                "error":
                "Cannot delete the category .Questions have refrence to this"
            },
            status=status.HTTP_401_UNAUTHORIZED)
    ug = UserGames.objects.filter(category=categoryData)
    if len(ug) != 0:
        return JsonResponse(
            {
                "error":
                "Cannot delete the category .Some players have played the game"
            },
            status=status.HTTP_401_UNAUTHORIZED)
    game = Game.objects.filter(category=categoryData)
    if len(game) != 0:
        return JsonResponse(
            {"error": "Cannot delete the category .Games are created"},
            status=status.HTTP_401_UNAUTHORIZED)
    Category.objects.filter(name=request.data['name']).delete()
    return JsonResponse({"category": "Category name deleted sucessfully"},
                        status=status.HTTP_204_NO_CONTENT)