def sign_up(request): print("SIGNUP") print(request) email = request.data.get('email', None) password = request.data.get('password', None) # TODO: Make this param validation decorator if not email or not password: res = {'error': 'please provide a email and a password'} return api_response_data(status.HTTP_405_METHOD_NOT_ALLOWED, res) user = user_manager.create( email=email, password=password, ) return api_response_data(status.HTTP_200_OK, {'user': model_to_dict(user)})
def log_in(request): email = request.data.get('email', None) password = request.data.get('password', None) if not email or not password: res = {'error': 'please provide a email and a password'} return api_response_data(status.HTTP_405_METHOD_NOT_ALLOWED, res) is_authenticate, user = user_manager.verify( email=email, password=password, ) if not is_authenticate: return api_response_data(status.HTTP_403_FORBIDDEN, {'error': 'Not authenticated'}) # user = user_manager.get(email) user_details = user_manager.log_in(user, request) return api_response_data(status.HTTP_200_OK, {'user_details': user_details})
def qa_main(request, **kwargs): function_mappings = { 'POST': create_qa, 'GET': get_poll_qas, } if request.method in function_mappings: return function_mappings[request.method](request, **kwargs) return api_response_data(status.HTTP_404_NOT_FOUND, {'err': 'Not supported request method'})
def poll_main(request): function_mappings = { 'POST': create_poll, 'GET': get_user_polls, } if request.method in function_mappings: return function_mappings[request.method](request) return api_response_data(status.HTTP_404_NOT_FOUND, {'err': 'Not supported request method'})
def create_qa(request, **kwargs): params_dict = copy.deepcopy(request.data) params_dict['poll_id'] = kwargs['pk'] new_question, correct_ans_l, wrong_ans_l = qa_manager.create(**params_dict) correct_ans_l = [ model_to_dict(x) for x in correct_ans_l ] # TODO: Delegate this model_to_dict logic to api_response_data wrong_ans_l = [model_to_dict(x) for x in wrong_ans_l] return api_response_data( status.HTTP_200_OK, { 'new_question': model_to_dict(new_question), 'correct_ans_l': correct_ans_l, 'wrong_ans_l': wrong_ans_l })
def get_poll_qas(request, **kwargs): poll_id = kwargs['pk'] questions = qa_manager.get_for_poll(poll_id) questions = [model_to_dict(x) for x in questions] return api_response_data(status.HTTP_200_OK, {'questions': questions})
def get_user_polls(request): polls = poll_manager.get_for_user(creator_id=request.data['creator_id']) polls = [model_to_dict(x) for x in polls] return api_response_data(status.HTTP_200_OK, {'polls': polls})
def create_poll(request): poll = poll_manager.create(**request.data) return api_response_data(status.HTTP_200_OK, {'poll': model_to_dict(poll)})
def reply(request, **kwargs): params_dict = copy.deepcopy(request.data) params_dict['question_id'] = kwargs['pk'] reply, is_created = qa_manager.reply_question(**params_dict) return api_response_data(status.HTTP_200_OK, {'reply': model_to_dict(reply), 'is_created': is_created})