def _create_dummy_sighting(self, username):
     user = User.objects.get(username=username)
     year = datetime.now().year
     start = datetime(year, 1, 1, 00, 00)
     end = datetime(year, 12, 31, 23, 59)
     created = self._random_datetime(start, end)
     identification = 'Acer saccharum'
     location = Location(user_input='Boston, Ma.')
     location.latitude = 41.73 + random.uniform(-0.1, 0.1)
     location.longitude = 71.43 + random.uniform(-0.1, 0.1)
     location.save()
     self.stdout.write('_create_dummy_sighting: %s, %s, %s, %s' %
         (user, created, identification, location))
     sighting = Sighting(user=user, created=created,
         identification=identification, location=location)
     sighting.save()
 def _create_dummy_sighting(self, username):
     user = User.objects.get(username=username)
     year = datetime.now().year
     start = datetime(year, 1, 1, 00, 00)
     end = datetime(year, 12, 31, 23, 59)
     created = self._random_datetime(start, end)
     identification = 'Acer saccharum'
     location = Location(user_input='Boston, Ma.')
     location.latitude = 41.73 + random.uniform(-0.1, 0.1)
     location.longitude = 71.43 + random.uniform(-0.1, 0.1)
     location.save()
     self.stdout.write('_create_dummy_sighting: %s, %s, %s, %s' %
                       (user, created, identification, location))
     sighting = Sighting(user=user,
                         created=created,
                         identification=identification,
                         location=location)
     sighting.save()
Example #3
0
def sightings_view(request):
    """View for the sightings collection: showing a list of recent sightings
    (GET) as well as handling adding a new sighting (the POST action from
    the new-sighting form).
    """
    MAX_RECENT_SIGHTINGS = 50

    if request.method == 'POST':
        # Handle posting a new sighting to the sightings collection.
        if request.user.is_authenticated():
            form = NewSightingForm(request.POST)
            if form.is_valid():
                location = Location(user_input=form.cleaned_data['location'],
                                    latitude=form.cleaned_data['latitude'],
                                    longitude=form.cleaned_data['longitude'])
                location.save()

                identification = form.cleaned_data['identification']
                notes = form.cleaned_data['notes']
                location_notes = form.cleaned_data['location_notes']
                sighting = Sighting(user=request.user,
                                    identification=identification, title='',
                                    notes=notes, location=location,
                                    location_notes=location_notes)

                sighting.save()

                #print 'saved:', sighting
                photo_ids = request.POST.getlist('sightings_photos')
                #print 'Got sightings photos: ', sighting_photos
                photos = ScreenedImage.objects.filter(id__in=photo_ids)
                sighting.photos.add(*photos)
                sighting.save()

                done_url = (reverse('ps-new-sighting-done') + '?s=%d'
                            % sighting.id)
                return HttpResponseRedirect(done_url)
            else:
                # Present the new-sighting form again for input correction.
                return _new_sighting_form_page(request, form)
        else:
            return HttpResponse(status=401)   # 401 Unauthorized
    elif request.method == 'GET':
        # Return a representation of the collection of sightings.
        sightings_queryset = Sighting.objects.all().select_related().\
            prefetch_related('location')[:MAX_RECENT_SIGHTINGS]
        sightings = []
        for sighting in sightings_queryset:
            sightings.append({
                'id': sighting.id,
                'identification': sighting.identification,
                'location': sighting.location,
                'user': _user_name(sighting.user),
                'created': sighting.created.strftime("%A, %B %e"),
            })

        return render_to_response('sightings.html', {
                    'sightings': sightings
               }, context_instance=RequestContext(request))
    else:
        # For an unsupported HTTP method, return a Bad Request response.
        return HttpResponse(status=400)