diff options
Diffstat (limited to 'apps/notes/views.py')
-rw-r--r-- | apps/notes/views.py | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/apps/notes/views.py b/apps/notes/views.py index b971390..d885ad9 100644 --- a/apps/notes/views.py +++ b/apps/notes/views.py @@ -13,7 +13,7 @@ from rest_framework import permissions from .serializers import NoteSerializer, NotebookSerializer from .models import Note, Notebook -from .forms import NoteForm +from .forms import NoteForm, NotebookForm @method_decorator(login_required, name='dispatch') @@ -40,6 +40,35 @@ class NoteListView(LoggedInViewWithUser, ListView): return ['sell.html'] +class NoteTagView(LoggedInViewWithUser, ListView): + model = Note + template_name = 'notes/notes_list.html' + + def get_queryset(self): + ''' + This can generate a crazy amount of joins if there's a lot of tags + have to keep an eye on it. Would be better to do: + from django.db.models import Q + from functools import reduce + from operator import and_, or_ + #query = reduce(and_, (Q(tags__slug=t) for t in self.tag_list)) + # Note.objects.filter(query, owner=self.request.user) + But that doesn't work for some reason. + ''' + if not self.request.user.is_anonymous: + self.tag_list = [x.strip() for x in self.kwargs['slug'].split("+")] + qs = Note.objects.filter(owner=self.request.user) + for tag in self.tag_list: + qs = qs.filter(tags__slug=tag) + return qs + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['notes_list'] = Note.objects.filter(owner=self.request.user) + context['tags'] = self.tag_list + return context + + class NoteListRedirectView(RedirectView, LoggedInViewWithUser): def get_redirect_url(self, *args, **kwargs): @@ -75,6 +104,34 @@ class NoteCreateView(CreateView, LoggedInViewWithUser): return reverse_lazy('notes:note-detail', kwargs={'user': self.request.user.username, 'slug': self.object.slug}) +class NotebookListView(CreateView, LoggedInViewWithUser): + model = Notebook + form_class = NotebookForm + template_name = 'notes/notebook_create.html' + + def get_queryset(self): + if not self.request.user.is_anonymous: + return Notebook.objects.filter(owner=self.request.user) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['notebook_list'] = Notebook.objects.filter(owner=self.request.user) + context['notes_list'] = Note.objects.filter(owner=self.request.user).select_related() + return context + + +class NotebookDetailView(DetailView, LoggedInViewWithUser): + model = Notebook + + def get_object(self): + return get_object_or_404(self.get_queryset().select_related(), owner=self.request.user, slug=self.kwargs["slug"]) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['notes_list'] = Note.objects.filter(owner=self.request.user).select_related() + return context + + class IsOwnerOrDeny(permissions.BasePermission): """ Custom permission to only allow owners to post to their endpoint @@ -118,3 +175,6 @@ class NotebookViewSet(viewsets.ModelViewSet): def perform_create(self, serializer): serializer.save(owner=self.request.user) + + def get_menu_data(self, serializer): + return Notebook.objects.filter(owner=self.request.user).order_by('-name')[:1] |