Esempio n. 1
0
 def test_location_save_parses_input_ignores_garbage_mixed(self):
     location = Location(user_input='aoeua87aoe349a8712b8qjk37a')
     location.save()
     self.assertIsNone(location.city)
     self.assertIsNone(location.state)
     self.assertIsNone(location.postal_code)
     self.assertIsNone(location.latitude)
     self.assertIsNone(location.longitude)
Esempio n. 2
0
 def test_location_save_parses_input_ignores_garbage_letters(self):
     location = Location(user_input='enutharocegusahosecsahkm')
     location.save()
     self.assertIsNone(location.city)
     self.assertIsNone(location.state)
     self.assertIsNone(location.postal_code)
     self.assertIsNone(location.latitude)
     self.assertIsNone(location.longitude)
Esempio n. 3
0
 def test_location_save_parses_input_ignores_garbage_numbers(self):
     location = Location(user_input='12873498712983749182')
     location.save()
     self.assertIsNone(location.city)
     self.assertIsNone(location.state)
     self.assertIsNone(location.postal_code)
     self.assertIsNone(location.latitude)
     self.assertIsNone(location.longitude)
Esempio n. 4
0
 def test_location_save_parses_input_ignores_lat_long_numeric(self):
     location = Location(user_input='41.2342, -76.2928')
     location.save()
     # Because latitude and longitude are now parsed and geocoded
     # from the client during form input instead, doing a save on the
     # location model does not populate the individual fields.
     self.assertEqual(location.latitude, None)
     self.assertEqual(location.longitude, None)
Esempio n. 5
0
 def test_location_save_parses_input_ignores_lat_long_numeric(self):
     location = Location(user_input='41.2342, -76.2928')
     location.save()
     # Because latitude and longitude are now parsed and geocoded
     # from the client during form input instead, doing a save on the
     # location model does not populate the individual fields.
     self.assertEqual(location.latitude, None)
     self.assertEqual(location.longitude, None)
Esempio n. 6
0
 def test_location_save_parses_input_ignores_garbage_mixed(self):
     location = Location(user_input='aoeua87aoe349a8712b8qjk37a')
     location.save()
     self.assertIsNone(location.street)
     self.assertIsNone(location.city)
     self.assertIsNone(location.state)
     self.assertIsNone(location.postal_code)
     self.assertIsNone(location.latitude)
     self.assertIsNone(location.longitude)
Esempio n. 7
0
 def test_location_save_parses_input_ignores_garbage_numbers(self):
     location = Location(user_input='12873498712983749182')
     location.save()
     self.assertIsNone(location.street)
     self.assertIsNone(location.city)
     self.assertIsNone(location.state)
     self.assertIsNone(location.postal_code)
     self.assertIsNone(location.latitude)
     self.assertIsNone(location.longitude)
Esempio n. 8
0
 def test_location_save_parses_input_ignores_garbage_letters(self):
     location = Location(user_input='enutharocegusahosecsahkm')
     location.save()
     self.assertIsNone(location.street)
     self.assertIsNone(location.city)
     self.assertIsNone(location.state)
     self.assertIsNone(location.postal_code)
     self.assertIsNone(location.latitude)
     self.assertIsNone(location.longitude)
Esempio n. 9
0
 def clean_location(self):
     if not 'location' in self.cleaned_data:
         return None
     user_text = self.cleaned_data['location']
     # Look for a Location record with the user input text.
     locations = Location.objects.filter(user_input=user_text)
     if locations.count() > 0:
         # The first one is OK because any duplicates are equivalent.
         location = locations[0]
     else:
         # Create a new location record.
         location = Location(user_input=user_text)
         location.save()
     return location
Esempio n. 10
0
 def clean_location(self):
     if not 'location' in self.cleaned_data:
         return None
     user_text = self.cleaned_data['location']
     # Look for a Location record with the user input text.
     locations = Location.objects.filter(user_input=user_text)
     if locations.count() > 0:
         # The first one is OK because any duplicates are equivalent.
         location = locations[0]
     else:
         # Create a new location record.
         location = Location(user_input=user_text)
         location.save()
     return location
 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()
Esempio n. 13
0
 def test_location_save_parses_input_city_state(self):
     location = Location(user_input='Framingham, MA')
     location.save()
     self.assertEqual(location.city, 'Framingham')
     self.assertEqual(location.state, 'MA')
Esempio n. 14
0
 def test_location_save_parses_input_zip_plus_four(self):
     location = Location(user_input='01701-2699')
     location.save()
     self.assertEqual(location.postal_code, '01701-2699')
Esempio n. 15
0
 def test_location_save_parses_input_city_state(self):
     location = Location(user_input='Framingham, MA')
     location.save()
     self.assertEqual(location.city, 'Framingham')
     self.assertEqual(location.state, 'MA')
Esempio n. 16
0
 def test_location_save_parses_input_address_city_state(self):
     location = Location(user_input='180 Hemenway Road, Framingham, MA')
     location.save()
     self.assertEqual(location.street, '180 Hemenway Road')
     self.assertEqual(location.city, 'Framingham')
     self.assertEqual(location.state, 'MA')
Esempio n. 17
0
 def test_location_save_parses_input_canadian_postal_code(self):
     location = Location(user_input='E7B 1A3')
     location.save()
     self.assertEqual(location.postal_code, 'E7B 1A3')
Esempio n. 18
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)