Exemple #1
0
 def post(self, request, **kwargs):
     collection = Collection.get_collection(self.request.user,
                                            self.kwargs['id'])
     note = Note(collection=collection, created_time=timestamp_now())
     note_entity = NoteEntity.from_json(request.data)
     note.from_entity(note_entity)
     note.created_time = timestamp_now()
     note.updated_time = timestamp_now()
     note.repeat_time = repeat_note(note_entity.repeat_policy)
     note.save()
     return Response({
         'note_id': note.id,
         'note_url': reverse('view_note', args=[note.id])
     })
Exemple #2
0
 def post(self, request, **kwargs):
     note = Note.get_note_by_id(self.request.user, self.kwargs['id'])
     action = request.data['action']
     if action == 'delete':
         note.delete()
     elif action == 'archive':
         note.archived = True
         note.updated_time = timestamp_now()
         note.save()
     elif action == 'unarchive':
         note.archived = False
         note.updated_time = timestamp_now()
         note.save()
     else:
         raise ValueError(f'Invalid action: {action}')
     return Response({})
Exemple #3
0
    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
        }
Exemple #4
0
 def post(self, request, **kwargs):
     note = Note.get_note_by_id(self.request.user, self.kwargs['id'])
     note_entity = NoteEntity.from_json(request.data)
     note.from_entity(note_entity)
     note.updated_time = timestamp_now()
     note.repeat_time = repeat_note(note_entity.repeat_policy)
     note.save()
     return Response({})
Exemple #5
0
 def get_active_notes(collection: Collection, reference_time: Optional[int] = None) -> QuerySet:
     """
     Authorization-safe getter for notes. Gets all active notes for a collection.
     :param collection: Collection to query.
     :param reference_time: Reference time. If not passed, it is now.
     """
     if reference_time is None:
         reference_time = timestamp_now()
     return Note.objects.filter(
         collection=collection, archived=False, repeat_time__lt=reference_time
     ).order_by('repeat_time')
    def get(self, request, **kwargs):
        # Is it a diary?
        is_diary = ('diary' in self.request.GET)

        # Get collection
        collection = Collection.get_collection(self.request.user,
                                               self.kwargs['id'])

        # Handle name
        name = ''
        if is_diary:
            dt_now = localtime_now()
            name = dt_now.strftime('Diary, %A, %Y-%m-%d')

        # TODO: Configurable default repeat and learn policies
        repeat_policy = RepeatPolicyEntity(RepeatPolicyType.DAILY,
                                           days=1,
                                           time_of_day=TimeOfDay.MORNING)
        if is_diary:
            repeat_policy = RepeatPolicyEntity(RepeatPolicyType.DAILY,
                                               days=365,
                                               time_of_day=TimeOfDay.MORNING)
        learn_policy = LearnPolicyEntity(LearnPolicyType.EXPONENTIAL)

        note_entity = NoteEntity(name,
                                 ContentsEntity([MarkdownSectionEntity("")]),
                                 repeat_policy=repeat_policy,
                                 learn_policy=learn_policy)
        note = Note(collection=collection, created_time=timestamp_now())
        note.from_entity(note_entity)
        note.created_time = timestamp_now()
        note.updated_time = timestamp_now()
        note.repeat_time = repeat_note(note_entity.repeat_policy)
        note.save()

        return redirect('edit_note', id=note.id)
Exemple #7
0
    def post(self, request, **kwargs):
        note_id = request.data['note_id']
        choice = request.data['choice']

        note = Note.get_note_by_id(self.request.user, note_id)
        note_entity = note.to_entity()
        choice_entity = ChoiceEntity.from_json(choice)
        note.last_repeat_time = timestamp_now()
        note.repeat_time = repeat_note(note_entity.repeat_policy,
                                       choice_entity)
        if choice_entity.overwrite_repeat_policy:
            assert choice_entity.repeat_policy is not None
            note_entity.repeat_policy = choice_entity.repeat_policy
            note.from_entity(note_entity,
                             load_name=False,
                             load_contents=False,
                             load_metadata=True)
        note.save()
        return Response({})
Exemple #8
0
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()}')
Exemple #9
0
 def get_context_data(self, **kwargs):
     collections = Collection.get_collections(self.request.user)
     reference_time = timestamp_now()
     caches = [CollectionCache(c, reference_time) for c in collections]
     return {'ccs': list(zip(collections, caches))}
Exemple #10
0
 def test_time(self):
     now = timestamp_now()
     dt = timestamp_to_localtime(now)
     now2 = localtime_to_timestamp(dt)
     assert now == now2