diff options
Diffstat (limited to 'apps/notes')
-rw-r--r-- | apps/notes/models.py | 4 | ||||
-rw-r--r-- | apps/notes/notebook_urls.py | 2 | ||||
-rw-r--r-- | apps/notes/views.py | 48 |
3 files changed, 43 insertions, 11 deletions
diff --git a/apps/notes/models.py b/apps/notes/models.py index bfcb8ba..c4b1df6 100644 --- a/apps/notes/models.py +++ b/apps/notes/models.py @@ -54,13 +54,13 @@ class Notebook(models.Model): class Meta: unique_together = ("owner", "name") + ordering = ("name", "date_created", "date_updated") def __str__(self): return self.name def save(self, **kwargs): - if self._state.adding: - self.slug = unique_slug_generator(self) + self.slug = unique_slug_generator(self) super(Notebook, self).save() @cached_property diff --git a/apps/notes/notebook_urls.py b/apps/notes/notebook_urls.py index b433d15..8e3133e 100644 --- a/apps/notes/notebook_urls.py +++ b/apps/notes/notebook_urls.py @@ -3,11 +3,13 @@ from django.urls import path from .views import ( NotebookListView, NotebookDetailView, + NotebookUpdateView, ) app_name = "notebooks" urlpatterns = [ + path(r'<slug>/update', NotebookUpdateView.as_view(), name='update',), path(r'<slug>', NotebookDetailView.as_view(), name='detail',), path(r'', NotebookListView.as_view(), name='list',), ] 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 |