def _resolve_location_string(self, location_str): if location_str: # TODO: could do some geocoding filtering here result = smart_text_resolve(location_str) if result.place is not None: place = result.place elif result.location is not None: place = Place(name=location_str, location=result.location) else: return None, None else: return None, None # neither the location nor place are in the DB yet. do this now l = place.location if l: place.location, _ = Location.close_manager.get_close_or_create( address=l.address, postcode=l.postcode, town=l.town, state=l.state, country=l.country, neighborhood=l.neighborhood, latitude=l.latitude, longitude=l.longitude) place, _ = Place.objects.get_or_create(name=place.name, location=place.location) return place, result.parse_status
def get_place(self, allow_import=True): ''' Returns full Place object for this event's venue. If venue has an ID, full place info will be taken from a separate FB request. Otherwise, one will be cobbled together from the available info. If data has no 'venue' field, this will None. In this case, it might be worth calling get_field with the key 'location' to get a plaintext primitive place name as a fallback. If allow_import is True, the Place will be created from a FBPage imported on the spot if there is an ID specified for the venue. Otherwise, the venue id will be ignored Remember the location assigned to the Place is not saved, so simply saving the Place (or even saving the Location then the Place) won't work. Need to do this: place.location.save() place.location = place.location # reassign so ModelField assignment statement handles the new ID place.save() ''' place = None # if we've got a place ID to work with, pull the full Place fb_pid = self.get_venue_id() if fb_pid and allow_import: client = self._client or None try: fb_page = FBPage.import_live(fb_pid, client=client) if fb_page.valid: place = fbpage_to_place(fb_page, save=False) if place: return place except IOError: # if there's a network problem log it and move on # TODO: log network error pass # if venue isn't available, we're done venue = self.data.get('venue') if not venue: return None # couldn't get a linked place on Facebook place = Place(name=self.data.get('location', '')) if venue: state = venue.get('state', '').strip() # State entry is often full state name if state != '' and len(state) != 2: state = abbreviate_state(state) or '' place.location = Location( address=venue.get('street', '').strip(), town=venue.get('city', '').strip(), state=state, postcode=venue.get('zip', '').strip(), latitude=venue.get('latitude'), longitude=venue.get('longitude')) return place
def fbpage_to_place(fbpage, save=False): if not fbpage.valid: return None p = Place() # special parking/hours objects used to serialize to DB # TODO: this is temporary. dig into django custom model field to make less hacky p.hours = fbpage.get_hours() parking = fbpage.get_parking() if parking: p.set_parking(parking) location = fbpage.get_location() if location is not None and save: location.save() p.location = location p.name = fbpage.get_field('name', '').strip()[:200] p.fb_id = fbpage.get_field('id', '').strip()[:50] p.description = fbpage.get_field('description', '').strip() # if no description, try 'about' if not p.description: p.description = fbpage.get_field('about', '').strip() p.phone = fbpage.get_field('phone', '').strip()[:200] p.url = fbpage.get_field('website', '').strip() if p.url: p.url = p.url.split()[0][:200] try: im_url = fbpage.get_picture_url(size='large') p.image = imagefile_from_url(im_url) except IOError: # TODO: log network/image format error pass if save: p.save() return p