Пример #1
0
def login(request):
    """
    Login api for user
    """
    resp_data = {}
    valid_data, data = get_request_content("login_api", request, None, ["email", "password"])

    if valid_data:
        try:
            user = User.objects.get(email=data['email'])
        except:
            logger.error("User not found: %s" %data["email"])
        else:
            # check password
            if (user.check_password(data["password"])):
                logger.debug("User successfullly logged in: %s" %data["email"])
                resp_data = fetch_smoker_analytics(user=user)

                if resp_data:
                    return Response(data=resp_data, status=status.HTTP_200_OK)
                else:
                    return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)

            # wrong password
            else:
                return Response(status=status.HTTP_401_UNAUTHORIZED)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
def get_stats(request):
    """
    Returns smoke analytics for individual smoker
    """
    resp_data = {}
    valid_data, data = get_request_content("get_stats", request, None, ["token"])

    if valid_data:
        try:
            token = data["token"]
            (user, smoker) = get_user_smoker_from_token(token)
        except:
            logger.exception("Smoker not found: %s" %token)
        else:
            resp_data = fetch_smoker_analytics(user=user)
            logger.debug("stats: %s" %resp_data)
            return Response(status=status.HTTP_200_OK, data=resp_data)
    else:

        return Response(status=status.HTTP_400_BAD_REQUEST)