diff options
Diffstat (limited to 'apps/notes/views.py')
-rw-r--r-- | apps/notes/views.py | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/apps/notes/views.py b/apps/notes/views.py index 245e2ab..280020a 100644 --- a/apps/notes/views.py +++ b/apps/notes/views.py @@ -90,6 +90,7 @@ class NoteDetailView(LoggedInViewWithUser, AjaxableResponseMixin, UpdateView): return context def form_valid(self, form): + print(self.request.META) if "trash" in self.request.POST: form.instance.notebook = Notebook.include_trash.get(owner=self.request.user, name="Trash") self.object = form.save() @@ -155,6 +156,8 @@ class NoteTagView(BaseListView): context = super().get_context_data(**kwargs) #context['notes_list'] = Note.objects.filter(owner=self.request.user) context['tags'] = LuxTag.objects.filter(slug__in=self.tag_list) + context['tag_list'] = LuxTag.objects.filter(note__owner=self.request.user).annotate(note_count=Count('note')) + context['notebook_list'] = Notebook.objects.filter(owner=self.request.user).annotate(note_count=Count('note')) return context @@ -174,23 +177,48 @@ class NotebookListView(LoggedInViewWithUser, CreateView): def form_valid(self, form): form.instance.owner = self.request.user - self.object = form.save() + if "create" in self.request.POST: + self.object = form.save() + messages.info(self.request, 'The notebook %s has been created, you may edit it below.' % (self.object.name)) + return redirect('notebooks:list') return super(NotebookListView, self).form_valid(form) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - NotebookFormSet = modelformset_factory(Notebook, form=NotebookForm, extra=0) - context['notebook_form_list'] = NotebookFormSet(queryset=Notebook.objects.filter(owner=self.request.user).select_related().annotate(note_count=Count('note'))) - #context['notebook_list'] = Notebook.objects.filter(owner=self.request.user).exclude(name="Trash").select_related().annotate(note_count=Count('note')) - #context['notes_list'] = Note.objects.filter(owner=self.request.user).select_related() + forms = [] + for n in Notebook.objects.filter(owner=self.request.user).prefetch_related('note_set').prefetch_related('note_set__tags').annotate(note_count=Count('note')): + #notes = [] + #tags = set(tag for tag in notes.tags_set.all().distinct() for notes in n.note_set.all()) + #n.tag_list = tags + n.tag_list = LuxTag.objects.filter(note__in=n.note_set.all()).distinct()[:3] + forms.append(NotebookForm(instance=n)) + context['notebook_form_list'] = forms + #context['notebook_list'] = Notebook.objects.filter(owner=self.request.user).select_related().annotate(note_count=Count('note')) return context - def get_success_url(self): - return reverse_lazy('notebooks:detail', kwargs={'slug': self.object.slug}) +class NotebookUpdateView(LoggedInViewWithUser, UpdateView): + model = Notebook + form_class = NotebookForm + template_name = 'notes/notebook_list.html' -class NotebookDetailView(BaseDetailView): + def get_queryset(self): + if not self.request.user.is_anonymous: + return Notebook.objects.filter(owner=self.request.user) + + def form_valid(self, form): + form.instance.owner = self.request.user + self.object = form.save() + data = { + 'notebook': {'name': self.object.name, 'color': self.object.color_rgb, 'permalink': self.object.slug} + } + return JsonResponse(data, safe=True) + + +class NotebookDetailView(LoggedInViewWithUser, AjaxableResponseMixin, UpdateView): model = Notebook + form_class = NotebookForm + template_name = 'notes/notebook_detail.html' def get_queryset(self): if not self.request.user.is_anonymous: @@ -200,7 +228,7 @@ class NotebookDetailView(BaseDetailView): return Notebook.objects.filter(owner=self.request.user) def get_object(self): - notebook = get_object_or_404(self.get_queryset().select_related(), owner=self.request.user, slug=self.kwargs["slug"]) + notebook = get_object_or_404(self.get_queryset().prefetch_related('note_set').prefetch_related('note_set__tags'), owner=self.request.user, slug=self.kwargs["slug"]) self.form = NotebookForm(instance=notebook) return notebook @@ -208,6 +236,8 @@ class NotebookDetailView(BaseDetailView): context = super().get_context_data(**kwargs) #context['notes_list'] = Note.objects.filter(owner=self.request.user).select_related() context['form'] = self.form + context['tag_list'] = LuxTag.objects.filter(note__owner=self.request.user).annotate(note_count=Count('note')) + context['notebook_list'] = Notebook.objects.filter(owner=self.request.user).annotate(note_count=Count('note')) return context |