def questionsIDRoute(id): data = toDict( request.data ) # toDict takes the request data and converts it to a dictionary success = False # assume the response is unsucessful message = "" # assume an empty message status = "" # accepted statues: 'OK', 'DENIED', 'FAILURE', 'WARNING', 'INVALID' response = {} # assume the response is empty dict() for now if request.method == 'PUT': try: vote_status = data['vote_status'] Votes.setVote(user_id=data['loggedin_id'], comment_id=id, comment_status=data['comment_status'], vote_status=vote_status) vote = Votes.getVote(user_id=data['loggedin_id'], comment_id=id, comment_status=data['comment_status']) response = json.dumps({ 'success': True, 'status': 'OK', 'message': 'Vote is set.' }) except KeyError: response = json.dumps({ 'success': False, 'status': 'FAILURE', 'message': 'Not proper keys.' }) return response
def getQuestionsByBoth(engineer, user_id, loggedin_id): from application.models import Users from application.models import Votes response = [] if loggedin_id < 0: # means user is not logged in print("NOT LOGGED IN") questions = Question.query.filter_by(engineer=engineer, user_id=user_id).all() if questions is not None: for question in questions: user = Users.User.query.filter_by(id=question.user_id).first() ques = dict(question) if user is None: ques['user'] = {} else: ques['user'] = dict(user) if loggedin_id == -1: ques['vote_status'] = {'vote_status': 0} else: try: ques['vote_status'] = Votes.getVote( loggedin_id, ques['id'], 'question')['vote_status'] except KeyError: ques['vote_status'] = {'vote_status': 0} response.append(ques) return response
def getQuestion(id, loggedin_id=-1): question = Question.query.filter_by(id=id).first() if question is None: return None else: question = dict(question) question['vote_status'] = Votes.getVote(loggedin_id, question['id'], 'question')['vote_status'] return question
def getAnswersByQuestion(question_id, loggedin_id=-1): response = [] answers = Answer.query.filter_by(question_id=question_id).all() if answers is not None: for answer in answers: ans = dict(answer) if loggedin_id == -1: ans['vote_status'] = {'vote_status': 0} else: try: ans['vote_status'] = Votes.getVote(loggedin_id, ans['id'], 'answer')['vote_status'] except KeyError: ans['vote_status'] = {'vote_status': 0} response.append(dict(ans)) return response