Exemple #1
0
def add_question_existing(request):
    """
    Allow user to add question to an existing route to the database
    :param request:
    :return: Return a response to ajax call on whether the question has been added successfully
    """
    # Get the values that user want to add into the database
    question = request.POST.get('question')
    answer = request.POST.get('answer')
    hint = request.POST.get('hint')
    node_num = 1
    latitude = request.POST.get('latitude')
    longtitude = request.POST.get('longtitude')
    routeID = request.POST.get('routeID')
    # Make the node number of the new question to be added highest out of all the node number with the same routeID
    while Questions.objects.filter(node_num=node_num,
                                   routeID=routeID).exists():
        node_num += 1
        print(node_num)
    b = Questions()
    b.questions = question
    b.answers = answer
    b.hints = hint
    b.latitude = float(latitude)
    b.longtitude = float(longtitude)
    b.node_num = int(node_num)
    b.routeID_id = int(routeID)
    b.save()
    if Questions.objects.filter(questions=question).exists():
        return HttpResponse("Added successfully")
    else:
        return HttpResponse("Not added")
Exemple #2
0
def add_question(request):
    """
    Add questions details retrieved from the ajax call and save it to the database
    :param request:
    :return: Return a reponse to ajax call whether the question has been added successfully or not
    """
    #adding question - getting data through post request
    question = request.POST.get('question')
    answer = request.POST.get('answer')
    hint = request.POST.get('hint')
    latitude = request.POST.get('latitude')
    longtitude = request.POST.get('longtitude')
    node_num = request.POST.get('node_num')
    routeID = request.POST.get('routeID')
    routeID = striptext(routeID)
    #
    print(question, answer, hint, latitude, longtitude, node_num, routeID)
    #setting up questions object
    b = Questions()
    b.questions = question
    b.answers = answer
    b.hints = hint
    b.latitude = float(latitude)
    b.longtitude = float(longtitude)
    b.node_num = int(node_num)
    b.routeID_id = int(routeID)
    # Save instance into database
    b.save()
    #checking if the questions were added successfully or not
    if Questions.objects.filter(questions=question).exists():
        return HttpResponse("Added successfully")
    else:
        return HttpResponse("Not added")