restapi/app/routes/answer.py
def view_detail():
	"""
	"	view all ratings and comments of user with question id
	"""
	try:
		current_user = get_jwt_identity()
		user = db.session.query(User).filter(User.email==func.binary(current_user)).first()
		if user is None:
			return response_error(MESSAGE.USER_INVALID_EMAIL, CODE.USER_INVALID_EMAIL)

		review_type = request.args.get('type', '')
		question_id = request.args.get('question_id', -1)
		object_id = request.args.get('id', -1)

		if question_id == -1 or \
			object_id == -1:
			return response_error(MESSAGE.ANSWER_INVALID_QUESTION_ID, CODE.ANSWER_INVALID_QUESTION_ID)

		result = answer_bl.is_valid_object_id(review_type, object_id)
		if result is None:
			return response_error(MESSAGE.ANSWER_INVALID_INPUT, CODE.ANSWER_INVALID_INPUT)
		
		rt = db.session.query(ReviewType).filter(ReviewType.name==func.binary(review_type)).first()
		response = {}

		# get all ratings
		r = db.session.query(Question.name, Question.id, func.avg(Rating.point).label('average'))\
						.filter(and_(Rating.object_id==object_id, Rating.question_id.in_(db.session.query(Question.id).filter(Question.type_id==rt.id)))) \
						.filter(Question.id==question_id) \
						.filter(Question.id==Rating.question_id) \
						.group_by(Question.name, Question.id) \
						.first()

		data = {
			"id": r.id,
			"name": r.name,
			"average": r.average
		}
		comments = db.session.query(Comment).filter(Comment.rating_id.in_(db.session.query(Rating.id).filter(Rating.question_id==r.id, Rating.object_id==object_id))).all()
		tmp = []
		for c in comments:
			cjson = c.to_json()
			if c.rating.user is not None:
				if user.role_id == 1:
					cjson['user'] = c.rating.user.to_json()	
				else:
					ujson = {
						'name': hashlib.md5('{}{}{}'.format(c.rating.user.email, g.PASSPHASE, object_id)).hexdigest()[:6]
					}
					cjson['user'] = ujson
			else:
				cjson['user'] = None
			cjson['point'] = c.rating.point
			tmp.append(cjson)
			
		data['comments'] = tmp
		response['ratings'] = data
		response['reviewed_object'] = result.to_json()

		return response_ok(response)
	except Exception, ex:
		return response_error(ex.message)

	"	view all ratings and comments of user with question id
	
"       view all ratings and comments of user with question id
" view all ratings and comments of user with question id
def view_detail():
	try:
		current_user = get_jwt_identity()
		user = db.session.query(User).filter(User.email==func.binary(current_user)).first()
		if user is None:
			return response_error(MESSAGE.USER_INVALID_EMAIL, CODE.USER_INVALID_EMAIL)
		review_type = request.args.get('type', '')
		question_id = request.args.get('question_id', -1)
		object_id = request.args.get('id', -1)
		if question_id == -1 or \
			object_id == -1:
			return response_error(MESSAGE.ANSWER_INVALID_QUESTION_ID, CODE.ANSWER_INVALID_QUESTION_ID)
		result = answer_bl.is_valid_object_id(review_type, object_id)
		if result is None:
			return response_error(MESSAGE.ANSWER_INVALID_INPUT, CODE.ANSWER_INVALID_INPUT)
		rt = db.session.query(ReviewType).filter(ReviewType.name==func.binary(review_type)).first()
		response = {}
		r = db.session.query(Question.name, Question.id, func.avg(Rating.point).label('average'))\
						.filter(and_(Rating.object_id==object_id, Rating.question_id.in_(db.session.query(Question.id).filter(Question.type_id==rt.id)))) \
						.filter(Question.id==question_id) \
						.filter(Question.id==Rating.question_id) \
						.group_by(Question.name, Question.id) \
						.first()
		data = {
			"id": r.id,
			"name": r.name,
			"average": r.average
		}
		comments = db.session.query(Comment).filter(Comment.rating_id.in_(db.session.query(Rating.id).filter(Rating.question_id==r.id, Rating.object_id==object_id))).all()
		tmp = []
		for c in comments:
			cjson = c.to_json()
			if c.rating.user is not None:
				if user.role_id == 1:
					cjson['user'] = c.rating.user.to_json()	
				else:
					ujson = {
						'name': hashlib.md5('{}{}{}'.format(c.rating.user.email, g.PASSPHASE, object_id)).hexdigest()[:6]
					}
					cjson['user'] = ujson
			else:
				cjson['user'] = None
			cjson['point'] = c.rating.point
			tmp.append(cjson)
		data['comments'] = tmp
		response['ratings'] = data
		response['reviewed_object'] = result.to_json()
		return response_ok(response)
	except Exception, ex:
		return response_error(ex.message)
def view_detail ( ) : try : current_user = get_jwt_identity ( ) user = db . session . query ( User ) . filter ( User . email == func . binary ( current_user ) ) . first ( ) if user is None : return response_error ( MESSAGE . USER_INVALID_EMAIL , CODE . USER_INVALID_EMAIL ) review_type = request . args . get ( 'type' , '' ) question_id = request . args . get ( 'question_id' , - 1 ) object_id = request . args . get ( 'id' , - 1 ) if question_id == - 1 or object_id == - 1 : return response_error ( MESSAGE . ANSWER_INVALID_QUESTION_ID , CODE . ANSWER_INVALID_QUESTION_ID ) result = answer_bl . is_valid_object_id ( review_type , object_id ) if result is None : return response_error ( MESSAGE . ANSWER_INVALID_INPUT , CODE . ANSWER_INVALID_INPUT ) rt = db . session . query ( ReviewType ) . filter ( ReviewType . name == func . binary ( review_type ) ) . first ( ) response = { } r = db . session . query ( Question . name , Question . id , func . avg ( Rating . point ) . label ( 'average' ) ) . filter ( and_ ( Rating . object_id == object_id , Rating . question_id . in_ ( db . session . query ( Question . id ) . filter ( Question . type_id == rt . id ) ) ) ) . filter ( Question . id == question_id ) . filter ( Question . id == Rating . question_id ) . group_by ( Question . name , Question . id ) . first ( ) data = { "id" : r . id , "name" : r . name , "average" : r . average } comments = db . session . query ( Comment ) . filter ( Comment . rating_id . in_ ( db . session . query ( Rating . id ) . filter ( Rating . question_id == r . id , Rating . object_id == object_id ) ) ) . all ( ) tmp = [ ] for c in comments : cjson = c . to_json ( ) if c . rating . user is not None : if user . role_id == 1 : cjson [ 'user' ] = c . rating . user . to_json ( ) else : ujson = { 'name' : hashlib . md5 ( '{}{}{}' . format ( c . rating . user . email , g . PASSPHASE , object_id ) ) . hexdigest ( ) [ : 6 ] } cjson [ 'user' ] = ujson else : cjson [ 'user' ] = None cjson [ 'point' ] = c . rating . point tmp . append ( cjson ) data [ 'comments' ] = tmp response [ 'ratings' ] = data response [ 'reviewed_object' ] = result . to_json ( ) return response_ok ( response ) except Exception , ex : return response_error ( ex . message )
"       view all ratings and comments of user with question id
" view all ratings and comments of user with question id
"""
	"	view all ratings and comments of user with question id
	""" # get all ratings