def finish_add_trip(request, place, selected, unknowns): selected_details = add_trip_utils.bulk_lookup(selected) if 'add_trip_form_displayed' not in request.POST: # Display form for the first time form = AddTripForm( user = request.user, place = place, initial = {'name': 'My trip'} ) else: form = AddTripForm(request.POST, user = request.user, place = place) if form.is_valid(): trip = Trip( name = form.cleaned_data['name'], start = form.cleaned_data['start'], start_accuracy = form.cleaned_data['start_accuracy'], description = form.cleaned_data['description'], rating = form.cleaned_data['rating'], place = place, ) trip.save() # Now save the selected and unknown sightings for i, details in enumerate(selected_details): trip.sightings.create( species = species_for_freebase_details(details), place = place, note = request.POST.get('selected_note_%s' % i, ''), ) for i, name in enumerate(unknowns): trip.sightings.create( place = place, species_inexact = name, note = request.POST.get('unknown_note_%s' % i, ''), ) # And we're done! return HttpResponseRedirect(trip.get_absolute_url()) return render(request, 'trips/add_trip_final.html', { 'form': form, 'place': place, 'selected': selected, 'selected_details': selected_details, 'unknowns': unknowns, 'request_path': request.path, })
def finish_add_sightings_to_place(request, country_code, slug): """ At this point, the user has told us exactly what they saw. We're going to add those as sightings, but first, let's see if we can get them to add a full trip (which has a date or a title or both, and a optional description for their tripbook). """ place, redirect_args = _get_place(country_code, slug) if redirect_args: return HttpResponseRedirect( reverse('place-add_sightings_to_place', args=redirect_args) ) saw_ids = request.REQUEST.getlist('saw') trip_id = request.REQUEST.get('trip', None) if trip_id: trip = get_object_or_404(Trip, pk=trip_id) if request.user.is_anonymous() or request.user != trip.created_by: # somehow they ended up trying to add something to a trip they # didn't own return HttpResponseForbidden() else: trip = None hiddens = [] for saw_id in saw_ids: hiddens.append( {'name': 'saw', 'value': saw_id} ) # Text descriptions of sightings, so we can display them to the user sightings = [] for saw_id in saw_ids: species = lookup_xapian_or_django_id(saw_id) if species: sightings.append(species) else: sightings.append(saw_id) if request.method == 'POST': if request.POST.get('just-sightings'): for i, id in enumerate(saw_ids): # Look up the id species = lookup_xapian_or_django_id(id) note = request.POST.get('sighting_note_%d' % i, '') if species: # None if it was somehow invalid Sighting.objects.create( species = species, place = place, note = note ) else: Sighting.objects.create( species_inexact = id, place = place, note = note ) return Redirect(place.get_absolute_url()) # if we don't have a trip yet, create or find one via AddTripForm if not trip: form = AddTripForm( request.POST, initial = {'place': place}, user = request.user, place = place ) if form.is_valid(): if request.POST.get('add-to-existing'): # if the user chose this option they want to add this # sighting to an existing trip of theirs trip = Trip.objects.get( id = form.cleaned_data['user_trips'] ) else: # create a new trip trip = Trip( name = form.cleaned_data['name'], start = form.cleaned_data['start'], start_accuracy = form.cleaned_data['start_accuracy'], description = form.cleaned_data['description'], rating = form.cleaned_data['rating'], place = place, ) trip.save() # created_by should happen automatically # by now we should have a trip. If not, then it probably means the # form was invalid if trip: # Now we finally add the sightings! for i, id in enumerate(saw_ids): # Look up the id species = lookup_xapian_or_django_id(id) note = request.POST.get('sighting_note_%d' % i, '') if species: # None if it wasn't a valid ID trip.sightings.create( species = species, place = place, note = note, ) else: # Invalid IDs are inexact sightings, add them as such trip.sightings.create( place = place, species_inexact = id, note = note, ) # TODO: Shouldn't allow a trip to be added if no valid # sightings return Redirect(trip.get_absolute_url()) elif not trip: # We pre-populate the name if not request.user.first_name: whos_trip = 'My' else: whos_trip = request.user.first_name if whos_trip.endswith('s'): whos_trip += "'" else: whos_trip += "'s" whos_trip += ' trip' form = AddTripForm( initial = {'name': whos_trip, 'place': place}, user = request.user, place = place ) if trip_id: if len(sightings)==0: # no sightings to add, so just redirect to the trip return Redirect(trip.urls.absolute) return render(request, 'trips/confirm-add-to-trip.html', { 'hiddens': hiddens, 'place': place, 'sightings': sightings, 'trip': trip, }) else: tcount = request.user.created_trip_set.all().filter( place = place ).count() return render(request, 'trips/why-not-add-to-your-tripbook.html', { 'hiddens': hiddens, 'place': place, 'form': form, 'tcount': tcount, 'sightings': sightings, })