Beispiel #1
0
def test_update(db: SQLAlchemy, first_user: User):
    db.session.add(first_user)
    db.session.commit()
    updates: UserInterface = dict(name='Eduardo Saverin')

    UserService.update(first_user, updates)

    result: User = User.query.get(first_user.user_id)
    assert result.name == 'Eduardo Saverin'
Beispiel #2
0
def test_create(db: SQLAlchemy, first_user: User, location: Location):
    u = dict(name='Bill Gates',
             email='*****@*****.**',
             password='******',
             location_id=location.location_id)
    UserService.create(u)

    results: List[User] = User.query.all()

    assert len(results) == 1

    for k in u.keys():
        if getattr(results[0], 'password'):
            continue
        assert getattr(results[0], k) == u[k]
Beispiel #3
0
def test_delete_by_id(db: SQLAlchemy, first_user: User, location: Location):
    second_user = User(user_id=2,
                       name='Bill Gates',
                       email='*****@*****.**',
                       password=bcrypt.hashpw(b'test', bcrypt.gensalt()),
                       location_id=location.location_id)

    db.session.add(first_user)
    db.session.add(second_user)
    db.session.commit()

    UserService.delete_by_id(first_user.user_id)

    results: List[User] = User.query.all()

    assert len(results) == 1
    assert first_user not in results and second_user in results
Beispiel #4
0
def user_role():
    data = request.values.to_dict()

    try:
        role = data.get('role', None)

        service = UserService()
        users = service.search_role(role)
        return jsonify({
            'status': 0,
            'message': 'ok',
            'data': users,
        })
    except Exception:
        return jsonify({
            'status': 500,
            'message': '获取用户列表失败,请联系管理员!',
            'data': data,
        })
Beispiel #5
0
def user_search():
    data = request.values.to_dict()

    try:
        key = data.get('key', None)
        value = data.get('value', None)
        department = int(data.get('department', 3))

        service = UserService()
        users = service.search_user(key, value, department)
        return jsonify({
            'status': 0,
            'message': 'ok',
            'data': users,
        })
    except Exception:
        return jsonify({
            'status': 500,
            'message': '获取用户列表失败,请联系管理员!',
            'data': data,
        })
Beispiel #6
0
def test_get_by_location(db: SQLAlchemy, first_user: User, location: Location):
    second_user = User(user_id=2,
                       name='Bill Gates',
                       email='*****@*****.**',
                       password=bcrypt.hashpw(b'test', bcrypt.gensalt()),
                       location_id=location.location_id)

    db.session.add(first_user)
    db.session.add(second_user)
    db.session.commit()

    results: List[User] = UserService.get_by_location(first_user.location_id)

    assert len(results) == 2
    assert first_user in results and second_user in results
    assert first_user.location_id == second_user.location_id
Beispiel #7
0
def get_pets():
    data = dict(request.args)
    errors = get_pets_request_schema.validate(data)
    if errors:
        raise ErrValidation(errors)
    curr_user = get_current_user()
    q = data["q"]
    page = int(data["page"])
    size = int(data["size"])
    skip = calc_skip(page, size)
    if curr_user.is_customer:
        pets = PetService.get_customer_pets(curr_user.identity, q, skip, size)
    else:
        pets = PetService.get_all(q, skip, size)
    for i, pet in enumerate(pets):
        owner = UserService.get_by_id(pet.owner_id)
        pet.owner = owner
    return send_ok(get_pets_response_schema.dump({"data": pets}))
Beispiel #8
0
from flask import Blueprint, request
from app.shared.decorators import return_json, token_required, restaurant_required
from app.user.service import Service as UserService

user_service = UserService()

user_routes = Blueprint('user_routes', __name__)

@user_routes.route('/login', methods=['POST'])
@return_json
def login_user():
    auth = request.authorization
    if not auth or not auth.username or not auth.password:
        return {
            'status': 409,
            'message': 'Missing Auth'
        }
    return user_service.login_user(auth['username'], auth['password'])

@user_routes.route('/login', methods=['GET'])
@return_json
@token_required
def login_using_token(user):
    return user_service.update_token(user)

@user_routes.route('/create', methods=['POST'])
@return_json
def create_new_user():
    data = request.get_json()
    auth = request.authorization
    if not auth or not auth.username or not auth.password:
Beispiel #9
0
 def get(self, locationId: int):
     return UserService.get_by_location(locationId)
Beispiel #10
0
 def put(self, userId: int):
     u = UserService.get_by_id(userId)
     return UserService.update(u, request.parsed_obj)
Beispiel #11
0
    def delete(self, userId: int):
        from flask import jsonify

        id = UserService.delete_by_id(userId)
        return jsonify(dict(status='Success', id=id))
Beispiel #12
0
 def get(self, userId: int) -> User:
     return UserService.get_by_id(userId)
Beispiel #13
0
 def post(self) -> User:
     return UserService.create(request.parsed_obj)
Beispiel #14
0
 def get(self) -> List[User]:
     return UserService.get_all()