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 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 prepare_address_osm(self, links: List[str], street: str, city: str, buffer_position: int) -> Union[Address, None]: """Извлекаем номер дома и его точки """ house_number = None for i in range(buffer_position, len(self.buffer)): if re.match(r'<tag k="addr:housenumber" v=', self.buffer[i]) is not None: house_number = self.buffer[i][29:-3] continue if re.match(r'<tag k="addr:city" v=', self.buffer[i]) is not None: city = self.buffer[i][22:-3].capitalize() if re.match(r'<nd ref="', self.buffer[i]) is not None: links.append(self.buffer[i][9:-3]) if house_number is not None: address = Address(street, house_number, city, ' '.join(links)) return address return None
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
def address(): return Address('Попова', '6', 'Екатеринбург', '')
def address(): return Address('проспект Ленина', '52', 'Екатеринбург', '1 2 3')
@patch.object(DbWorker, 'get_coordinates') def test_get_address_coordinate(mock, client, point): mock.return_value = point response = client.get( '/geocoder/api/v1.0/get_coord/?city=city&street=street&number=number' ) data = json.loads(response.get_data()) assert response.status_code == 200 if point is None: assert data['Attention'] == 'This address does not exist in data base' else: assert data['Coordinates']['Latitude'] == point.latitude assert data['Coordinates']['Longitude'] == point.longitude @pytest.mark.parametrize('address', (None, Address('Ленина', '1', 'Екатеринбург', ''))) @patch.object(DbWorker, 'get_address') def test_get_coordinate_address(mock, client, address): mock.return_value = address response = client.get( '/geocoder/api/v1.0/get_addr/?latitude=latitude&longitude=longitude' ) data = json.loads(response.get_data()) assert response.status_code == 200 if address is None: assert ( data['Error'] == 'This address does not exist or you coordinate incorrect' ) else: assert data['Address']['City'] == address.city assert data['Address']['Street'] == address.street