def get_user_by_name(key, value): key = str(key).lower() jsonDict = {} got_user = None if key == 'id': try: got_user = User.get(User.id == value) jsonDict = got_user.to_dict() return json.dumps(got_user.to_dict()) except DoesNotExist: return Response(status=404) elif key == 'username': try: got_user = User.get(User.username == value) jsonDict = got_user.to_dict() return json.dumps(got_user.to_dict()) except DoesNotExist: return Response(status=404) return Response(status=404)
def update_answer(answerid): answer = QuestionAnswer.get(QuestionAnswer.id == answerid) answer.text = content['newanswertext'] user = User.get(User.id == answer.user) qtext = QuestionText.get(QuestionText.id == answer.questiontextid) quiz_creator = User.get(User.username == quiz.username) # this will check the new answer if quiz.username != user.username: for x in QuestionAnswer.select().where( (QuestionAnswer.user_id == quiz_creator) & (QuestionAnswer.isRight == True)): if x.to_dict()['text'].lower() == content['newanswertext']: answer.isRight = True break else: # the question creator and the answer are the same thust they may change what they want. if 'isright' in content.keys(): if content['isright'] == True: answer.isRight = True elif content['isright'] == False: answer.isRight = False return json.dumps(answer.to_dict())
def create_quiz(): ''' basic creation of quizes publish is a new field so it is checked for before being changed. ''' content = format_json(request.get_json()) user = None try: user = User.get(User.id == content['userid']) except: return Response(status=404) # checking database for the existance of the quiz the user is trying to create. if len( list(Quiz.select().where( Quiz.userid == user, Quiz.quizname.contains( content['quizname'])))) > 0: return Response(status=216) new_quiz = Quiz() new_quiz.quizname = content['quizname'] new_quiz.userid = content['userid'] if "published" in content.keys(): new_quiz.published = content["published"] try: new_quiz.save() except IntegrityError: return Response(status=409) return json.dumps(new_quiz.to_dict())
def get_by_id(id): try: got_user = User.get(User.id == id) return json.dumps(got_user.to_dict()) except: return Response(status=404)
def delete_user(id=None, key=None, value=None): content = format_json(request.get_json()) keys = ['email', 'username'] if id != None: try: got_user = User.get(User.id == id) except DoesNotExist: return Response(status=409) if got_user.Password == content['password']: try: got_user.delete_instance() return Response(status=200) except IntegrityError: return Response(status=409) elif str(key).lower() in keys: try: got_user = None if str(key).lower() == 'email': got_user = User.get(User.Email == value) elif str(key).lower() == 'username': got_user = User.get(User.Username == value) except DoesNotExist: return Response(status=409) if got_user.Password == content['password']: try: got_user.delete_instance() return Response(status=200) except IntegrityError: return Response(status=409) else: return Response(status=203) return Response(status=404)
def create_questionanswer(): content = format_json(request.get_json()) # this is the answe that is getting modified answer = QuestionAnswer() # this finds the user sending the request and the information in the request user = User.get(User.id == content['userid']) qtext = QuestionText.get(QuestionText.id == content['questiontextid']) quiz = Quiz.get(Quiz.id == qtext.quizid) if quiz.userid != user.id: quiz_creator = User.get(User.id == quiz.userid) # this check needs to be here so # the user may not be able to set their answer to true. # this will set their answer to right or not. # providing instant feedback for x in QuestionAnswer.select().where( (QuestionAnswer.user_id == quiz_creator) & (QuestionAnswer.isRight == True)): if x.to_dict()['text'].lower() == content['answertext']: answer.isRight = True break else: # the question creator and the answer are the same thust they may change what they want. if 'isright' in content.keys(): if content['isright'] == True: answer.isRight = True elif content['isright'] == False: answer.isRight = False answer.text = content['answertext'] answer.user = user answer.questiontextid = qtext answer.save() return json.dumps(answer.to_dict())
def create_user(): content = format_json(request.get_json()) new_user = User() new_user.username = content.get("username") new_user.password = content.get("password") new_user.logged_in = True try: new_user.save() except: return Response(status=409) return json.dumps(new_user.to_dict())
def inject_answers(): content = request.get_json() # get quiz try: quiz = Quiz.get(Quiz.id == content['id']) except: return Response(status=404) # get user try: user = User.get(User.id == content['userid']) except: return Response(status=404) # not the owner and quiz is published for question in content['questions']: # get the questions try: qt = QuestionText.get(QuestionText.id == question['id']) except: return Response(status=414) for answer in question['answers']: # if the answers does not have an id then make a full answer user_answers = [ x for x in QuestionAnswer.select().where( QuestionAnswer.user == user, QuestionAnswer.questiontextid == qt) ] if 'id' not in answer.keys() and len(user_answers) == 0: try: qa = QuestionAnswer(text=answer['text'], user=user, questiontextid=qt) qa.save() return_answer = qa.to_dict() except: return Response(status=418) elif 'id' not in answer.keys(): try: qa = user_answers[0] qa.text = answer['text'] qa.save() return_answer = qa.to_dict() except: return Response(status=444) return json.dumps(get_full_quizdb(quiz.id))
def logout_user(): # this is not safe at all I know it will be improved if time allows content = format_json(request.get_json()) try: if "id" in content.keys(): got_user = User.get(User.id == str(content['id'])) elif "username" in content.keys(): got_user = User.get(User.username.contains((content['username']))) if got_user.logged_in == True: got_user.logged_in = False got_user.save() return json.dumps(got_user.to_dict()) else: return Response(status=409) except DoesNotExist: return Response(status=404) return Response(status=404)
def login_user(): # this is not safe at all I know it will be improved if time allows content = format_json(request.get_json()) try: got_user = User.get(User.username.contains(str(content['username']))) if got_user.password == content[ 'password'] and got_user.logged_in != True: got_user.logged_in = True got_user.save() return json.dumps(got_user.to_dict()) else: return Response(status=226) except: return Response(status=404)
def update_user(id): # this will probably need changed for ease of use content = format_json(request.get_json()) if "newpassword" in content.keys() and "password" in content.keys(): try: update_user = User.get(User.id == id) if update_user.Password == content['password']: update_user.Password = content['newpassword'] # this save may need a try/catch update_user.save() return json.dumps({'id': id}) else: return Response(status=418) except DoesNotExist: return Response(status=404) return Response(status=404)
def mass_assignment(): content = format_json(request.get_json()) try: user = User.get(User.id == content['userid']) except: return Response(status=404) quiz = None return_quiz = [] # if the id is not in the json create the quiz this should not be the case the quiz will typically be gotten if 'id' not in content.keys(): pass # create the quiz name in the database try: query = Quiz.select().where(Quiz.quizname == content['quizname'], Quiz.userid == user) query.execute() query = list(query) if len(query) == 0: quiz = Quiz(quizname=content['quizname'], userid=user) quiz.save() else: quiz = query[0] except: return Response(status=416) else: try: quiz = Quiz.get(Quiz.id == content['id']) if quiz.quizname != content['quizname']: quiz.quizname = content['quizname'] try: quiz.save() except: return Response(status=403) except: return Response(status=404) if quiz.published == False and int(user.id) == int(quiz.userid.id): # if the quiz is not published and the user is not the quiz maker # first items will be deleted query = QuestionText.select().where(QuestionText.quizid == quiz.id) query.execute() all_questions = list(query) request_questions = [x['text'] for x in content['questions']] remove_this = [] for quest in all_questions: if quest.text not in request_questions: remove_this.append(quest) for quest in remove_this: try: QuestionAnswer.delete().where( QuestionAnswer.questiontextid == quest.id).execute() except: return Response(status=433) try: QuestionText.delete().where( QuestionText.id == quest.id).execute() except: return Response(status=423) for question in content['questions']: qt = None if user.id == quiz.userid.id: # if they are they make change the questions # then created if 'id' not in question.keys(): try: query = QuestionText.select().where( QuestionText.text == question['text'], QuestionText.quizid == quiz) query.execute() query = list(query) if len(query) == 0: qt = QuestionText(text=question['text'], quizid=quiz) qt.save() else: qt = query[0] except: return Response(status=417) else: try: qt = QuestionText.get( QuestionText.id == question['id']) qt.text = question['text'] qt.save() except: return Response(status=404) else: # if the user.id is not quiz.id then the question exists try: qt = QuestionText.get(QuestionText.id == question['id']) except: return Response(status=404) # anyone may make changes to their answers for answer in question['answers']: qa = None query = [] if user.id != quiz.userid.id: query = QuestionAnswer.select().where( QuestionAnswer.user == user, QuestionAnswer.questiontextid == qt) query.execute() query = list(query) if len(query) == 0: if 'id' not in answer.keys(): try: qa = QuestionAnswer(text=answer['text'], user=user, questiontextid=qt) qa.save() except: return Response(status=418) else: try: qa = QuestionAnswer.get( QuestionAnswer.id == answer['id']) qa.text = answer['text'] qa.save() except: return Response(status=404) return Response(json.dumps(get_full_quizdb(quiz.id)))
def get_all_users(): query = User.select() query.execute() query = list(query) return json.dumps([x.to_dict() for x in query])