示例#1
0
    def test_google_places_city_without_state(self, m):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        url = "https://maps.googleapis.com/maps/api/" \
              "place/details/json?placeid=%s&key=%s" % (
                  wixom_without_state_data['place_id'],
                  settings.GOOGLE_MAPS_API_SERVER)
        m.get(url, json=wixom_server_response, status_code=status.HTTP_200_OK)

        location = parse_google_places(
            wixom_without_state_data['address_components'],
            wixom_without_state_data['place_id'])

        self.assertEqual(location.name, "Wixom")
        res, _ = db.cypher_query('MATCH (a:Location '
                                 '{name: "Michigan"}) RETURN a')
        state = Location.inflate(res.one)
        res, _ = db.cypher_query(
            'MATCH (a:Location '
            '{name: "United States of America"}) RETURN a')
        country = Location.inflate(res.one)
        self.assertTrue(state in location.encompassed_by)
        self.assertTrue(location in state.encompasses)

        self.assertTrue(country in state.encompassed_by)
        self.assertTrue(state in country.encompasses)
 def populate_sectors(self):
     skip = 0
     while True:
         query = 'MATCH (location:Location) RETURN location ' \
                 'SKIP %s LIMIT 25' % skip
         skip += 24
         res, _ = db.cypher_query(query)
         if not res.one:
             break
         for location in [Location.inflate(row[0]) for row in res]:
             if not location.sector:
                 try:
                     encompassed_by = Location.nodes.get(
                         object_uuid=location.get_encompassed_by(
                             location.object_uuid)[0]).name
                 except (DoesNotExist, Location.DoesNotExist, IndexError):
                     encompassed_by = None
                 try:
                     int(location.name)
                     location.sector = "federal"
                 except ValueError:
                     if location.name == "United States of America":
                         location.sector = "federal"
                         location.save()
                         continue
                     state = us.states.lookup(location.name)
                     if state is None:
                         location.sector = "local"
                     elif encompassed_by is not None and state is not None \
                             and encompassed_by != \
                             "United States of America":
                         location.sector = "local"
                     else:
                         location.sector = "federal"
                 location.save()
示例#3
0
def update_address_location(object_uuid):
    try:
        address = Address.nodes.get(object_uuid=object_uuid)
    except (DoesNotExist, Address.DoesNotExist, CypherException, IOError,
            ClientError) as e:
        raise update_address_location.retry(exc=e,
                                            countdown=3,
                                            max_retries=None)
    try:
        state = us.states.lookup(address.state)
        district = address.congressional_district
        query = 'MATCH (a:Address {object_uuid:"%s"})-[r:ENCOMPASSED_BY]->' \
                '(l:Location) DELETE r' % object_uuid
        db.cypher_query(query)
        query = 'MATCH (s:Location {name:"%s"})-[:ENCOMPASSES]->' \
                '(d:Location {name:"%s", sector:"federal"}) RETURN d' % \
                (state, district)
        res, _ = db.cypher_query(query)
        if res.one is not None:
            district = Location.inflate(res.one)
            address.encompassed_by.connect(district)
        address.set_encompassing()
    except (CypherException, IOError, ClientError) as e:
        raise update_address_location.retry(exc=e,
                                            countdown=3,
                                            max_retries=None)
    return True
示例#4
0
 def get_location(self):
     from sb_locations.neo_models import Location
     query = 'MATCH (a:Mission {object_uuid: "%s"})' \
             '-[:WITHIN]->(b:Location) RETURN b' % self.object_uuid
     res, _ = db.cypher_query(query)
     if res.one:
         return Location.inflate(res.one)
     else:
         return None
示例#5
0
    def test_google_places_city(self):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        location = parse_google_places(wixom_data['address_components'],
                                       wixom_data['place_id'])

        self.assertEqual(location.name, "Wixom")
        res, _ = db.cypher_query('MATCH (a:Location '
                                 '{name: "Michigan"}) RETURN a')
        state = Location.inflate(res.one)
        res, _ = db.cypher_query(
            'MATCH (a:Location '
            '{name: "United States of America"}) RETURN a')
        country = Location.inflate(res.one)
        self.assertTrue(state in location.encompassed_by)
        self.assertTrue(location in state.encompasses)

        self.assertTrue(country in state.encompassed_by)
        self.assertTrue(state in country.encompasses)
示例#6
0
 def add_addresses_to_locations(self):
     for address in Address.nodes.all():
         if not address.encompassed_by.all():
             state = us.states.lookup(address.state)
             district = address.congressional_district
             query = 'MATCH (s:Location {name:"%s"})-[:ENCOMPASSES]->' \
                     '(d:Location {name:"%s"}) RETURN d' % \
                     (state, district)
             res, _ = db.cypher_query(query)
             district = Location.inflate(res[0][0])
             address.encompassed_by.connect(district)
示例#7
0
 def setUp(self):
     self.address = Address(city="Wixom", state="MI").save()
     try:
         Location.nodes.get(name="Wixom").delete()
     except (Location.DoesNotExist, DoesNotExist):
         pass
     except MultipleNodesReturned:
         query = 'MATCH (a:Location {name:"Wixom"}) RETURN a'
         res, _ = db.cypher_query(query)
         for location in res[0]:
             Location.inflate(location).delete()
     try:
         self.state = Location.nodes.get(name="Michigan")
     except MultipleNodesReturned:
         query = 'MATCH (a:Location {name:"Michigan"}) RETURN a'
         res, _ = db.cypher_query(query)
         for location in res[0]:
             Location.inflate(location).delete()
     except (Location.DoesNotExist, DoesNotExist):
         self.state = Location(name="Michigan").save()
