Beispiel #1
0
def suggest_species(request, username, photo_id):
    from zoo.trips.models import Sighting
    photo = get_object_or_404(
        Photo, created_by__username=username, pk=photo_id
    )
    
    note = request.POST.get('note', '').strip()
    
    guid = request.POST.get('guid', None)
    
    if not guid:
        # Do we have an add_species_%s style guid?
        for key in request.POST:
            if key.startswith('add_selected_'):
                guid = key.replace('add_selected_', '').split('.')[0]
                break
    
    if guid:
        # Create species page for that animal
        selected_details = add_trip_utils.bulk_lookup(
            [guid.replace('#', '/guid/')]
        )
        if len(selected_details) == 1:
            # Now save the selected and unknown sightings
            species = species_for_freebase_details(selected_details[0])
            photo.suggestions.create(
                species = species,
                suggested_by = request.user,
                suggested_at = datetime.datetime.now(),
                denorm_suggestion_for = photo.created_by,
                note = note,
            )
            return HttpResponseRedirect(photo.get_absolute_url())

    add_unknown_text = request.POST.get('add_unknown_text', '').strip()
    if 'add_unknown.x' in request.POST and add_unknown_text:
        photo.suggestions.create(
            species_inexact = add_unknown_text,
            suggested_by = request.user,
            suggested_at = datetime.datetime.now(),
            denorm_suggestion_for = photo.created_by,
            note = note,
        )
        return HttpResponseRedirect(photo.get_absolute_url())

    # If we get here, we need to show search results for the 'species' string
    # normally this means the user doesn't have JavaScript enabled
    species_q = request.REQUEST.get('species', '')
    results = []
    if species_q:
        kwargs = {
            'limit': 10,
        }
        if photo.trip:
            kwargs['place'] = photo.trip.place
        results = add_trip_utils.search(species_q, **kwargs)[:5]

    return render(request, 'photos/suggest_species.html', {
        'photo': photo,
        'species_q': species_q,
        'note': note,
        'results': results,
    })
Beispiel #2
0
def add_species(request, username, photo_id):
    from zoo.trips.models import Sighting
    photo = get_object_or_404(
        Photo, created_by__username=username, pk=photo_id
    )
    
    if photo.created_by != request.user:
        return HttpResponseForbidden()
    
    if not photo.trip:
        return HttpResponse(
            'Photo must be assigned to a trip before you can add any species'
        )
    
    guid = request.POST.get('guid', None)
    if guid:
        # Create species page for that animal
        selected_details = add_trip_utils.bulk_lookup(
            [guid.replace('#', '/guid/')]
        )
        if len(selected_details) == 1:
            # Now save the selected and unknown sightings
            species = species_for_freebase_details(selected_details[0])
            sighting, created = photo.trip.sightings.get_or_create(
                species = species,
                defaults = {
                    'place': photo.trip.place,
                    'note': ''
                }
            )
            photo.sightings.add(sighting)
            photo.flickr_needs_tagging = True
            photo.save()
            return HttpResponseRedirect(photo.get_absolute_url())
    
    add_unknown_text = request.POST.get('add_unknown_text', '').strip()
    if add_unknown_text:
        sighting, created = photo.trip.sightings.get_or_create(
            species_inexact = add_unknown_text,
            defaults = {
                'place': photo.trip.place,
                'note': ''
            }
        )
        photo.sightings.add(sighting)
        photo.flickr_needs_tagging = True
        photo.save()
        return HttpResponseRedirect(photo.get_absolute_url())
    
    # If we get here, we need to show search results for the 'species' string
    # normally this means the user doesn't have JavaScript enabled
    species_q = request.REQUEST.get('species', '')
    results = []
    if species_q:
        results = add_trip_utils.search(
            species_q, limit=10, place=photo.trip.place
        )[:5]
    
    return render(request, 'photos/add_species.html', {
        'photo': photo,
        'species_q': species_q,
        'results': results,
    })