Пример #1
0
def set_location(request):
    """
    Logged in users use this view to set the location on their profile -
    anonymous users get a cookie instead
    """
    from zoo.search import search_locations
    (current_location, (lat, lon)) = location_from_request(request)

    location = request.POST.get('location', '')
    msg = ''

    if location:
        description, (lat, lon) = google_geocode(location)
        if description is not None:
            msg = 'Your location has been set to <strong>%s</strong>.' % description
            try:
                random_animal = Species.objects.order_by('?')[0]
                nearest = nearest_places_with_species(random_animal.common_name, (lat, lon))
                msg += ' As an example, your nearest <a href="%s">%s</a> is %s miles away, at <a href="%s">%s</a>.' % (random_animal.urls.absolute, random_animal.common_name, int(nearest[0].distance_miles+0.5), nearest[0].urls.absolute, nearest[0])
            except IndexError:
                nearest = None
            current_location = description
            # Set their location
            if not request.user.is_anonymous():
                profile = request.user.get_profile()
                profile.location = description
                profile.latitude = lat
                profile.longitude = lon
                profile.recalculate_percentage_complete()
                profile.save()
            else:
                # Set it in a cookie instead
                response = Redirect('/set-location/?done')
                response.set_cookie(
                    key = 'location',
                    value = '%s:%f,%f' % tuple(
                        [description] + result['latlon']
                    ),
                    path = '/',
                )
                return response
        else:
            msg = 'No matches for your search'
    return render(request, 'accounts/set_location.html', {
        'msg': msg,
        'current_location': current_location,
    })
Пример #2
0
def species(request, slug):
    try:
        species = Species.objects.get(slug=slug)
    except Species.DoesNotExist:
        # eggs
        species = get_object_or_404(SuperSpecies, slug=slug)
        t = loader.get_template('animals/%s.html' % species.type)
        c = Context({'species': species})
        return HttpResponse(t.render(c), status=species.status)
    
    num_favourites = species.favourited.count()
    
    # If we have the user's location, find the nearest animal of this species
    description, (latitude, longitude) = location_from_request(request)
    nearest = None
    if description:
        try:
            nearest = [p for p in nearest_places_with_species(
                'exactspecies:species_%s' % species.pk,
                '%f %f' % (latitude, longitude)
            ) if not p.is_unlisted][0]
        except IndexError:
            nearest = None
    
    spotters = User.objects.filter(
        created_sighting_set__species = species
    ).distinct()
    return render(request, 'animals/species.html', {
        'species': species,
        'favourited': species.is_favourited_by(request.user),
        'num_favourites': num_favourites,
        'nearest': nearest,
        'location_description': description,
        'spotters': spotters[0:SPOTTERS_ON_SPECIES_PAGE],
        'more_spotters': spotters.count() > SPOTTERS_ON_SPECIES_PAGE,
    })