Example #1
0
def add_user(
    name,
    email,
    username='',
    avatar='',
    bio='',
    is_admin=False,
):

    profile = Profile(name=name, bio=bio, username=username)
    profile.avatar = avatar or profile.set_avatar(email)

    user = User(password='******')
    user.email = email
    user.is_admin = is_admin
    user.profile = profile

    user.save()
    return user
Example #2
0
def register_user():
    post_data = request.get_json()

    if not post_data:
        return bad_request("No input data provided")

    try:
        data = AuthSchema(partial=True).load(post_data)
    except ValidationError as err:
        return error_response(422, err.messages)

    name = data.get('name')
    username = data.get('username')
    email = data.get('email')
    password = data.get('password')

    # check for existing user
    user = User.query.filter(User.email == email).first()

    if user:
        return bad_request('That user already exists.')

    profile = Profile()
    profile.name = name
    profile.username = username
    profile.avatar = profile.set_avatar(email)

    user = User(password=password)
    user.email = email
    user.profile = profile

    try:
        user.save()
    except (exc.IntegrityError, ValueError):
        db.session.rollback()
        return server_error('Something went wrong, please try again.')

    response = jsonify({'token': user.encode_auth_token()})
    response.status_code = 201
    response.headers['Location'] = url_for('users.get_user', id=user.id)
    return response