Пример #1
0
    def get_species(self, user=None, limit=None):
        """
        If user argument is provided, seen=True flag will be added to all
        species which have been seen by that user.
        """
        from zoo.trips.models import Trip, Sighting
        if user and not user.is_anonymous():
            seen_species = Trip.get_passport(user).seen_species
        else:
            seen_species = []

        by_count = {}
        for sighting in Sighting.objects.filter(
                place=self, species__isnull=False
            ).select_related('species'):
            species = sighting.species
            by_count[species] = by_count.get(species, 0) + 1

        if by_count.values():
            max_species = max(by_count.values())

        species_list = by_count.keys()
        for species in species_list:
            species.count = by_count[species]
            species.quad = int(4 * (by_count[species] - 1.0) / max_species)
            if species in seen_species:
                species.seen = True

        species_list.sort(key=lambda s: s.common_name)

        if limit:
            species_list = species_list[:limit]

        return species_list
Пример #2
0
def place(request, country_code, slug):
    place, redirect_args = _get_place(country_code, slug)
    if redirect_args:
        return HttpResponseRedirect(
            reverse('place', args=redirect_args)
        )
    
    species_list = place.get_species(request.user, SPECIES_ON_PLACE_PAGE + 1)
    times_sorted = get_times_sorted(place)
    
    return render(request, 'places/place.html', {
        'place': place,
        'species_list': species_list[0:SPECIES_ON_PLACE_PAGE],
        'species_list_more': len(species_list) > SPECIES_ON_PLACE_PAGE,
        'opening_times': times_sorted,
        'rating' : Trip.get_average_rating(place),
        'been_here': Trip.objects.filter(
            place = place, created_by = request.user
        ).count(),
        'places_nearby': place.nearby.filter(
            place__is_unlisted = False
        ).select_related(
            'place', 'place__country'
        )[:3],
    })
Пример #3
0
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,
    })
Пример #4
0
def trip_view(request, username, trip_id):
    user = get_object_or_404(User, username=username)
    trip = get_object_or_404(Trip, id=trip_id, created_by=user)
    if user == request.user:
        photos = trip.photos.all().select_related('created_by')
    else:
        photos = trip.visible_photos().select_related('created_by')
    return render(request, 'trips/trip.html', {
        'profile': user.get_profile(),
        'trip': trip,
        'sightings': trip.sightings.select_related('species', 'place'),
        # do this to prepare the rating for the view
        'rating': Trip.calculate_rating_object([trip]),
        'belongs_to_user': request.user.id == user.id,
        'visible_photos': photos,
    })
Пример #5
0
 def passport(self):
     return Trip.get_passport(self.user)
Пример #6
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,
        })