def setUp(self): place = Place() place.name = 'Sirvo Jantar' place.address = 'Endereço' place.neighborhood = 'Bairro' place.time = 'Seg. a Sexta: 19h às 2' place.phone = '(41) 5555-5555' place.city = City.objects.get(name='Curitiba') place.save() jantar = Meal.objects.get(name='Jantar') place.meal.add(jantar) place.save() self.place = place place2 = Place() place2.name = u'Sirvo Almoço' place2.address = 'Endereço' place2.neighborhood = 'Bairro' place2.time = 'Seg. a Sexta: 19h às 2' place2.phone = '(41) 5555-5555' place2.city = City.objects.get(name='São Paulo') place2.save() almoco = Meal.objects.get(name='Almoço') place2.meal.add(almoco) place2.save() self.place2 = place2
def create_dataset(request, name, description, in_places, user_id): try: # if the dataset already exists ds = Dataset.objects.get(name=name) messages.error(request, "שם המאגר קיים במערכת") return False except Dataset.DoesNotExist: pass ds = Dataset() ds.owner = User.objects.get(id=user_id) ds.name = name ds.description = description ds.save() for p in in_places: place = Place() try: place.vendor_id = p["id"] place.address = p["address"].strip() place.city = p["city"].strip() place.title = p["title"].strip() except KeyError, ex: print ex delete_dataset(ds) messages.error(request, "אחד השדות הדרושים חסר. וודא כי כל הרשומות מכילות את השדות: id, address, city, title") return False place.data = json.dumps(p) place.dataset = ds place.save()
def setUp(self): place = Place() place.name = 'Nome' place.address = 'Endereço' place.neighborhood = 'Bairro' place.time = 'Seg. a Sexta: 19h às 2' place.phone = '(41) 5555-5555' place.city = City.objects.get(name='Curitiba') place.save() self.place = place
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()
def process_search(self, circuit, place_name, lat, lng, location, description): """ invokes search_place and processes the response """ location = location.replace(' ', '-') description = description.replace('-', ' ') if location == 'The-World' and lat == 'NO-LAT': self.print_failed_place(place_name, circuit) return elif lat == 'NO-LAT' and location is not 'The-World': result = self.search_place( query=place_name, near=location, ) elif lat is not 'NO-LAT' and location == 'The-World': result = self.search_place( query=place_name, ll=lat + ', ' + lng, ) else: result = self.search_place( query=place_name, near=location, ll=lat + ', ' + lng, ) if result is None: self.print_failed_place(place_name, circuit) return cat_id = None find_venue = False if 'venues' in result: for field in result['venues']: place_name = field['name'] place_id = field['id'] find_venue = True try: place_phone = field['contact']['phone'] except KeyError: place_phone = None try: place_address = field['location']['address'] except KeyError: place_address = None try: latitude = field['location']['lat'] longitud = field['location']['lng'] place_coords = Point(latitude, longitud) except KeyError: # FIXME: place_coords should not default to 0,0 # but for now place_coords is mandatory field on DB place_coords = Point(0, 0) for elem in field['categories']: cat_id = elem['id'] if cat_id is None: cat_id = DEFAULT_PLACE_TYPE_ID if find_venue: # see if already in DB try: pl = Place.objects.get(place_id=place_id) except Place.DoesNotExist: pl = Place() pl.name = place_name pl.place_id = place_id pl.coordinates = place_coords if place_phone is not None: pl.phone_number = place_phone if place_address is not None: pl.address = place_address pl.save() try: pt = PlaceType.objects.get(place_type_id=cat_id) except PlaceType.DoesNotExist: pt = PlaceType.objects.all()[0] pl.place_type.add(pt) pl.save() cs = CircuitStop() cs.circuit = circuit cs.place = pl cs.description = description cs.save() else: self.print_failed_place(place_name, circuit) return
def process_objects(self, circuit): results = OrderedDict() # multiple = true if creating a place and a CircuitStop results['multiple'] = False # conflict = CircuitStop with this place already exists results['conflict'] = False new_place = False place = None place_id = self.cleaned_data.get('place_id', None) print type(place_id) if place_id != '': try: place = Place.objects.get(place_id=place_id) except Place.DoesNotExist: place = None # Enter here if query did not returned a Place if place is None: # Mandatory fields place = Place() place.name = self.cleaned_data.get('name', '') place.coordinates = Point( self.cleaned_data.get('lat'), self.cleaned_data.get('lng') ) if place_id is not None: place.place_id = place_id else: # TODO handle case when no place_id is passed pass # Optional fields if 'address' in self.cleaned_data: place.address = self.cleaned_data.get('address', '') if 'phone_number' in self.cleaned_data: place.phone_number = self.cleaned_data.get('phone_number', '') if 'website' in self.cleaned_data: place.website = self.cleaned_data.get('website', '') if 'crossStreet' in self.cleaned_data: place.crossStreet = self.cleaned_data.get('crossStreet', '') if 'twitter' in self.cleaned_data: place.twitter = self.cleaned_data.get('twitter', '') # get place_type from db or default try: place_type = PlaceType.objects.get( place_type_id=self.cleaned_data.get('place_type_id') ) except PlaceType.DoesNotExist: place_type = PlaceType.objects.get( place_type_id=DEFAULT_PLACE_TYPE_ID ) place = Place.new_place_save(place, place_type) # Sync new Place with MongoDB # check_mongo(place) # Setting the new place flag to True new_place = True # Check if the place object has not yet been included in # a circuit stop that is part of the circuit if circuit.circuit_stops.filter(place=place).exists(): # There is a conflict with the current state of the # resource since there already exists a circuit stop # referring to that place results['conflict'] = True return results # Creating a circuit stop for the place circuit_stop = CircuitStop( circuit=circuit, place=place, description=self.clean_description(), picture=self.cleaned_data.get('picture') ) circuit_stop.save() # Now we add it to the circuit circuit.circuit_stops.add(circuit_stop) circuit.save() # Create objects dictionary results['circuit_stop'] = circuit_stop if new_place: results['place'] = place results['multiple'] = True # Return results dictionary return results
def process_search(self, circuit, place_name, lat, lng, location, description ): """ invokes search_place and processes the response """ location = location.replace(' ', '-') description = description.replace('-', ' ') if location == 'The-World' and lat == 'NO-LAT': self.print_failed_place(place_name, circuit) return elif lat == 'NO-LAT' and location is not 'The-World': result = self.search_place( query=place_name, near=location, ) elif lat is not 'NO-LAT' and location == 'The-World': result = self.search_place( query=place_name, ll = lat+', '+lng, ) else: result = self.search_place( query = place_name, near = location, ll = lat+', '+lng, ) if result is None: self.print_failed_place(place_name, circuit) return cat_id = None find_venue = False if 'venues' in result: for field in result['venues']: place_name = field['name'] place_id = field['id'] find_venue = True try: place_phone = field['contact']['phone'] except KeyError: place_phone = None try: place_address = field['location']['address'] except KeyError: place_address = None try: latitude = field['location']['lat'] longitud = field['location']['lng'] place_coords = Point(latitude, longitud) except KeyError: # FIXME: place_coords should not default to 0,0 # but for now place_coords is mandatory field on DB place_coords = Point(0,0) for elem in field['categories']: cat_id = elem['id'] if cat_id is None: cat_id = DEFAULT_PLACE_TYPE_ID if find_venue: # see if already in DB try: pl = Place.objects.get(place_id = place_id) except Place.DoesNotExist: pl = Place() pl.name = place_name pl.place_id = place_id pl.coordinates = place_coords if place_phone is not None: pl.phone_number = place_phone if place_address is not None: pl.address = place_address pl.save() try: pt = PlaceType.objects.get(place_type_id=cat_id) except PlaceType.DoesNotExist: pt = PlaceType.objects.all()[0] pl.place_type.add(pt) pl.save() cs = CircuitStop() cs.circuit = circuit cs.place = pl cs.description = description cs.save() else: self.print_failed_place(place_name, circuit) return
fl = sys.argv[1] 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()