Esempio n. 1
0
def POST_users(request, format):
    """ Respond to the "POST users" request.

        We create a new user and log them in.
    """
    # Get the request parameters.

    params = apiHelper.get_params(request, resource_name="user")

    error = apiHelper.check_params(params,
                                   required_params=[],
                                   optional_params=["username", "password"])
    if error != None: return error

    username = params.get("username")
    password = params.get("password")

    if username == "": username = None
    if password == "": password = None

    # Check that the given username doesn't already exist.

    if username != None:
        if User.objects.filter(username=username).exists():
            return HttpResponseBadRequest("Duplicate User")

    # Create a new User object for this user.

    user = User()
    user.username = username
    if password != None:
        user.set_password(password)
    user.created_at = datetime.datetime.now()
    user.updated_at = datetime.datetime.now()
    user.save()

    # Open up a session for this new user.

    token = session.create(user)

    # Finally, return the newly-created user and the login token back to the
    # caller.

    return apiHelper.response({'user'  : user.to_dict(),
                               'token' : token},
                              format=format, status=HTTP_RESPONSE_POST_OK)
Esempio n. 2
0
def POST_sessions(request, format):
    """ Respond to a "POST sessions" request.
    """
    params = apiHelper.get_params(request, "session")

    if "username" in params:
        error = apiHelper.check_params(params,
                                       required_params=["username",
                                                        "password"])
        login_via_username = True
    elif "number" in params:
        error = apiHelper.check_params(params,
                                       required_params=["number"],
                                       optional_params=["country"])
        login_via_username = False
    else:
        return HttpResponseBadRequest("username/password or " +
                                      "phone number required")

    if error != None: return error

    if login_via_username:

        # Log the caller in using the supplied username and password.

        username = params['username']
        password = params['password']

        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = None

        if user == None or not user.check_password(password):
            return HttpResponseForbidden("Incorrect username or password")

        # Open up a session for this user.

        token = session.create(user)

        # Finally, return the user and the login token back to the caller.

        return apiHelper.response({'user'  : user.to_dict(),
                                   'token' : token},
                                  format=format, status=HTTP_RESPONSE_POST_OK)

    else:

        # Log the caller in using the supplied phone number.

        number = params['number']

        if "country" in params:
            country = params['country']
        else:
            country = "US"

        # Ask the "phonenumbers" library to format the phone number into E.164
        # format.

        phone_number     = phonenumbers.parse(number, country)
        phone_format     = phonenumbers.PhoneNumberFormat.E164
        formatted_number = phonenumbers.format_number(phone_number, phone_format)

        # Open up a session for this phone number.

        token,verification_code = session.create_for_phone(formatted_number)

        # Send an SMS message to the supplied phone number.

        sms_gateway.send_message(text="code: " + verification_code +
                                      " -- " + settings.SYSTEM_NAME,
                                 from_user=None,
                                 from_profile=None,
                                 to_user=None,
                                 to_profile=None,
                                 to_phone_number=formatted_number,
                                 message_id=None)

        # Finally, return the formatted phone number and login token back to
        # the caller.

        return apiHelper.response({'number'  : formatted_number,
                                   'token'   : token},
                                  format=format, status=HTTP_RESPONSE_POST_OK)