def reset_database(request): Flight.objects.all().delete() Distance.objects.all().delete() num_trip = 0 num_dis = 0 for trip_wrapper in base_trips.flights['flights']: trip = trip_wrapper['flight'] new_flight = Flight() new_flight.build_from_json(trip) new_flight.save() num_trip += 1 if trip['round-trip'] == 'True': new_return_flight = Flight() new_return_flight.build_return_flight(new_flight, trip['return']) new_return_flight.save() num_trip += 1 keys = [trip['origin'], trip['destination']] keys.sort() d, created = Distance.objects.get_or_create(point_a = keys[0], point_b = keys[1]) if created: d.distance_km = haversine( new_flight.origin_long, new_flight.origin_lat, new_flight.dest_long, new_flight.dest_lat) d.save() num_dis += 1 recap = 'num of entries added: %d <br />' % num_trip recap += 'num of distances created: %d <br />' % num_dis return HttpResponse(recap)