Ejemplo n.º 1
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
        }
Ejemplo n.º 2
0
 def post(self, request, **kwargs):
     collection = Collection.get_collection(self.request.user,
                                            self.kwargs['id'])
     action = request.data['action']
     if action == 'delete':
         collection.delete()
     else:
         raise ValueError(f'Invalid action: {action}')
     return Response({})
Ejemplo n.º 3
0
 def post(self, request, **kwargs):
     collection = Collection.create_collection(request.data['name'],
                                               self.request.user)
     return Response({
         'collection_id':
         collection.id,
         'collection_url':
         reverse('list_collection', args=[collection.id])
     })
Ejemplo n.º 4
0
def import_collections(filename: str):
    with open(filename, 'rt') as f:
        collections_json = json.load(f)
        for collection_json in collections_json:
            try:
                collection = Collection.objects.get(id=collection_json['id'])
                print(f'Found existing collection: {collection.id}')
            except:
                collection = Collection(id=collection_json['id'])
                print(f'New collection: {collection.id}')
            collection.name = collection_json['name']
            print(f'... name={collection_json["name"]}')
            collection.save()
Ejemplo n.º 5
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])
     })
Ejemplo n.º 6
0
    def get(self, request, **kwargs):
        """ This is used for listing. """

        # Get all collections
        collections = Collection.get_collections(self.request.user).only('id')

        # Get all active notes
        active_notes = []
        for collection in collections:
            notes = Note.get_active_notes(collection).only('id', 'repeat_time')
            active_notes.extend(notes)

        if len(active_notes) == 0:
            return Response({'num_active_notes': 0})

        # Sort active notes by repeat_time
        active_notes.sort(key=lambda x: x.repeat_time)

        # Fetch the top note
        note = Note.objects.get(id=active_notes[0].id)
        note_entity = note.to_entity()

        # Fetch choices
        choices = create_note_choices(note_entity.repeat_policy,
                                      note_entity.learn_policy)

        # Get URLs
        url_note_action = reverse("api_note_action", args=[note.id])
        url_list_collection = reverse("list_collection",
                                      args=[note.collection.id])
        url_note_edit = reverse("edit_note", args=[note.id])

        return Response({
            'num_active_notes': len(active_notes),
            'active_note': note_entity.to_json(),
            'choices': [c.to_json() for c in choices],
            'urls': {
                'url_note_action': url_note_action,
                'url_list_collection': url_list_collection,
                'url_note_edit': url_note_edit
            }
        })
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
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))}