def login_user(request): try: user_name = request.data.get('username') user_pass = request.data.get('password') user = User.objects.get(username=user_name) if user: isPasswordCorrect = user.check_password(user_pass) if isPasswordCorrect: token = generate_token(user) return Response( { 'username': user_name, 'email': user.email, 'token': token, '_rest': str(user.id) + "," + make_user_hash(user.id) }, status=status.HTTP_200_OK) else: return Response( { 'errors': 'Incorrect credentials. Password is incorrect.' }, status=status.HTTP_400_BAD_REQUEST) except User.DoesNotExist as e: print e.args return Response( {'errors': 'Incorrect credentials. Username is incorrect.'}, status=status.HTTP_400_BAD_REQUEST)
def login_user(request): try: user_name = request.data.get('username') user_pass = request.data.get('password') user = User.objects.get(username = user_name) if user: isPasswordCorrect = user.check_password(user_pass) if isPasswordCorrect: token = generate_token(user) return Response({'username':user_name,'email': user.email,'token':token, '_rest': str(user.id)+","+make_user_hash(user.id) }, status = status.HTTP_200_OK) else: return Response({'errors':'Incorrect credentials. Password is incorrect.'}, status = status.HTTP_400_BAD_REQUEST) except User.DoesNotExist as e: print e.args return Response({'errors':'Incorrect credentials. Username is incorrect.'}, status = status.HTTP_400_BAD_REQUEST)
def test_user_data(request): data = { 'test': {} } if request.method == 'GET': logger.info('home.test_user_data GET call') test_user_id = request.query_params.get('test_user_id', None) token = request.query_params.get('token', None) if test_user_id and token: test_user = TestUser.objects.get(id = test_user_id) data['status'] = 'SUCCESS' data['username'] = test_user.user.username data['test_key'] = test_user.test_key data['token'] = token data['testUser'] = test_user.id data['test'].update(test_data_helper(test_user)) return Response(data, status = status.HTTP_200_OK) else: logger.error('home.test_user_data GET call error - Unable to get test details') return Response({'errors': 'Unable to get test details.'}, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'POST': '''Save data of test user if new >> then create new obj. , If found in DB then reuse it''' name = request.data.get('username') email = request.data.get('email') test_key = request.data.get('test_key') logger.info('home.test_user_data POST call') try: quiz = Quiz.objects.get(quiz_key = test_key) # If test is not public then check private access of user. if not quiz.allow_public_access: try: # Is user invited check. invited_user = InvitedUser.objects.get(user_name = name, user_email = email) if not invited_user.check_if_invited(quiz.id)[0]: logger.error('home.test_user_data POST call - Unable to access this test') return Response({'status':'NOT-ALLOW', 'errors': 'Unable to access this test.'}, status=status.HTTP_400_BAD_REQUEST) except InvitedUser.DoesNotExist as e: # User can't access this test. logger.error('home.test_user_data POST call - Unable to access this test '+str(e.args)) return Response({'status':'NOT-ALLOW', 'errors': 'Unable to access this test.'}, status=status.HTTP_400_BAD_REQUEST) user = User.objects.get(username = name, email = email) create = False except User.DoesNotExist as e: try: logger.info('home.test_user_data POST call - User.DoesNotExist') user = User.objects.create_user(username = name, email = email, password = name[::-1]+email[::-1]) create = True except Exception as e: logger.error('home.test_user_data POST call - Unable to create user '+str(e.args)) return Response({'status':'FAIL', 'errors': 'Unable to create user.'}, status=status.HTTP_400_BAD_REQUEST) except Quiz.DoesNotExist as e: logger.error('home.test_user_data POST call - Unable to find this test '+str(e.args)) return Response({'status':'FAIL', 'errors': 'Unable to find this test.'}, status=status.HTTP_400_BAD_REQUEST) serializer = TestUserSerializer(data = {'user': user.id, 'test_key' : test_key}) if serializer.is_valid(): data['status'] = 'SUCCESS' data['username'] = name data['test_key'] = test_key is_new = True if create: test_user = serializer.save() else: try: test_user = TestUser.objects.get(user = user, test_key = test_key) is_new = False test_user.save() except TestUser.DoesNotExist as e: logger.error('home.test_user_data POST call '+str(e.args)) test_user = serializer.save() if not test_user.no_attempt < quiz.no_of_attempt: logger.info('home.test_user_data POST call - NOT_REMAINING') return Response({'status':'SUCCESS', 'test':{'status':'NOT_REMAINING'}, 'errors': 'There are no remaining attempts left for this test.'}, status = status.HTTP_400_BAD_REQUEST) else: data['test'].update({'remaining_attempts':quiz.no_of_attempt - test_user.no_attempt }) token = generate_token(user) data['token'] = token data['is_new'] = is_new data['testUser'] = test_user.id data['test'].update(test_data_helper(test_user)) data['test'].update({'testURL':TEST_URL_THIRD_PARTY.format(quiz_key = test_key, test_user_id = test_user.id, token = token)}) return Response(data, status = status.HTTP_200_OK) else: logger.error('home.test_user_data POST call '+str(serializer.errors)) return Response({'status':'FAIL', 'errors': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
def test_user_data(request): data = {'test': {}} if request.method == 'GET': logger.info('home.test_user_data GET call') test_user_id = request.query_params.get('test_user_id', None) token = request.query_params.get('token', None) if test_user_id and token: test_user = TestUser.objects.get(id=test_user_id) data['status'] = 'SUCCESS' data['username'] = test_user.user.username data['test_key'] = test_user.test_key data['token'] = token data['testUser'] = test_user.id data['test'].update(test_data_helper(test_user)) return Response(data, status=status.HTTP_200_OK) else: logger.error( 'home.test_user_data GET call error - Unable to get test details' ) return Response({'errors': 'Unable to get test details.'}, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'POST': '''Save data of test user if new >> then create new obj. , If found in DB then reuse it''' name = request.data.get('username') email = request.data.get('email') test_key = request.data.get('test_key') logger.info('home.test_user_data POST call') try: quiz = Quiz.objects.get(quiz_key=test_key) # If test is not public then check private access of user. if not quiz.allow_public_access: try: # Is user invited check. invited_user = InvitedUser.objects.get(user_name=name, user_email=email) if not invited_user.check_if_invited(quiz.id)[0]: logger.error( 'home.test_user_data POST call - Unable to access this test' ) return Response( { 'status': 'NOT-ALLOW', 'errors': 'Unable to access this test.' }, status=status.HTTP_400_BAD_REQUEST) except InvitedUser.DoesNotExist as e: # User can't access this test. logger.error( 'home.test_user_data POST call - Unable to access this test ' + str(e.args)) return Response( { 'status': 'NOT-ALLOW', 'errors': 'Unable to access this test.' }, status=status.HTTP_400_BAD_REQUEST) user = User.objects.get(username=name, email=email) create = False except User.DoesNotExist as e: try: logger.info( 'home.test_user_data POST call - User.DoesNotExist') user = User.objects.create_user(username=name, email=email, password=name[::-1] + email[::-1]) create = True except Exception as e: logger.error( 'home.test_user_data POST call - Unable to create user ' + str(e.args)) return Response( { 'status': 'FAIL', 'errors': 'Unable to create user.' }, status=status.HTTP_400_BAD_REQUEST) except Quiz.DoesNotExist as e: logger.error( 'home.test_user_data POST call - Unable to find this test ' + str(e.args)) return Response( { 'status': 'FAIL', 'errors': 'Unable to find this test.' }, status=status.HTTP_400_BAD_REQUEST) serializer = TestUserSerializer(data={ 'user': user.id, 'test_key': test_key }) if serializer.is_valid(): data['status'] = 'SUCCESS' data['username'] = name data['test_key'] = test_key is_new = True if create: test_user = serializer.save() else: try: test_user = TestUser.objects.get(user=user, test_key=test_key) is_new = False test_user.save() except TestUser.DoesNotExist as e: logger.error('home.test_user_data POST call ' + str(e.args)) test_user = serializer.save() if not test_user.no_attempt < quiz.no_of_attempt: logger.info('home.test_user_data POST call - NOT_REMAINING') return Response( { 'status': 'SUCCESS', 'test': { 'status': 'NOT_REMAINING' }, 'errors': 'There are no remaining attempts left for this test.' }, status=status.HTTP_400_BAD_REQUEST) else: data['test'].update({ 'remaining_attempts': quiz.no_of_attempt - test_user.no_attempt }) token = generate_token(user) data['token'] = token data['is_new'] = is_new data['testUser'] = test_user.id data['test'].update(test_data_helper(test_user)) data['test'].update({ 'testURL': TEST_URL_THIRD_PARTY.format(quiz_key=test_key, test_user_id=test_user.id, token=token) }) return Response(data, status=status.HTTP_200_OK) else: logger.error('home.test_user_data POST call ' + str(serializer.errors)) return Response({ 'status': 'FAIL', 'errors': serializer.errors }, status=status.HTTP_400_BAD_REQUEST)