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
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