diff options
Diffstat (limited to 'apps/notes/views.py')
-rw-r--r-- | apps/notes/views.py | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/apps/notes/views.py b/apps/notes/views.py index 6751340..245e2ab 100644 --- a/apps/notes/views.py +++ b/apps/notes/views.py @@ -8,6 +8,7 @@ from django.db.models import Count from django.views.generic.base import RedirectView from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required +from django.contrib import messages from django.shortcuts import get_object_or_404, render, redirect from django.urls import reverse, reverse_lazy @@ -59,7 +60,7 @@ class NoteListView(BaseListView): def get_context_data(self, **kwargs): context = super(NoteListView, self).get_context_data(**kwargs) - context['notebook_list'] = Notebook.objects.filter(owner=self.request.user).exclude(name="Trash").annotate(note_count=Count('note')) + context['notebook_list'] = Notebook.objects.filter(owner=self.request.user).annotate(note_count=Count('note')) context['tag_list'] = LuxTag.objects.filter(note__owner=self.request.user).annotate(note_count=Count('note')) return context @@ -89,13 +90,19 @@ class NoteDetailView(LoggedInViewWithUser, AjaxableResponseMixin, UpdateView): return context def form_valid(self, form): - self.object = form.save() - tags = serializers.serialize("json", self.object.tags.all()) - data = { - 'tags': tags, - 'notebook': {'name': self.object.notebook.name, 'color': self.object.notebook.color_rgb} - } - return JsonResponse(data, safe=True) + if "trash" in self.request.POST: + form.instance.notebook = Notebook.include_trash.get(owner=self.request.user, name="Trash") + self.object = form.save() + messages.info(self.request, 'The note %s was moved to the trash. <a href="/nb/trash">View trash</a>' % (self.object.title), extra_tags='safe') + return redirect('notes:list') + else: + self.object = form.save() + tags = serializers.serialize("json", self.object.tags.all()) + data = { + 'tags': tags, + 'notebook': {'name': self.object.notebook.name, 'color': self.object.notebook.color_rgb} + } + return JsonResponse(data, safe=True) class NoteCreateView(LoggedInViewWithUser, CreateView): @@ -173,7 +180,7 @@ class NotebookListView(LoggedInViewWithUser, CreateView): 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).exclude(name="Trash").select_related().annotate(note_count=Count('note'))) + 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() return context @@ -185,6 +192,13 @@ class NotebookListView(LoggedInViewWithUser, CreateView): class NotebookDetailView(BaseDetailView): model = Notebook + def get_queryset(self): + if not self.request.user.is_anonymous: + if self.kwargs["slug"] == 'trash': + return Notebook.include_trash.filter(owner=self.request.user) + else: + 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"]) self.form = NotebookForm(instance=notebook) |