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()
import django import sys import os sys.path.append(os.path.dirname(__file__) + '/..') os.environ['DJANGO_SETTINGS_MODULE'] = 'carebackend.settings' django.setup() from places.models import Place import pandas as pd import sys fl = sys.argv[1] df = pd.read_csv(fl) for _, row in df.iterrows(): try: p = Place.objects.get(place_id=row['place_id']) except Place.DoesNotExist: p = Place(place_id=row['place_id']) p.lat = row['lat'] p.lng = row['lng'] p.address = row['formatted_address'] p.user_rating = row['rating'] if not p.name: p.name = row['name'] p.num_ratings = row['user_ratings_total'] p.gift_card_url = row.get('gift_card_url') p.photo_attribution = row['image_attribution'] p.image_url = row['photo_url'] p.save()
df = pd.read_csv(fl) for _, row in df.iterrows(): place_id = row['place_id'] try: p = Place.objects.get(place_id=place_id) except Place.DoesNotExist: 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 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 or row['lat'] p.lng = lng or row['lng'] p.image_attribution = photo_attrib p.gift_card_url = row.get('gift_card_url','') p.takeout_url = row.get('takeout_url','') p.donation_url = row.get('donation_url','') p.email_contact = row.get('email','') p.save()