示例#8
0
def connect_to_state_districts(object_uuid):
    try:
        address = Address.nodes.get(object_uuid=object_uuid)
    except (DoesNotExist, Address.DoesNotExist, CypherException, IOError,
            ClientError) as e:
        raise connect_to_state_districts.retry(exc=e,
                                               countdown=3,
                                               max_retries=None)
    try:
        lookup_url = settings.OPENSTATES_DISTRICT_SEARCH_URL % \
            (address.latitude, address.longitude) \
            + "&apikey=53f7bd2a41df42c082bb2f07bd38e6aa"
    except TypeError:
        # in case an address doesn't have a latitude or longitude
        return False
    response = requests.get(
        lookup_url, headers={"content-type": 'application/json; charset=utf8'})
    response_json = response.json()
    try:
        for rep in response_json:
            try:
                sector = 'state_%s' % rep['chamber']
                query = 'MATCH (l:Location {name: "%s", sector:"federal"})-' \
                        '[:ENCOMPASSES]->(district:Location {name:"%s", ' \
                        'sector:"%s"}) RETURN district ' % \
                        (us.states.lookup(address.state).name,
                         rep['district'], sector)
                res, _ = db.cypher_query(query)
            except KeyError:
                return False
            try:
                res = res[0]
            except IndexError as e:
                raise connect_to_state_districts.retry(exc=e,
                                                       countdown=3,
                                                       max_retries=None)
            try:
                state_district = Location.inflate(res.district)
            except (CypherException, ClientError, IOError,
                    CouldNotCommit) as e:
                raise connect_to_state_districts.retry(exc=e,
                                                       countdown=3,
                                                       max_retries=None)
            if state_district not in address.encompassed_by:
                address.encompassed_by.connect(state_district)
        spawn_task(task_func=create_and_attach_state_level_reps,
                   task_param={"rep_data": response_json})
        return True
    except (CypherException, IOError, ClientError, CouldNotCommit) as e:
        raise connect_to_state_districts.retry(exc=e,
                                               countdown=3,
                                               max_retries=None)
示例#9
0
    def test_google_places_state(self):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        location = parse_google_places(quebec_data['address_components'],
                                       quebec_data['place_id'])

        self.assertEqual(location.name, "Quebec")
        res, _ = db.cypher_query('MATCH (a:Location '
                                 '{name: "Canada"}) RETURN a')
        country = Location.inflate(res.one)

        self.assertTrue(country in location.encompassed_by)
        self.assertTrue(location in country.encompasses)
示例#10
0
 def test_create_state_districts_already_exist(self):
     mi = Location(name=us.states.lookup("MI").name,
                   sector="federal").save()
     address = Address(state="MI", latitude=42.532020,
                       longitude=-83.496500).save()
     upper = Location(name="15", sector="state_upper").save()
     lower = Location(name="38", sector="state_lower").save()
     address.encompassed_by.connect(lower)
     address.encompassed_by.connect(upper)
     mi.encompasses.connect(upper)
     upper.encompassed_by.connect(mi)
     mi.encompasses.connect(lower)
     lower.encompassed_by.connect(mi)
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertTrue(res.result)
     query = 'MATCH (l:Location {name:"38", sector:"state_lower"}), ' \
             '(l2:Location {name:"15", sector:"state_upper"}) RETURN l, l2'
     res, _ = db.cypher_query(query)
     lower = Location.inflate(res[0].l)
     upper = Location.inflate(res[0].l2)
     self.assertTrue(lower in address.encompassed_by)
     self.assertTrue(upper in address.encompassed_by)
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertTrue(res.result)
     query = 'MATCH (l:Location {name:"38", sector:"state_lower"}), ' \
             '(l2:Location {name:"15", sector:"state_upper"}) RETURN l, l2'
     res, _ = db.cypher_query(query)
     self.assertEqual(len(res[0]), 2)  # assert only two nodes returned
     self.assertEqual(lower, Location.inflate(res[0].l))
     # assert only one lower node
     self.assertEqual(upper, Location.inflate(res[0].l2))
     # assert only one upper node
     mi.delete()
     address.delete()
     upper.delete()
     lower.delete()
示例#11
0
 def set_encompassing(self):
     from .tasks import connect_to_state_districts
     try:
         encompassed_by = Location.nodes.get(name=self.city)
         if Location.get_single_encompassed_by(
                 encompassed_by.object_uuid) != \
                 us.states.lookup(self.state).name:
             # if a location node exists with an incorrect encompassing
             # state
             raise DoesNotExist("This Location does not exist")
     except (Location.DoesNotExist, DoesNotExist):
         encompassed_by = Location(name=self.city, sector="local").save()
         try:
             city_encompassed = Location.nodes.get(
                 name=us.states.lookup(self.state).name)
         except (Location.DoesNotExist, DoesNotExist):
             city_encompassed = Location(name=us.states.lookup(
                 self.state).name,
                                         sector="federal").save()
         if city_encompassed not in encompassed_by.encompassed_by:
             encompassed_by.encompassed_by.connect(city_encompassed)
         if encompassed_by not in city_encompassed.encompasses:
             city_encompassed.encompasses.connect(encompassed_by)
     except (MultipleNodesReturned, Exception):
         query = 'MATCH (l1:Location {name:"%s"})-[:ENCOMPASSED_BY]->' \
                 '(l2:Location {name:"%s"}) RETURN l1' % \
                 (self.city, self.state)
         res, _ = db.cypher_query(query)
         if res.one is not None:
             encompassed_by = Location.inflate(res.one)
         else:
             encompassed_by = None
     if encompassed_by is not None:
         if encompassed_by not in self.encompassed_by:
             self.encompassed_by.connect(encompassed_by)
     # get or create the state level districts and attach them to the
     # address
     spawn_task(task_func=connect_to_state_districts,
                task_param={'object_uuid': self.object_uuid})
     return self