Пример #1
0
def user_get(id):
	if id:
		user = User.query.filter_by(site_id=g.sid, branch_id=g.bid, is_active='Y', id=id).first()
	else:
		no = db.session.query(db.func.max(User.no)).filter_by(site_id=g.sid, branch_id=g.bid).scalar() or 0
		no = no + 1
		user = User(site_id=g.sid, branch_id=g.bid)
		user.no = no
		user.code = 'C' + str(no).zfill(4)
	schema = UserSchema()
	return jsonify({
		'user': schema.dump(user).data
	})
Пример #2
0
def get_users(page=1):
    """Get list of users"""
    users = User.query.paginate(
        page,
        current_app.config['ITEMS_PER_PAGE'],
        False
    )
    next_url = url_for('users.get_users', page=users.next_num) \
        if users.has_next else None
    prev_url = url_for('users.get_users', page=users.prev_num) \
        if users.has_prev else None

    return {
        'items': UserSchema(many=True).dump(users.items),
        'next_url': next_url,
        'prev_url': prev_url
    }
Пример #3
0
def update_user(id):
    request_data = request.get_json()

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

    try:
        data = UserSchema().load(request_data)
    except ValidationError as err:
        return error_response(422, err.messages)

    try:
        user = User.find_by_id(id)
        existing_user = User.find_by_identity(data.get('username'))

        if existing_user is not None:
            print(existing_user)
            if existing_user.id != user.id:
                print(existing_user.id != user.id)
                return bad_request(f'Username already exists.')

        # update user
        user.firstname = data.get('firstname')
        user.lastname = data.get('lastname')
        user.username = data.get('username')
        user.bio = data.get('bio')
        user.is_active = data.get('is_active')
        user.is_admin = data.get('is_admin')
        user.save()

        response = jsonify({
            'message': f'Successfully updated {user.firstname}.',
        })
        response.status_code = 200

        return response

    # handle errors
    except (exc.IntegrityError, ValueError):
        db.session.rollback()
        return bad_request('Something went wrong, please try again.')
Пример #4
0
def add_user():
    request_data = request.get_json()

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

    try:
        data = UserSchema().load(request_data)
    except ValidationError as err:
        return error_response(422, err.messages)

    email = data.get('email')
    password = data.get('password')
    firstname = data.get('firstname')
    lastname = data.get('lastname')
    username = data.get('username') or \
        f'{firstname}{lastname}_{int(random()*1e6)}'

    try:
        # check for existing user
        user_email = User.find_by_identity(data.get('email'))
        user_username = User.find_by_identity(data.get('username'))

        if user_email is None and user_username is None:
            # add new user to db
            user = User()
            user.firstname = firstname
            user.lastname = lastname
            user.username = username
            user.email = email
            user.password = password
            user.bio = data.get('bio')
            user.is_active = data.get('is_active') or False
            user.is_admin = data.get('is_admin') or False

            user.save()

            response = jsonify({
                'message': 'Successfully added new user.',
            })
            response.status_code = 201
            response.headers['Location'] = url_for(
                'users.get_user', id=user.id)
            return response
        else:
            return bad_request('Sorry. That user already exists.')

    # handle errors
    except (exc.IntegrityError, ValueError):
        db.session.rollback()
        return error_response(500, 'Something went wrong, please try again.')
Пример #5
0
def get_user(id):
    """Get a single user"""
    user = User.find_by_id(id)
    if user is None:
        return not_found('User not found!')
    return jsonify(UserSchema().dump(user))
Пример #6
0
def user_get(id):
    user = User.query.filter_by(id=id).first()
    schema = UserSchema()
    return jsonify({
        'user': schema.dump(user).data
    })