def get_address_coordinate() -> Any: """Обработка запроса в формате get_coord/?city=city&street=street&number=number """ city = request.args.get('city') street = request.args.get('street') number = request.args.get('number') if city is None or street is None or number is None: return ( jsonify({ 'Error': 'Bad request, give full address with city, street and number' }), 400, ) coordinates = DbWorker.get_coordinates(city, street, number) if coordinates is None: return jsonify( {'Attention': 'This address does not exist in data base'}), 200 if coordinates.latitude == coordinates.longitude == 0: return jsonify({'Error': 'Can not calculate coordinates, sorry'}), 200 response = { 'Latitude': coordinates.latitude, 'Longitude': coordinates.longitude, } return jsonify({'Coordinates': response}), 200
def get_save_addr(username: str) -> Any: data = DbWorker.get_user_data(username) dict_data = {} for item in data: key = ( f'City: {item[0].city}, Street: {item[0].street}, Number: {item[0].number}' ) value = f'latitude: {item[1].latitude}, longitude: {item[1].longitude}' dict_data[key] = value return jsonify({'Information': 'abc'}), 200
def add_user() -> Any: if (not request.json or 'username' not in request.json or 'password' not in request.json): return ( jsonify( {'ERROR': 'Invalid data, please give username and password'}), 400, ) username = request.json.get('username') password = generate_password_hash(request.json.get('password')) if DbWorker.add_user(username, password) is None: return ( jsonify( {'Warning': 'User with the same name or password does exist'}), 400, ) return jsonify({'Successful registration': {'username': username}}), 200
def save_address(username: str) -> Any: if (not request.json or 'city' not in request.json or 'street' not in request.json or 'number' not in request.json): return ( jsonify({ 'Error': 'Invalid data, please give city street and number of address' }), 400, ) city = request.json.get('city') street = request.json.get('street') number = request.json.get('number') address = Address(street, number, city, '') if DbWorker.add_data_to_user(username, address): return jsonify({'Success': 'Address will be add to your storage'}), 200 return jsonify({'Error': 'This address does not exist id database'}), 400
from final_project.app import server from final_project.db_worker import DbWorker if __name__ == '__main__': DbWorker.create_db() server.run()
def test_get_user_data(address): DbWorker.add_user('username', 'password') DbWorker.add_data_to_user('username', address) result = DbWorker.get_user_data('username') assert type(result) is list assert result[0][0].city == address.city
def test_add_data_to_user(address): DbWorker.add_user('username', 'password') result = DbWorker.add_data_to_user('username', address) assert result is True
def test_add_user(): result = DbWorker.add_user('username', 'password') assert result == 'username'
def test_get_coordinates(address): result = DbWorker.get_coordinates(address.city, address.street, address.number) assert result.latitude is not None assert result.longitude is not None
def test_prepare_coordinates(address): with create_session() as session: result = DbWorker.prepare_coordinates(address.city, address.street, int(address.number), session) assert result[0][0] is not None assert result[0][1] is not None
def test_prepare_coordinates_bad_address(): with create_session() as session: result = DbWorker.prepare_coordinates('None', 'None', 0, session) assert result is None
def init_db(parser): Base.metadata.create_all(engine) DbWorker.prepare_db(parser) yield Base.metadata.drop_all(engine)
def get_password(username: str, password: Any) -> Union[str, bool]: user_password = DbWorker.get_password(username) if user_password is None: return False return check_password_hash(user_password, password)