Exemplo n.º 1
0
 def save(self, *args, **kwargs):
     from places.helper import check_link_against_blacklist
     from places.google_places_helper import fetch_details_for_place_id
     if self.gift_card_url and not check_link_against_blacklist(
             self.gift_card_url):
         raise Exception("Bad Link Saved")
     # if self.lat and self.lng:
     #     self.geom = Point([float(x) for x in (self.lng, self.lat)], srid=4326)
     r, _, _ = fetch_details_for_place_id(self.place_id)
     if not r:
         raise Exception("Failed to fetch details from google api")
     self.lat = r['geometry']['location']['lat']
     self.lng = r['geometry']['location']['lng']
     self.geom = Point(float(self.lng), float(self.lat), srid=4326)
     super(self.__class__, self).save(*args, **kwargs)
Exemplo n.º 2
0
def accept_place(modeladmin, request, queryset, accept_link=True):
    from places.google_places_helper import fetch_details_for_place_id
    # we listify the queryset since we're going to edit objects such that they won't appear
    # in the queryset anymore
    any_added = False
    for suggestion in list(queryset):
        place_id = suggestion.place_id
        try:
            p = Place.objects.get(place_id=place_id)
        except Place.DoesNotExist:
            any_added = True
            p = Place(place_id=place_id)

            r, photo_url, photo_attrib = fetch_details_for_place_id(place_id)
            if not r.get(
                    'rating'
            ):  # probably not meaningful place, or Google returned NOT_FOUND
                suggestion.processed = True
                suggestion.save()
                continue
            p.name = r['name']
            p.address = r['formatted_address']
            p.image_url = photo_url
            p.user_rating = r['rating']
            p.num_ratings = r['user_ratings_total']
            p.place_types = ','.join(r.get('types', []))
            p.place_url = r.get('website')
            lat, lng = r['geometry']['location']['lat'], r['geometry'][
                'location']['lng']
            p.lat = lat
            p.lng = lng
            p.image_attribution = photo_attrib
        if accept_link:
            p.gift_card_url = check_link_against_blacklist(
                suggestion.gift_card_url) or p.gift_card_url
        p.donation_url = p.donation_url or suggestion.donation_url
        p.email_contact = suggestion.email or p.email_contact
        p.save()
        suggestion.processed = True
        suggestion.save()
    if any_added:
        # Note: this is a fairly expensive operation, but should be ok to run
        # once at the end of an admin action
        Area.update_area_for_all_places()
Exemplo n.º 3
0
    def save(self, *args, **kwargs):
        from places.google_places_helper import fetch_details_for_place_id
        r, photo_url, photo_attrib = fetch_details_for_place_id(self.place_id)
        self.name = r['name']
        self.address = r['formatted_address']
        if r['rating']:
            self.user_rating = r['rating']
        self.num_ratings = r['user_ratings_total']
        self.place_types = ','.join(r.get('types', []))
        self.place_url = r.get('website')
        lat, lng = r['geometry']['location']['lat'], r['geometry']['location']['lng']
        self.lat = lat
        self.lng = lng
        self.image_url = photo_url
        self.image_attribution = photo_attrib

        from places.helper import check_link_against_blacklist
        if self.gift_card_url and not check_link_against_blacklist(self.gift_card_url):
            raise Exception("Bad Link Saved")

        if (self.lat and self.lng):
            self.geom = Point([float(x) for x in (self.lng, self.lat)], srid=4326)
        super(self.__class__, self).save(*args, **kwargs)
     lng = centroid.x
 elif row.get('Location') and not pd.isna(row['Location']):
     lat, lng = [x.strip() for x in row['Location'].split(',')]
 elif row.get('Geometry') and not pd.isna(row['Geometry']):
     geometry_json = json.loads(row['Geometry'])
     xmin = geometry_json['geometry']['viewport']['southwest']['lng']
     ymin = geometry_json['geometry']['viewport']['southwest']['lat']
     xmax = geometry_json['geometry']['viewport']['northeast']['lng']
     ymax = geometry_json['geometry']['viewport']['northeast']['lat']
     bbox = (xmin, ymin, xmax, ymax)
     n.bounds = Polygon.from_bbox(bbox)
     lat = geometry_json['geometry']['location']['lat']
     lng = geometry_json['geometry']['location']['lng']
 elif row.get('Place Id') and not pd.isna(row['Place Id']):
     place_id = row['Place Id']
     r, photo_url, photo_attrib = fetch_details_for_place_id(place_id)
     geometry_json = r['geometry']
     xmin = geometry_json['viewport']['southwest']['lng']
     ymin = geometry_json['viewport']['southwest']['lat']
     xmax = geometry_json['viewport']['northeast']['lng']
     ymax = geometry_json['viewport']['northeast']['lat']
     bbox = (xmin, ymin, xmax, ymax)
     n.bounds = Polygon.from_bbox(bbox)
     lat = geometry_json['location']['lat']
     lng = geometry_json['location']['lng']
 else:
     print("missing necessary data!")
     continue
 n.lat = lat
 n.lng = lng
 n.area = area