def is_forecast_in_past(self, forecast): day_service = DayService(user = self.__user) end_of_day = day_service.get_end_of_day(forecast.date) if end_of_day < timezone.now(): return True else: return False
def get_study_end_datetime(self): study_start_date = self.get_study_start_date() if study_start_date and self.study_length: end_date = self.study_start + timedelta(days=self.study_length) service = DayService(user=self.user) return service.get_end_of_day(end_date) else: return None
def update_message_receipts(self, date): if not self.user: return day_service = DayService(user=self.user) messages = PushMessage.objects.filter( recipient=self.user, created__gte=day_service.get_start_of_day(date), created__lte=day_service.get_end_of_day(date)).all() for message in messages: message.update_message_receipts()
def __get_time_range(self, time_range): if time_range not in self.TIME_RANGES: raise RuntimeError('time range not found') else: service = DayService(user=self.user) today = service.get_current_date() start_date = today - timedelta(days=time_range.offset) return [ service.get_start_of_day(start_date), service.get_end_of_day(today) ]
def export_activity_logs(username, filename=None, directory=None, start=None, end=None): if not directory: directory = './' if not filename: filename = '%s.activity_logs.csv' % (username) activity_log_query = ActivityLog.objects.filter( user__username=username ) \ .order_by('start') \ .prefetch_related('type') day_service = DayService(username=username) if start: start_datetime = day_service.get_start_of_day(start) activity_log_query = activity_log_query.filter( start__gte=start_datetime) if end: end_datetime = day_service.get_end_of_day(end) activity_log_query = activity_log_query.filter(start__lte=end_datetime) rows = [] headers = [ 'Participant ID', 'Activity ID', 'Study Day', 'Date', 'Start Time', 'Timezone', 'Activity Type ID', 'Activity Type Title', 'Activity Duration', 'Activity Vigorous' ] rows.append(headers) for activity_log in activity_log_query.all(): rows.append([ username, activity_log.id, 'study day', activity_log.start.astimezone( pytz.timezone('America/Los_Angeles')).strftime('%Y-%m-%d'), activity_log.start.astimezone(pytz.timezone( 'America/Los_Angeles')).strftime('%Y-%m-%d %H:%M:%S'), 'timezone', activity_log.type.id, activity_log.type.title, activity_log.duration, activity_log.vigorous ]) _file = open(path.join(directory, filename), 'w') writer = csv.writer(_file) writer.writerows(rows) _file.close()
def suggestion_time_category_available_at(self, time): category = self.suggestion_time_category_at(time) day_service = DayService(user=self.__user) query = WalkingSuggestionDecision.objects.filter( user=self.__user, test=False, time__range=[ day_service.get_start_of_day(time), day_service.get_end_of_day(time) ]) tags = [decision.category for decision in query.all()] if category in tags: raise self.Unavailable('Time already taken') else: return category
def get_adherence_during(self, start, end): if not self.user: return [] metrics = {} adherence_metrics = AdherenceMetric.objects.filter( user=self.user, date__range=[start, end]).all() for metric in adherence_metrics: if metric.date not in metrics: metrics[metric.date] = {} metrics[metric.date][metric.category] = metric.value messages = {} day_service = DayService(user=self.user) adherence_messages = AdherenceMessage.objects.filter( user=self.user, created__range=[ day_service.get_start_of_day(start), day_service.get_end_of_day(end) ]).all() for message in adherence_messages: message_date = day_service.get_date(message.created) if message_date not in messages: messages[message_date] = [] messages[message_date].append({ 'category': message.category, 'body': message.body }) summaries = [] _dates = [ end - timedelta(days=offset) for offset in range((end - start).days + 1) ] for _date in _dates: _metrics = {} if _date in metrics: _metrics = metrics[_date] _messages = [] if _date in messages: _messages = messages[_date] summaries.append({ 'date': _date, 'metrics': _metrics, 'messages': _messages }) return summaries
def was_updated_on(self, date): day_service = DayService(user=self.__user) start = day_service.get_start_of_day(date) end = day_service.get_end_of_day(date) return self.account.was_updated_between(start, end)