def _import(self, folder): json_folder = folder + JSONPATTERN print("Nuking locations:", end='') Location.objects.all().delete() print("Done") print("Importing: %s" % json_folder) file_counter = 0 for filename in iglob(json_folder): with open(filename) as f: file_counter += 1 record_id = self._file_to_record_number(folder, filename) print("[%04d] %s " % (file_counter, record_id), end="") details = json.load(f)[0] try: location = Location.objects.get( place_id=int(details["place_id"])) print("loaded ", end="") except Location.DoesNotExist: location = Location( place_id=int(details["place_id"]), display_name=details["display_name"], lat=float(details["lat"]), lon=float(details["lon"]), osm_id=int(details["osm_id"]), geom=details["geojson"] if "geojson" in details else None, ) location.save() print("saved ", end="") # Synching with record try: record = Record.objects.get(record_number=record_id) location.records.add(record) location.save() print("linked ", end="") except Record.DoesNotExist: print("record missing") location.delete() print("")
def record_update_with_location(record_id, place_id): try: location = Location.objects.get(place_id=place_id) except Location.DoesNotExist: details = grab_location_data(place_id) location = Location( place_id=int(details["place_id"]), display_name=details['localname'], lat=details["centroid"]["coordinates"][0], lon=details["centroid"]["coordinates"][1], osm_id=int(details["osm_id"]), geom=details["geometry"] if "geometry" in details else None, ) location.save() record = Record.objects.get(id=record_id) if record.location_set.count(): record.location_set.all().delete() record.save() record.location_set.add(location) record.save() pass
def create_location(self, chat, user, photo, longitude, latitude): location = Location(chat, user, photo, longitude, latitude) self._context.get_context().add(location) self._context.get_context().flush() self._context.get_context().refresh(location) return location