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")
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")
def create_questions(): data = request.get_json() question = data.get('question') date_posted = datetime.datetime.now() new_question = Questions(question, date_posted) return jsonify({ 'message': 'success, Question created', 'question': new_question.save() }), 201
def views(request): data = {'title': 'Challenges'} if request.method == 'POST': if 'action' in request.POST: action = request.POST['action'] if action == 'create_challenge': try: with transaction.atomic(): if 'challenge_name' in request.POST and request.POST[ 'challenge_name'] != '': challenge_name = request.POST['challenge_name'] if Challenges.objects.filter( name=challenge_name).exists(): return bad_json( message="Challenge Name already exists. " "Please change the name of the challenge and try again. " ) new_code = generate_code() challenge = Challenges(name=challenge_name, code=new_code) challenge.save() return ok_json( data={ 'message': 'Good Job!. You successfully created a new Challenge.' }) else: return bad_json(message="Not Challenge received") except Exception as ex: return bad_json(error=1) if action == 'questions': challenge = Challenges.objects.get(pk=int(request.POST['id'])) question = None if 'qid' in request.POST and request.POST['qid'] != '': question = Questions.objects.get( pk=int(request.POST['qid'])) correct_answer = int(request.POST['q_answers_checks']) try: with transaction.atomic(): if not question: question = Questions( challenge=challenge, text=request.POST['q_text'], order=request.POST['q_order'], counter_time=request.POST['q_counter'], answer1=request.POST['q_answer1'], answer2=request.POST['q_answer2'], answer3=request.POST['q_answer3'], answer4=request.POST['q_answer4'], is_correct_answer1=True if correct_answer == 1 else False, is_correct_answer2=True if correct_answer == 2 else False, is_correct_answer3=True if correct_answer == 3 else False, is_correct_answer4=True if correct_answer == 4 else False) else: question.text = request.POST['q_text'] question.order = request.POST['q_order'] question.counter_time = request.POST['q_counter'] question.answer1 = request.POST['q_answer1'] question.answer2 = request.POST['q_answer2'] question.answer3 = request.POST['q_answer3'] question.answer4 = request.POST['q_answer4'] question.is_correct_answer1 = True if correct_answer == 1 else False question.is_correct_answer2 = True if correct_answer == 2 else False question.is_correct_answer3 = True if correct_answer == 3 else False question.is_correct_answer4 = True if correct_answer == 4 else False question.save() return ok_json( data={ 'redirect_url': '/challenges', 'message': 'Successfully {} a Question'.format( 'Created' if not question else 'Edited') }) except Exception: return bad_json(message="Error saving data") if action == 'delete_question': try: question = Questions.objects.get( pk=int(request.POST['qid'])) with transaction.atomic(): if question.responses_set.exists(): question.responses_set.all().delete() question.delete() return ok_json( data={ 'redirect_url': '/challenges', 'message': 'Successfully Deleted the Question.' }) except Exception: return bad_json(message="Error deleting Question") return bad_json(message="Bad Request") else: if 'action' in request.GET: if 'action' in request.GET: action = request.GET['action'] return HttpResponseRedirect('/access') return render(request, 'challenges/access.html', data)
def views(request): data = {'title': 'Challenges'} if request.method == 'POST': if 'action' in request.POST: action = request.POST['action'] if action == 'create_challenge': try: with transaction.atomic(): if 'challenge_name' in request.POST and request.POST['challenge_name'] != '': challenge_name = request.POST['challenge_name'] if Challenges.objects.filter(name=challenge_name).exists(): return bad_json(message="Challenge Name already exists. " "Please change the name of the challenge and try again. ") new_code = generate_code() challenge = Challenges(name=challenge_name, code=new_code) challenge.save() return ok_json(data={'message': 'Good Job!. You successfully created a new Challenge.'}) else: return bad_json(message="Not Challenge received") except Exception as ex: return bad_json(error=1) if action == 'questions': challenge = Challenges.objects.get(pk=int(request.POST['id'])) question = None if 'qid' in request.POST and request.POST['qid'] != '': question = Questions.objects.get(pk=int(request.POST['qid'])) correct_answer = int(request.POST['q_answers_checks']) try: with transaction.atomic(): if not question: question = Questions(challenge=challenge, text=request.POST['q_text'], order=request.POST['q_order'], answer1=request.POST['q_answer1'], answer2=request.POST['q_answer2'], answer3=request.POST['q_answer3'], answer4=request.POST['q_answer4'], is_correct_answer1=True if correct_answer == 1 else False, is_correct_answer2=True if correct_answer == 2 else False, is_correct_answer3=True if correct_answer == 3 else False, is_correct_answer4=True if correct_answer == 4 else False) else: question.text = request.POST['q_text'] question.order = request.POST['q_order'] question.counter_time = request.POST['q_counter'] question.answer1 = request.POST['q_answer1'] question.answer2 = request.POST['q_answer2'] question.answer3 = request.POST['q_answer3'] question.answer4 = request.POST['q_answer4'] question.is_correct_answer1 = True if correct_answer == 1 else False question.is_correct_answer2 = True if correct_answer == 2 else False question.is_correct_answer3 = True if correct_answer == 3 else False question.is_correct_answer4 = True if correct_answer == 4 else False question.save() return ok_json(data={'redirect_url': '/challenges', 'message': 'Successfully {} a Question'.format('Created' if not question else 'Edited')}) except Exception: return bad_json(message="Error saving data") if action == 'delete_question': try: question = Questions.objects.get(pk=int(request.POST['qid'])) with transaction.atomic(): if question.responses_set.exists(): question.responses_set.all().delete() question.delete() return ok_json(data={'redirect_url': '/challenges', 'message': 'Successfully Deleted the Question.'}) except Exception: return bad_json(message="Error deleting Question") if action == 'access_challenge': try: code = request.POST['code'] if not Challenges.objects.filter(code=code).exists(): return bad_json(message='Code does not exist in the system.') return ok_json(data={'challengeID': Challenges.objects.filter(code=code)[0].id}) except Exception: return bad_json(message='Error getting data') if action == 'response_question': question = Questions.objects.get(pk=int(request.POST['qid'])) response = int(request.POST['response']) if response == 1 and question.is_correct_answer1: return ok_json() elif response == 2 and question.is_correct_answer2: return ok_json() elif response == 3 and question.is_correct_answer3: return ok_json() elif response == 4 and question.is_correct_answer4: return ok_json() else: return bad_json(message='INCORRECT') return bad_json(message="Bad Request") else: if 'action' in request.GET: if 'action' in request.GET: action = request.GET['action'] if action == 'questions': try: data['title'] = 'New Question' data['challenge'] = Challenges.objects.get(pk=request.GET['id']) question = None if 'qid' in request.GET and request.GET['qid'] != '': question = Questions.objects.get(pk=int(request.GET['qid'])) data['question'] = question return render(request, 'challenges/questions.html', data) except Exception: pass if action == 'play': try: data['title'] = 'Play Challenge' data['challenge'] = challenge = Challenges.objects.get(pk=request.GET['id']) data['questions'] = questions = challenge.get_my_questions() data['first_question'] = questions[0] data['current_question'] = questions[0] return render(request, 'challenges/play.html', data) except Exception: pass return HttpResponseRedirect('/challenges') data['challenges'] = Challenges.objects.all() return render(request, 'challenges/view.html', data)