def update_activity_log_from_fitbit_activity(sender, instance, *args, **kwargs): fitbit_activity = instance fitbit_service = FitbitService(account=fitbit_activity.account) for user in fitbit_service.get_users(): if fitbit_activity.duration > 10: service = FitbitActivityLogService(user=user) try: activity_log = service.get_activity_log(fitbit_activity) service.update_activity_log(activity_log, fitbit_activity) except FitbitActivityLogService.NoActivityLog: service.create_activity_log(fitbit_activity)
def update_timezone_from_fitbit(sender, instance, *args, **kwargs): fitbit_service = FitbitService(account=instance.account) for user in fitbit_service.get_users(): day, _ = Day.objects.update_or_create( user = user, date = instance.date, defaults = { "timezone": instance._timezone } ) day_service = DayService(user=user) current_date = day_service.get_current_date() if current_date == instance.date: timezone_updated.send(User, username=user.username)
def fitbit_days_worn(self): try: fitbit_service = FitbitService(user=self.user) except FitbitService.NoAccount: return 0 return FitbitDay.objects.filter(account=fitbit_service.account, wore_fitbit=True).count()
def test_updates_day(self, fitbit_day_update, get_timezone): FitbitService.create_account( user = self.user ) response = self.client.get(reverse('activity-summary-day-update', kwargs={ 'day': '2018-12-10' })) fitbit_day_update.assert_called() self.assertEqual(response.status_code, 200) self.assertEqual(response.data['steps'], 0) self.assertEqual(response.data['miles'], 0) self.assertEqual(response.data['minutes'], 0) self.assertEqual(response.data['moderateMinutes'], 0) self.assertEqual(response.data['vigorousMinutes'], 0)
def fitbit_account(request): try: service = FitbitService(user=request.user) except FitbitService.NoAccount: response = Response({}, status=status.HTTP_404_NOT_FOUND) response['Cache-Control'] = 'no-cache' return response serializer = FitbitAccountSerializer(service.account) return Response(serializer.data, status=status.HTTP_200_OK)
def setUp(self): self.user = User.objects.create(username="******") self.account = FitbitService.create_account( user = self.user ) self.fitbit_day = FitbitDay.objects.create( account = self.account, date = date(2019, 1, 6) )
def reload_fitbit_data(modeladmin, request, queryset): for participant in queryset.all(): try: account = FitbitService.get_account(user=participant.user) update_all_fitbit_data.apply_async( kwargs={'fitbit_user': account.fitbit_user}) messages.add_message( request, messages.SUCCESS, 'Update fitbit account %s for %s' % (account.fitbit_user, participant.heartsteps_id)) except FitbitService.NoAccount: messages.add_message( request, messages.ERROR, '%s has no fitbit account' % (participant.heartsteps_id))
def subscribe_to_fitbit(username): # TODO: remove print debugging try: service = FitbitService(fitbit_user=username) print('fitbit service: ', service) fitbit_client = FitbitClient(account=service.account) print('fitbit client: ', fitbit_client) fitbit_client.subscriptions_update() if not fitbit_client.is_subscribed(): fitbit_client.subscribe() except FitbitService.NoAccount: print('FitbitService.NoAccount exception') return False
def get_fitbit_activity_by_date(username, days): fitbit_activity_by_date = {} for day in days: fitbit_activity_by_date[day.date] = { 'step_count': 0, 'completely_updated': 0, 'wore_fitbit': 0, 'minutes_worn': 0, 'update_count': 0 } try: fitbit_service = FitbitService(username=username) fitbit_account = fitbit_service.account fitbit_days = FitbitDay.objects.filter(account=fitbit_account, date__gte=days[0].date, date__lte=days[-1].date).all() for _fitbit_day in fitbit_days: fitbit_activity_by_date[ _fitbit_day.date]['step_count'] = _fitbit_day.step_count fitbit_activity_by_date[_fitbit_day.date][ 'completely_updated'] = _fitbit_day.completely_updated fitbit_activity_by_date[ _fitbit_day.date]['wore_fitbit'] = _fitbit_day.wore_fitbit fitbit_activity_by_date[ _fitbit_day.date]['minutes_worn'] = _fitbit_day.minutes_worn account_updates = FitbitAccountUpdate.objects.filter( account=fitbit_account ).order_by('created') \ .all() subscription_updates = FitbitSubscriptionUpdate.objects.filter( subscription__fitbit_account=fitbit_account ).order_by('created') \ .all() account_activity = list(account_updates) + list(subscription_updates) account_activity.sort(key=lambda x: x.created) for day in days: count = 0 while account_activity and account_activity[ 0].created >= day.start and account_activity[ 0].created <= day.end: account_activity.pop(0) count += 1 fitbit_activity_by_date[day.date]['update_count'] = count except FitbitService.NoAccount: pass return fitbit_activity_by_date
def unauthorize_fitbit_account(username): service = FitbitService(username=username) client = FitbitClient(account=service.account) client.unsubscribe() client.subscriptions_update() service.remove_credentials()
def fitbit_day_updates_day(sender, instance, *args, **kwargs): fitbit_day = instance fitbit_service = FitbitService(account=instance.account) for user in fitbit_service.get_users(): for day in list_and_create_day(user, fitbit_day.date): day.update_from_fitbit()
def get_fitbit_days_before_date(self, date): fitbit_service = FitbitService(user=self.__user) return FitbitDay.objects.filter( account=fitbit_service.account, date__range=[self.__user.date_joined, date]).order_by('-date').all()
def fitbit_authorized(self): if self.fitbit_account: service = FitbitService(account=self.fitbit_account) return service.is_authorized() return False