def get_context_data(self, **kwargs): collections = Collection.get_collections(self.request.user) q_obj = Q(collection__in=collections) q = self.request.GET.get('q', '').strip() filter_for_archived = False if q: for word in q.split(): if word.lower() == 'archived': filter_for_archived = True else: q_obj = q_obj & Q(name__icontains=word) if filter_for_archived: q_obj = q_obj & Q(archived=True) else: q_obj = q_obj & Q(archived=False) notes = Note.objects.filter(q_obj).order_by('-updated_time').only( 'name', 'created_time', 'updated_time', 'repeat_time', 'collection__id', 'collection__name') paginator = Paginator(notes, per_page=12) p = int(self.request.GET.get('p', '1')) page = paginator.page(p) reference_time = timestamp_to_localtime(timestamp_now()) return { 'notes': page.object_list, 'page': page, 'p': p, 'q_search': q, 'paginator': paginator, 'reference_time': reference_time, 'pretty_format_timedelta': pretty_format_timedelta }
def repeat_note(repeat_policy: RepeatPolicyEntity, choice: Optional[ChoiceEntity] = None, reference_time: Optional[int] = None) -> int: """ Returns the next repeat time. :param repeat_policy: Existing repeat_policy_entity for the note. :param choice: Potentially a choice made by the user. :param reference_time: Present time. """ if reference_time is None: reference_time = timestamp_now() if choice is not None: if choice.repeat_policy is not None: repeat_policy = choice.repeat_policy elif choice.skip_to_end: # User chose to skip to the end. return reference_time - 1 if repeat_policy.typ == RepeatPolicyType.HOURLY: hours = repeat_policy.hours assert isinstance(hours, int) and hours > 0 return reference_time + hours * 3600 elif repeat_policy.typ == RepeatPolicyType.DAILY: days = repeat_policy.days assert isinstance(days, int) and days > 0 localtime = timestamp_to_localtime(reference_time) localtime += timedelta(days=days) if repeat_policy.time_of_day == TimeOfDay.MORNING: hour = settings.MORNING_HOURS else: hour = settings.EVENING_HOURS localtime = make_localtime(localtime.year, localtime.month, localtime.day, hour) return localtime_to_timestamp(localtime) elif repeat_policy.typ == RepeatPolicyType.NONE: return FAR_FUTURE else: raise ValueError(f'Invalid repeat policy: {repeat_policy.to_json()}')
def last_repeat_time_local(self) -> Optional[datetime]: if self.last_repeat_time == 0: return None return timestamp_to_localtime(self.last_repeat_time)
def repeat_time_local(self) -> datetime: return timestamp_to_localtime(self.repeat_time)
def updated_time_local(self) -> datetime: return timestamp_to_localtime(self.updated_time)
def test_time(self): now = timestamp_now() dt = timestamp_to_localtime(now) now2 = localtime_to_timestamp(dt) assert now == now2