def process_item(self, item, spider): session = self.Session() question = Question( question_number=item.get('question_number', ''), question=item.get('question', ''), possible_answers=item.get('possible_answers', ''), correct_answer=item.get('correct_answer', ''), explanation=item.get('explanation', ''), section=item.get('section', ''), type_of_quiz=item.get('type_of_quiz', ''), ) existing_query = session.query(Question).filter( Question.question_number == question.question_number).first() if existing_query: question = existing_query question.question_number = item.get('question_number', '') question.question = item.get('question', '') question.possible_answers = item.get('possible_answers', '') question.correct_answer = item.get('correct_answer', '') question.explanation = item.get('explanation', '') question.section = item.get('section', '') question.type_of_quiz = item.get('type_of_quiz', '') else: session.add(question) session.commit() session.close() return item
def create_test_backup(request): test_obj = json.loads(request.body) test = Test() #import pdb; pdb.set_trace() if request.user.is_authenticated(): owner = User_Profile.objects.filter(user=request.user) test.owner = owner[0] test.test_name = test_obj['PRE_TEST']['test_name'] #test.subject = test_obj['PRE_TEST'].subject #test.target_exam = test_obj['PRE_TEST'].target_exam #test.topics = test_obj['PRE_TEST'].topics_included test.total_time = test_obj['PRE_TEST']['total_time'] test.pass_criteria = test_obj['PRE_TEST']['pass_criteria'] test.assoicated_class = Class.objects.get(pk=test_obj['CLASS_INFO']) test.save() try: for item in test_obj['QUESTIONS']: question = Question() question.question_text = item['question_text'] question.explanation = item['explanation'] question.options = json.dumps(item['options']) question.hint = item['hint'] question.difficulty = item['difficulty_level'] question.points = item['points'] question.owner = owner[0] #question.target_exam = test.target_exam #question.subject = test.subject #question.topic = item.topic question.save() test.questions.add(question) data = {"status": "success"} return JsonResponse(data) except Exception, e: raise e
def create_test_backup(request): test_obj = json.loads(request.body) test = Test() #import pdb; pdb.set_trace() if request.user.is_authenticated(): owner = User_Profile.objects.filter(user = request.user) test.owner = owner[0] test.test_name = test_obj['PRE_TEST']['test_name'] #test.subject = test_obj['PRE_TEST'].subject #test.target_exam = test_obj['PRE_TEST'].target_exam #test.topics = test_obj['PRE_TEST'].topics_included test.total_time = test_obj['PRE_TEST']['total_time'] test.pass_criteria = test_obj['PRE_TEST']['pass_criteria'] test.assoicated_class = Class.objects.get(pk=test_obj['CLASS_INFO']) test.save() try: for item in test_obj['QUESTIONS']: question = Question() question.question_text = item['question_text'] question.explanation = item['explanation'] question.options = json.dumps(item['options']) question.hint = item['hint'] question.difficulty = item['difficulty_level'] question.points = item['points'] question.owner = owner[0] #question.target_exam = test.target_exam #question.subject = test.subject #question.topic = item.topic question.save() test.questions.add(question) data = {"status" : "success"} return JsonResponse(data) except Exception, e: raise e