示例#1
0
def pick_sightings_for_place(request, country_code, slug):
    place, redirect_args = _get_place(country_code, slug)
    if redirect_args:
        return HttpResponseRedirect(
            reverse('place-pick_sightings_for_place', args=redirect_args)
        )
    
    return pick_sightings(request, '/%s/%s/add-sightings/' % (
        country_code.lower(), place.url_slug()
    ))
示例#2
0
def add_trip(request, country_code, slug):
    place, redirect_args = _get_place(country_code, slug)
    if redirect_args:
        return HttpResponseRedirect(
            reverse('place-add-trip', args=redirect_args)
        )
    
    selected = _get_selected(request.POST)
    unknowns = _get_unknowns(request.POST)
    
    if 'finish' in request.POST and (selected or unknowns):
        return finish_add_trip(request, place, selected, unknowns)
    
    q = request.REQUEST.get('q', '').strip()
    # Clear search if they selected one of the options
    if _add_selected_was_pressed(request.POST):
        q = ''
    # Or if they hit the 'clear' button:
    if 'clear' in request.POST:
        q = ''
    
    results = []
    showing_animals_seen_here = False
    if q:
        # Search for 10 but only show the first 5, so our custom ordering 
        # that shows animals spotted here before can take effect
        results = add_trip_utils.search(q, limit=10, place=place)[:5]
    else:
        # Show animals that other people have seen here
        showing_animals_seen_here = True
        results = add_trip_utils.previously_seen_at(place)
    
    details = add_trip_utils.bulk_lookup(selected, place=place)
    
    return render(request, 'trips/add_trip.html', {
        'place': place,
        'results': results,
        'selected_details': details,
        'q': q,
        'unknowns': unknowns,
        'request_path': request.path,
        'debug': pformat(request.POST.lists()),
        'showing_animals_seen_here': showing_animals_seen_here,
    })
示例#3
0
def ajax_search_species(request, country_code, slug):
    place, redirect_args = _get_place(country_code, slug)
    if redirect_args:
        return HttpResponseRedirect(
            reverse('place-add-trip-ajax-search-species', args=redirect_args)
        )
    
    q = request.GET.get('q', '')
    if not q:
        results = add_trip_utils.previously_seen_at(place)
        return render(request, 'trips/ajax_search_species.html', {
            'q': '',
            'showing_animals_seen_here': True,
            'results': results,
        })
    
    if len(q) >= 3:
        return render(request, 'trips/ajax_search_species.html', {
            'q': q,
            'showing_animals_seen_here': False,
            'results': add_trip_utils.search(q, limit=10, place=place)[:5],
        })
    else:
        return render(request, 'trips/_add_trip_help.html')
示例#4
0
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,
        })