Exemple #1
0
def get_subcategory(request, userid, categoryid, format = None):
	"""
	If all_subcategories = True then return all else return subcategory not have any category.
	Either get all subcategories under each quiz and category or get subcategories under specifc quiz and category.
	"""
	if verify_user_hash(userid, request.query_params.get('hash')):
		logger.info('under quiz.get_subcategory')
		serializer = None
		subcategories = []
		if categoryid == 'all':
			if checkIfTrue(request.query_params.get('all_subcategories')):
				subcategories = SubCategory.objects.filter(user = userid)
			else:
				subcategories = SubCategory.objects.filter(user = userid, category = None)
			serializer = SubCategorySerializer(subcategories, many = True)
		elif categoryid.isnumeric():
			try:
				subcategories = SubCategory.objects.filter(category = categoryid, user = userid)
				serializer = SubCategorySerializer(subcategories, many = True)
			except SubCategory.DoesNotExist as e:
				logger.error('under quiz.get_subcategory '+str(e.args))
				return Response({'errors': 'Sub-categories not found'}, status = status.HTTP_404_NOT_FOUND)
		else:
			logger.error('under quiz.get_subcategory wrong hash')
			return Response({'errors': 'Wrong URL passed.'}, status = status.HTTP_404_NOT_FOUND)
		return Response(serializer.data, status = status.HTTP_200_OK) 
	logger.error('under quiz.get_subcategory wrong hash')
	return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)
Exemple #2
0
def all_questions_under_subcategory(request, user_id, subcategory_id):
	"""
	Either get all questions under specific subcategory.
	If questionFormat is True --> Then we also uses answers and options for each question in JSON.
	Otherwise, we ignore them.
	"""
	if verify_user_hash(user_id, request.query_params.get('hash')):
		logger.info('under quiz.all_questions_under_subcategory')
		try:
			if not checkIfTrue(request.query_params.get('questionFormat')):
				quizzes = get_questions_format(user_id, subcategory_id, True)
			else:
				quizzes = get_questions_format(user_id, subcategory_id)
			return Response(quizzes, status = status.HTTP_200_OK)	
		except SubCategory.DoesNotExist as e:
			logger.error('under quiz.all_questions_under_subcategory '+str(e.args))
			return Response({'errors': 'Questions not found'}, status = status.HTTP_404_NOT_FOUND)
	logger.error('under quiz.all_questions_under_subcategory wrong hash')
	return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)