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 get_location(self): '''returns a places.models.Location object''' fb_loc = self.data.get('location') if not fb_loc: return None state = fb_loc.get('state', '').strip() # State entry is often full state name if state != '' and len(state) != 2: state = abbreviate_state(state) or '' return Location(address=fb_loc.get('street', '').strip(), town=fb_loc.get('city', '').strip(), state=state, postcode=fb_loc.get('zip', '').strip(), latitude=fb_loc.get('latitude'), longitude=fb_loc.get('longitude'))