Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
    def post(self, request, subject, format=None):
        # todo
        data = request.data
	model = Question()
        serializer = QuestionPostSerializer(data=data,many=True)
        if not serializer.is_valid():
            err = {}
            err['msg'] = serializer.errors
            return Response(err)
	li=[]
	for i in data:
            obj1 = Content.objects.get(entry=subject)
    	    if obj1:
                obj = Question()
                obj.que = i['que']
                obj.subject = subject
                obj.difficulty = i['difficulty']
                obj.ref = obj1
                obj.save()
		li.append(obj)
	con = QuestionSerializer(li,many=True)
        return Response(con.data)
Ejemplo n.º 4
0
  def add_question():
    try:
      # formData = Question(request.get_json().get('question'),request.get_json().get('answer'),request.get_json().get('category'),request.get_json().get('difficulty'))
      formData = Question('', '', 0, 0)
      formData.question = request.get_json().get('question')
      formData.category = request.get_json().get('category')
      formData.answer = request.get_json().get('answer')
      formData.difficulty = request.get_json().get('difficulty')
      #print(formData.question)

      if(formData.question and formData.answer and formData.category and formData.difficulty):
        Question.insert(formData)
        return jsonify({
          'success':True
        })  
      else:
         return jsonify({
          'success':False,
          'message':'Check All required Fields'

        })
    except Exception as e:
      print(e)
      abort(422)
Ejemplo n.º 5
0
    def post(self):
		# Grab album from url
		urlstring = self.request.POST['album']
		album_key = ndb.Key(urlsafe=urlstring)
		album = album_key.get()
		# Check whether we're storing a album or a Question
		if self.request.GET['album'] == '1':
			album.title = self.request.POST['albumTitle']
			album.category = self.request.POST['categoryTitle']
			album.put()
			time.sleep(0.1)
			# Save album and redirect to edit if the user clicks on 'Save and continue editing'
			# Else, save album and go back to the main page which lists all albums
			if self.request.POST.get('stay') == '1':
				self.redirect('/edit?album=1&id='+urlstring)
			else:
				if album.album_type == 'match':
					self.redirect('/match')
				elif album.album_type == 'correlate':
					self.redirect('/correlate')
				else:
					self.redirect('/oddmanout')
		else:
			# Create Question with the album as parent for strong consistency
			question = ""
			new = "1"
			if self.request.POST['question'] == "":
				question = Question(parent=album_key)
				question.question_id = question.put().id()
			else:
				new = "0"
				question_url = self.request.POST['question']
				question_key = ndb.Key(urlsafe=question_url)
				question = question_key.get()
			question.title = self.request.get('title')
			question.fact = self.request.get('fact')
			question.effect = self.request.get('revealEffect')
			question.difficulty = self.request.get('difficulty')
			# Create answer choices
			answer = int(self.request.get('correct_answer'))
			input_images = self.request.get('image', allow_multiple=True)
			num_images = 4
			if album.album_type == 'correlate':
				num_images = 5
			image_list = []
			for i in range(num_images):
				img = ""
				input_img = input_images[i]
				# If old retrieve the Image
				if new == "0":
					img = question.images[i]
				else:
					img = Image()
				# Resize image
				if input_img:
					op_img = images.Image(input_img)
					op_img.resize(width=256, height=256, crop_to_fit=True)
					result_img = op_img.execute_transforms(output_encoding=images.JPEG)
					img.image = result_img
				# Set the title and correct fields
				if answer == i:
					img.title = "correct_answer_"+str(i)
					img.correct = True
				elif num_images == 5 and i == 0: # This is if the album is of type Correlate
					img.title = "main_image_"+str(i)
					img.correct = False
				else:
					img.title = "incorrect_answer_"+str(i)
					img.correct = False
			 	# If old Question, free up the old Image and put in new Image 
				if new == "0":
					question.images.pop(i)
					question.images.insert(i, img)
				else:
					question.images.append(img)

			question.put()
			# Query all Question(s) for the album in recently added order for /create
			# Retrieve previously input values, and indicate whether this is a new album (edit)
			questions = Question.query(ancestor=album_key).order(-Question.date).fetch()
			retrieve = 1
			edit = self.request.GET['edit']
			template_values = {
				'album': album,
				'album_type': album.album_type,
				'questions': questions,
				'edit': edit,
				'retrieve': retrieve
			}
			template = JINJA_ENVIRONMENT.get_template('create.html')
			self.response.write(template.render(template_values))
Ejemplo n.º 6
0
    def post(self):
        # Grab album from url
        urlstring = self.request.POST['album']
        album_key = ndb.Key(urlsafe=urlstring)
        album = album_key.get()
        # Check whether we're storing a album or a Question
        if self.request.GET['album'] == '1':
            album.title = self.request.POST['albumTitle']
            album.category = self.request.POST['categoryTitle']
            album.put()
            time.sleep(0.1)
            # Save album and redirect to edit if the user clicks on 'Save and continue editing'
            # Else, save album and go back to the main page which lists all albums
            if self.request.POST.get('stay') == '1':
                self.redirect('/edit?album=1&id=' + urlstring)
            else:
                if album.album_type == 'match':
                    self.redirect('/match')
                elif album.album_type == 'correlate':
                    self.redirect('/correlate')
                else:
                    self.redirect('/oddmanout')
        else:
            # Create Question with the album as parent for strong consistency
            question = ""
            new = "1"
            if self.request.POST['question'] == "":
                question = Question(parent=album_key)
                question.question_id = question.put().id()
            else:
                new = "0"
                question_url = self.request.POST['question']
                question_key = ndb.Key(urlsafe=question_url)
                question = question_key.get()
            question.title = self.request.get('title')
            question.fact = self.request.get('fact')
            question.effect = self.request.get('revealEffect')
            question.difficulty = self.request.get('difficulty')
            # Create answer choices
            answer = int(self.request.get('correct_answer'))
            input_images = self.request.get('image', allow_multiple=True)
            num_images = 4
            if album.album_type == 'correlate':
                num_images = 5
            image_list = []
            for i in range(num_images):
                img = ""
                input_img = input_images[i]
                # If old retrieve the Image
                if new == "0":
                    img = question.images[i]
                else:
                    img = Image()
                # Resize image
                if input_img:
                    op_img = images.Image(input_img)
                    op_img.resize(width=256, height=256, crop_to_fit=True)
                    result_img = op_img.execute_transforms(
                        output_encoding=images.JPEG)
                    img.image = result_img
                # Set the title and correct fields
                if answer == i:
                    img.title = "correct_answer_" + str(i)
                    img.correct = True
                elif num_images == 5 and i == 0:  # This is if the album is of type Correlate
                    img.title = "main_image_" + str(i)
                    img.correct = False
                else:
                    img.title = "incorrect_answer_" + str(i)
                    img.correct = False
            # If old Question, free up the old Image and put in new Image
                if new == "0":
                    question.images.pop(i)
                    question.images.insert(i, img)
                else:
                    question.images.append(img)

            question.put()
            # Query all Question(s) for the album in recently added order for /create
            # Retrieve previously input values, and indicate whether this is a new album (edit)
            questions = Question.query(
                ancestor=album_key).order(-Question.date).fetch()
            retrieve = 1
            edit = self.request.GET['edit']
            template_values = {
                'album': album,
                'album_type': album.album_type,
                'questions': questions,
                'edit': edit,
                'retrieve': retrieve
            }
            template = JINJA_ENVIRONMENT.get_template('create.html')
            self.response.write(template.render(template_values))