def get_location_context(self): location = self.get_location() if location: location_service = LocationService(self.user) return location_service.categorize_location( latitude=location.latitude, longitude=location.longitude) else: return Place.OTHER
def test_get_past_timezone(self, timezone_at): service = LocationService(self.user) ten_days_ago = timezone.now() - timedelta(days=10) tz = service.get_timezone_on(ten_days_ago) self.assertEqual(tz.zone, 'US/Eastern') timezone_at.assert_called_with( lat = float(20), lng = float(40) )
def test_user_not_at_office(self): user = User.objects.create(username="******") kpwri = make_kpwri_location() kpwri.type = 'office' kpwri.user = user kpwri.save() place = make_space_needle_location() location_service = LocationService(user) place_type = location_service.categorize_location(place.latitude, place.longitude) self.assertEqual(place_type, 'other')
def is_forecast_location_accurate(self, forecast): if not forecast.latitude or not forecast.longitude: return False location_service = LocationService(user = self.__user) current_location = location_service.get_current_location() current_coords = (current_location.latitude, current_location.longitude) forecast_coords = (forecast.latitude, forecast.longitude) distance = geopy_distance(current_coords, forecast_coords) if distance.km <= 50: return True else: return False
def update_location(self): location_content_type = ContentType.objects.get_for_model(Location) existing_decision_locations = DecisionContext.objects.filter( decision=self.decision, content_type=location_content_type).delete() try: location_service = LocationService(self.user) location = location_service.get_location_on(self.time) DecisionContext.objects.create(decision=self.decision, content_object=location) self.location = location return location except LocationService.UnknownLocation: return None
def test_use_user_locations_only(self): # Add location that simulates other participant's accounts other_user = User.objects.create(username="******") Location.objects.create( user = other_user, latitude = 10, longitude = 10, time = timezone.now() - timedelta(days=4), source = "other user" ) service = LocationService(self.user) three_days_ago = timezone.now() - timedelta(days=3) location = service.get_location_on(three_days_ago) self.assertEqual(location.source, '7 days ago')
def get_location(self): if hasattr(self, 'location'): return self.location location_content_type = ContentType.objects.get_for_model(Location) existing_decision_locations = DecisionContext.objects.filter( decision=self.decision, content_type=location_content_type).all() if len(existing_decision_locations) > 0: self.location = existing_decision_locations[0].content_object return self.location try: location_service = LocationService(self.user) location = location_service.get_location_near(self.decision.time) DecisionContext.objects.create(decision=self.decision, content_object=location) self.location = location return location except LocationService.UnknownLocation: return None
def test_get_past_location(self): service = LocationService(self.user) three_days_ago = timezone.now() - timedelta(days=3) location = service.get_location_on(three_days_ago) self.assertEqual(location.source, '7 days ago') eight_days_ago = timezone.now() - timedelta(days=8) location = service.get_location_on(eight_days_ago) self.assertEqual(location.source, '20 days ago') throws_unknown_location_error = False try: twenty_one_days_ago = timezone.now() - timedelta(days=21) location = service.get_location_on(twenty_one_days_ago) except LocationService.UnknownLocation: throws_unknown_location_error = True self.assertTrue(throws_unknown_location_error)
def get_daily_forecast(self, latitude, longitude, date): timezone = LocationService.get_timezone_at(latitude=latitude, longitude=longitude) dt = datetime(date.year, date.month, date.day, tzinfo=timezone) url = self.make_url( latitude=latitude, longitude=longitude, datetime=dt, exclude=['hourly', 'currently', 'alerts', 'minutely', 'flags']) response_data = self.make_request(url, 'DarkSky: get weekly forecast') tz = pytz.timezone(response_data['timezone']) return self.parse_daily_forecast( data=response_data['daily']['data'][0], timezone=tz)
def get_default_timezone(self): service = LocationService(user=self.__user) return service.get_home_timezone()
def test_get_last_location(self): service = LocationService(self.user) location = service.get_last_location() self.assertEqual(location.source, '2 days ago')
def reset_test_participants(date_joined=None, number_of_days=9): current_year = date.today().strftime('%Y') study, _ = Study.objects.get_or_create(name='Test') cohort, _ = Cohort.objects.get_or_create( name = "test", study = study ) try: participant = Participant.objects.get(heartsteps_id = 'test-new') participant.delete() except Participant.DoesNotExist: pass Participant.objects.create( heartsteps_id = 'test-new', enrollment_token = 'test-new1', birth_year = current_year, cohort = cohort ) try: participant = Participant.objects.get(heartsteps_id = 'test') participant.delete() except Participant.DoesNotExist: pass participant = Participant.objects.create( heartsteps_id = 'test', enrollment_token = 'test-test', birth_year = current_year, cohort = cohort ) participant_service = ParticipantService(participant=participant) participant_service.initialize() participant = Participant.objects.get(heartsteps_id='test') user = participant.user user.is_staff = True user.save() ContactInformation.objects.update_or_create( user = user, defaults = { 'name': 'Testy test', 'email': '*****@*****.**', 'phone': '5555555555' } ) fitbit_account, _ = FitbitAccount.objects.get_or_create( fitbit_user = '******' ) FitbitAccountUser.objects.update_or_create( user = user, defaults = { 'account': fitbit_account } ) Place.objects.create( user = user, type = Place.HOME, address = 'Space Needle, Seattle, Washington, United States of America', latitude = 47.6205, longitude = -122.34899999999999 ) Place.objects.create( user = user, type = Place.WORK, address = '1730 Minor Avenue, Seattle, Washington, United States of America', latitude = 47.6129, longitude = -122.327 ) ws_configuration, _ = WalkingSuggestionConfiguration.objects.update_or_create( user=user, defaults = { 'enabled': True } ) ws_configuration.set_default_walking_suggestion_times() ReflectionTime.objects.update_or_create( user = user, defaults = { 'day': 'sunday', 'time': '19:00' } ) # Clear and re-create activity data Day.objects.filter(user=user).all().delete() FitbitDay.objects.filter(account=fitbit_account).all().delete() location_service = LocationService(user = user) tz = location_service.get_home_timezone() current_dt = location_service.get_home_current_datetime() if date_joined: user.date_joined = date_joined else: user.date_joined = current_dt - timedelta(days=number_of_days) user.save() date_joined = date( user.date_joined.year, user.date_joined.month, user.date_joined.day ) dates_to_create = [date_joined + timedelta(days=offset) for offset in range(number_of_days)] dates_to_create.append(date(current_dt.year, current_dt.month, current_dt.day)) for _date in dates_to_create: day, _ = FitbitDay.objects.update_or_create( account = fitbit_account, date = _date, defaults = { '_timezone': tz.zone, 'step_count': 2000, '_distance': 2, 'wore_fitbit': True } ) # Add heartrate for every minute of the day dt = day.get_start_datetime() day_end = dt.replace(hour=20) while dt < day_end: FitbitMinuteHeartRate.objects.create( account = fitbit_account, time = dt, heart_rate = 1234 ) dt = dt + timedelta(minutes=1) day.save() Day.objects.update_or_create( user = user, date = _date, defaults = { 'timezone': tz.zone } )
def _get_location_on(self, date): try: location_service = LocationService(user=self.__user) return location_service.get_location_on(date) except LocationService.UnknownLocation: return self._get_home_location()
def _get_home_location(self): try: location_service = LocationService(user=self.__user) return location_service.get_home_location() except LocationService.UnknownLocation: raise WeatherService.UnknownLocation('Unknown location')
def unknown_location(dt): raise LocationService.UnknownLocation()