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 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 } })
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))}