def test_get_trip_by_id(django_assert_num_queries): trip_owner, _ = get_or_create_user("trip-owner") trip = create_trip(trip_owner, "test trip") trip_member, _ = get_or_create_user("trip-member") add_member_to_trip(trip_member, trip) for user in [trip_owner, trip_member]: with django_assert_num_queries(1): retrieved_trip = get_trip_by_id(user, trip.trip_id) assert retrieved_trip.pk == trip.pk assert get_trip_by_id(trip_owner, str(uuid4())) is None someone_else, _ = get_or_create_user("someone-else") assert get_trip_by_id(someone_else, uuid4()) is None
def get_trip_handler(request, trip_id): db_trip = get_trip_by_id(request.user, trip_id) if not db_trip: return error_response(status=404) trip = get_response_trip(db_trip) return success_response(data=trip)
def create_location_handler(request, trip_id): db_trip = get_trip_by_id(request.user, trip_id) if not db_trip: return error_response(status=404) event = parse_location_from_request_body(request.body) if not event.is_valid: return validation_error_response(event.validation_errors) location = persist_location(db_trip, event) return success_response(status=201, data=location)
def itinerary_handler(request, trip_id): trip = get_trip_by_id(request.user, trip_id) if trip is None: return error_response(status=404) locations = get_locations_for_trip(trip) logger.info( "constructing itinerary for trip '%s' with %d locations", trip.trip_id, len(locations), ) itinerary = get_itinerary_events_by_day_for_locations(locations) return success_response(data={"itinerary": to_response_shape(itinerary)})
def update_trip_handler(request, trip_id): db_trip = get_trip_by_id(request.user, trip_id) if not db_trip: return error_response(status=404) parsed_body = safe_parse_json(request.body) if parsed_body is None: return error_response(message="Could not parse JSON body") try: updated_trip = update_trip(request.user, db_trip, **parsed_body) except UpdateNotAllowed: return validation_error_response( ["Cannot update one or more requested fields"]) return success_response(data=Trip.from_db_model(updated_trip))