Exemplo n.º 1
0
    def get(self):
        """Return json of all of the records saved in the db"""

        all_recs = Geolocations.get_all()
        all_recs_details = [r.json() for r in all_recs]

        return {"All records saved to db": all_recs_details}
Exemplo n.º 2
0
    def delete(self):
        """Delete a record from 'geolocations' table where ip=ip and url=url"""

        url = request.get_json("url").get("url")
        ip = request.get_json("ip").get("ip")

        if url and ip is None:
            ip = socket.gethostbyname(url)

        record = Geolocations.find_by_ip_url(ip, url)

        if not record:
            return {
                "message":
                "Item with the ip={} and url={} not found in the db.".format(
                    ip, url)
            }, 404

        try:
            record.delete_from_db()
            db.session.commit()
            return {
                "message":
                "Item with the ip={} and url={} deleted successfully!.".format(
                    ip, url)
            }, 200
        except:
            db.session.rollback()
            return {
                "message": "Something went wrong! Deleting from the db failed!"
            }, 500
        finally:
            db.session.close()
Exemplo n.º 3
0
    def get(self, ip):
        """Return a record from 'geolocations' table where ip=ip"""

        geo_from_db = Geolocations.find_by_ip(ip)
        if not geo_from_db:
            return {'message': 'Record with ip {} not found in the db.'.format(ip)}, 404

        return {'IP: {}'. format(ip): "{}".format(geo_from_db.json())}, 200
    def test_delete_success(self):
        with self.app_context(), self.app() as client:
            new_geo = Geolocations(id=1,
                                   ip="134.201.250.155",
                                   url=None,
                                   ipv_type="ipv4",
                                   continent_code="NA",
                                   continent_name="North America",
                                   country_code="US",
                                   country_name="United States",
                                   region_code="CA",
                                   region_name="California",
                                   city="Los Angeles",
                                   zip="90012",
                                   latitude="34.0655517578125",
                                   longitude="-118.240539550781")
            new_geo.add_to_session()
            db.session.commit()

            response = client.delete(f"{URL}/geolocation",
                                     headers={
                                         "Content-Type":
                                         "application/json",
                                         "Authorization":
                                         f"Bearer {self.access_token}"
                                     },
                                     data=json.dumps({"ip":
                                                      "134.201.250.155"}))

            expected = {
                "message":
                "Item with the ip=134.201.250.155 and url=None deleted successfully!."
            }

            self.assertDictEqual(
                json.loads(response.data), expected,
                "Message returned after request successfully deleted a record by its id incorrect."
            )
            self.assertEqual(
                response.status_code, 200,
                "Status code returned after request successfully deleted a record by its id incorrect."
            )
Exemplo n.º 5
0
def create_add_to_session(location_info):
    """
    Check if IP already exists in db,
    if not then instantiate a Geolocation object with location_info as params and add it to session.
    """

    geolocation_check = Geolocations.find_by_ip(location_info.get('ip'))
    if geolocation_check:
        return {
            "message": "IP already exists in db.",
            "IP data": geolocation_check.json()
        }

    new_geolocation = Geolocations(
        ip=location_info.get('ip'),
        ipv_type=location_info.get('type'),
        continent_code=location_info.get('continent_code'),
        continent_name=location_info.get('continent_name'),
        country_code=location_info.get('country_code'),
        country_name=location_info.get('country_name'),
        region_code=location_info.get('region_code'),
        region_name=location_info.get('region_name'),
        city=location_info.get('city'),
        zip=location_info.get('zip'),
        latitude=location_info.get('latitude'),
        longitude=location_info.get('longitude')
    )

    new_geolocation.add_to_session()

    return {
               "message": "Success!",
               "IP data": new_geolocation.json()
           }
    def test_get_all_from_db_success_not_empty(self):
        with self.app_context(), self.app() as client:
            new_geo = Geolocations(ip="127.0.0.1")
            new_geo.add_to_session()
            db.session.commit()
            response = client.get(
                f"{URL}/geolocation",
                headers={"Authorization": f"Bearer {self.access_token}"})

            expected = {
                "All records saved to db": [{
                    "id": 1,
                    "ip": "127.0.0.1",
                    "url": None,
                    "ipv_type": None,
                    "continent_code": None,
                    "continent_name": None,
                    "country_code": None,
                    "country_name": None,
                    "region_code": None,
                    "region_name": None,
                    "city": None,
                    "zip": None,
                    "latitude": None,
                    "longitude": None
                }]
            }

            self.maxDiff = None

            self.assertDictEqual(
                json.loads(response.data), expected,
                "Message returned when when showing all records from not empty db is incorrect."
            )
            self.assertEqual(
                response.status_code, 200,
                "Status code returned when showing all records from not empty db is incorrect."
            )
Exemplo n.º 7
0
    def delete(self, ip):
        """Delete a record from 'geolocations' table where ip=ip"""

        record = Geolocations.find_by_ip(ip)

        if not record:
            return {"message": "Item with the ip {} not found in the db.".format(ip)}, 404

        try:
            record.delete_from_db()
            db.session.commit()
            return {"message": "Item with the ip {} deleted successfully!.".format(ip)}, 200
        except:
            db.session.rollback()
            return {"message": "Something went wrong! Deleting from the db failed!"}, 500
        finally:
            db.session.close()
Exemplo n.º 8
0
    def save_to_db(self, location_info):
        """Instantiate a Geolocation object with location_info as params and save it to db."""

        geolocation_check = Geolocations.find_by_ip_url(
            self.ip_of_interest, self.url_of_interest)
        if geolocation_check:
            return {
                "message": "IP and URL combination already exists in db.",
                "IP data": geolocation_check.json()
            }, 200

        new_geolocation = Geolocations(
            ip=location_info.get('ip'),
            url=self.url_of_interest,
            ipv_type=location_info.get('type'),
            continent_code=location_info.get('continent_code'),
            continent_name=location_info.get('continent_name'),
            country_code=location_info.get('country_code'),
            country_name=location_info.get('country_name'),
            region_code=location_info.get('region_code'),
            region_name=location_info.get('region_name'),
            city=location_info.get('city'),
            zip=location_info.get('zip'),
            latitude=location_info.get('latitude'),
            longitude=location_info.get('longitude'))

        new_geolocation.add_to_session()

        try:
            db.session.commit()
            return {
                "message": "Successfully saved to db",
                "IP data": new_geolocation.json()
            }, 201
        except:
            db.session.rollback()
            return {
                "message": "Something went wrong! Saving to db failed!",
                "IP data": new_geolocation.json()
            }, 500
        finally:
            db.session.close()