Example #1
0
 def initialize(self, date=None):
     if not date:
         day_service = DayService(user=self.__user)
         date = day_service.get_current_date()
     dates = self.get_initialization_days(date)
     gap_dates = self.get_gap_days(dates)
     data = {
         'date':
         date.strftime('%Y-%m-%d'),
         'pooling':
         self.__configuration.pooling,
         'totalStepsArray': [self.get_steps(date) for date in dates],
         'preStepsMatrix': [{
             'steps': self.get_pre_steps(date)
         } for date in dates],
         'postStepsMatrix': [{
             'steps': self.get_post_steps(date)
         } for date in dates],
         'PriorAntiMatrix': [{
             'priorAnti':
             self.get_all_anti_sedentary_treatments(date)
         } for date in gap_dates],
         'DelieverMatrix': [{
             'walking': self.get_received_messages(date)
         } for date in gap_dates]
     }
     self.make_request('initialize', data=data)
     self.__configuration.service_initialized_date = date
     self.__configuration.save()
Example #2
0
    def send_notification(self, day=False, test=False):
        if not day:
            service = DayService(user=self.__user)
            day = service.get_current_date()
        morning_message, _ = self.get_or_create(day)

        if not self.__configuration.enabled:
            raise MorningMessageService.NotEnabled()

        serialized = MorningMessageSerializer(morning_message).data
        serialized['type'] = 'morning-message'

        if not serialized['text']:
            del serialized['text']
        if not serialized['anchor']:
            del serialized['anchor']

        if serialized['notification']:
            serialized['body'] = serialized['notification']
            del serialized['notification']

        push_service = PushMessageService(user=self.__user)
        message = push_service.send_notification(
            body=morning_message.notification,
            title='Morning check-in',
            data=serialized,
            collapse_subject='morning-message',
            send_message_id_only=True)
        morning_message.add_context(message)
        return message
Example #3
0
def initialize_and_update(username):
    configuration = Configuration.objects.get(user__username = username)
    day_service = DayService(user=configuration.user)
    walking_suggestion_service = WalkingSuggestionService(configuration=configuration)
    
    date_joined = day_service.get_date_at(configuration.user.date_joined)
    today = day_service.get_current_date()
    days_to_go_back = (today - date_joined).days
    date_range = [today - timedelta(days=offset+1) for offset in range(days_to_go_back)]

    while len(date_range):
        initialize_date = date_range.pop()
        try:
            walking_suggestion_service.get_initialization_days(initialize_date)
            break
        except WalkingSuggestionService.UnableToInitialize:
            pass
    
    walking_suggestion_service.initialize(initialize_date)
    NightlyUpdate.objects.filter(user = configuration.user).delete()
    
    while len(date_range):
        update_date = date_range.pop()
        walking_suggestion_service.update(update_date)
        NightlyUpdate.objects.create(
            user = configuration.user,
            day = update_date,
            updated = True
        )
Example #4
0
def daily_update(username):
    service = ParticipantService(username=username)
    day_service = DayService(username=username)
    yesterday = day_service.get_current_date() - timedelta(days=1)
    service.update(yesterday)
    if not service.participant.study_start_date:
        service.participant.study_start_date = service.participant.get_study_start_date()
        service.participant.save()
    update_location_categories(username)
Example #5
0
 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)
         ]
Example #6
0
 def send_message(self):
     if not self.user.is_active:
         raise Configuration.ConfigurationDisabled(
             'Configuration user disabled')
     if not self.enabled:
         raise Configuration.ConfigurationDisabled(
             'Configuration task disabled')
     if self.message:
         raise Configuration.MessageAlreadySent(
             'Will not send message twice')
     day_service = DayService(user=self.user)
     current_date = day_service.get_current_date()
     if current_date < self.closeout_date:
         raise Configuration.BeforeCloseoutDate(
             'Will not send before closeout date')
     sms_service = SMSService(user=self.user)
     self.message = sms_service.send(CLOSEOUT_MESSAGE)
     self.save()
     self.disable()
Example #7
0
 def get_current_date(self):
     service = DayService(user=self.user)
     return service.get_current_date()
Example #8
0
 def current_date(self):
     if not hasattr(self, '_current_date'):
         service = DayService(self.user)
         self._today = service.get_current_date()
     return self._today
Example #9
0
 def get_datetime_for_today(self):
     service = DayService(user = self.user)
     today = service.get_current_date()
     return self.get_datetime_on(today)