def get_user_data(username: str) -> List[Tuple[Address, OsmPoint]]: data_with_coordinate = [] with create_session() as session: user_record = session.query(User).filter(User.username == username).first() user_addresses = ( session.query(UsersData) .filter(UsersData.user_id == user_record.id) .all() ) for item in user_addresses: data = ( session.query(AddressToPoints) .filter(AddressToPoints.id == item.id) .first() ) coordinates = ( session.query(AddressToCoordinates) .filter(AddressToCoordinates.address_id == item.id) .first() ) data_with_coordinate.append( ( Address(data.street, data.number, data.city, ''), OsmPoint(coordinates.latitude, coordinates.longitude), ) ) return data_with_coordinate
def add_user(username: str, password: str) -> Union[None, str]: try: with create_session() as session: session.add(User(username, password)) return username except IntegrityError: return None
def get_address(latitude: str, longitude: str) -> Union[Address, None]: """Получаем адрес по координатам""" try: latitude_f = float(latitude) longitude_f = float(longitude) except TypeError: return None with create_session() as session: item = ( session.query(AddressToCoordinates) .filter(AddressToCoordinates.latitude == latitude_f) .filter(AddressToCoordinates.longitude == longitude_f) .first() ) if item is None: return None address_id = getattr(item, 'address_id') address = ( session.query(AddressToPoints) .filter(AddressToPoints.id == address_id) .first() ) city = getattr(address, 'city') street = getattr(address, 'street') number = getattr(address, 'number') return Address(street, number, city, '')
def add_data_to_user(username: str, address: Address) -> bool: with create_session() as session: user_record = session.query(User).filter(User.username == username).first() address_record = ( session.query(AddressToPoints) .filter(AddressToPoints.street == address.street) .filter(AddressToPoints.number == address.number) .first() ) if address_record is None: return False session.add(UsersData(user_record.id, address_record.id)) return True
def prepare_db(parser: OsmParser) -> None: """Наполняем базу данных""" with create_session() as session: with open(str(parser.path), encoding='utf-8') as f: current_city = 'Екатеринбург' for string in f: string = string.lstrip()[:-1] parser.buffer.append(string) if re.match(r'<node id="', string) is not None: point_data = parser.extract_coordinates_osm(string) if point_data is not None: session.add( PointToCoordinate( point_data.link, point_data.latitude, point_data.longitude, ) ) elif re.match(r'<tag k="addr:street" v="', string) is not None: address_data = parser.extract_address_osm(string, current_city) if address_data is not None: session.add( AddressToPoints( address_data.city, address_data.street, address_data.number, address_data.links, ) ) if len(parser.buffer) == 40: parser.buffer.pop(0) session.commit() for item in session.query(AddressToPoints).all(): coordinates_data = DbWorker.prepare_coordinates( item.city, item.street, item.number, session ) point = find_centroid(coordinates_data, len(coordinates_data)) session.add( AddressToCoordinates(item.id, point.latitude, point.longitude) )
def get_coordinates(city: str, street: str, number: str) -> Union[OsmPoint, None]: """Получаем координаты по адресу""" with create_session() as session: item = ( session.query(AddressToPoints) .filter(AddressToPoints.city == city) .filter(AddressToPoints.street == street) .filter(AddressToPoints.number == number) .first() ) if item is None: return None address_id = getattr(item, 'id') coordinates = ( session.query(AddressToCoordinates) .filter(AddressToCoordinates.address_id == address_id) .first() ) latitude = getattr(coordinates, 'latitude') longitude = getattr(coordinates, 'longitude') return OsmPoint(latitude, longitude)
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 test_prepare_db(): with create_session() as session: data = session.query(AddressToCoordinates).all() assert data is not None assert len(data) > 0
def get_password(username: str) -> Union[str, None]: with create_session() as session: user = session.query(User).filter(User.username == username).first() if user is None: return None return getattr(user, 'password',)