diff options
Diffstat (limited to 'apps/notes/views.py')
-rw-r--r-- | apps/notes/views.py | 68 |
1 files changed, 52 insertions, 16 deletions
diff --git a/apps/notes/views.py b/apps/notes/views.py index e4b8fda..b971390 100644 --- a/apps/notes/views.py +++ b/apps/notes/views.py @@ -1,21 +1,23 @@ from django.views.generic import CreateView, ListView, UpdateView, DeleteView from django.views.generic.detail import DetailView +from django.views.generic.base import View, RedirectView from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404, render, redirect -from django.urls import reverse +from django.urls import reverse, reverse_lazy from rest_framework import viewsets from rest_framework.response import Response from rest_framework.decorators import list_route from rest_framework import permissions -from .serializers import NoteSerializer, FolderSerializer -from .models import Note, Folder +from .serializers import NoteSerializer, NotebookSerializer +from .models import Note, Notebook +from .forms import NoteForm @method_decorator(login_required, name='dispatch') -class LoggedInCreateViewWithUser(CreateView): +class LoggedInViewWithUser(View): def get_form_kwargs(self, **kwargs): kwargs = super().get_form_kwargs(**kwargs) @@ -23,20 +25,56 @@ class LoggedInCreateViewWithUser(CreateView): return kwargs -class NoteListView(ListView): +class NoteListView(LoggedInViewWithUser, ListView): model = Note def get_queryset(self): if not self.request.user.is_anonymous: - return Note.objects.filter(created_by=self.request.user) + return Note.objects.filter(owner=self.request.user) def get_template_names(self): + # print("IP Address for debug-toolbar: " + self.request.META['REMOTE_ADDR']) if not self.request.user.is_anonymous: return ['notes/notes_list.html'] else: return ['sell.html'] +class NoteListRedirectView(RedirectView, LoggedInViewWithUser): + + def get_redirect_url(self, *args, **kwargs): + return reverse_lazy("notes:note-list", kwargs={"user": self.request.user.username}) + + +class NoteDetailView(UpdateView, LoggedInViewWithUser): + model = Note + form_class = NoteForm + template_name = 'notes/notes_detail.html' + + def get_queryset(self): + if not self.request.user.is_anonymous: + return Note.objects.filter(owner=self.request.user) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['notes_list'] = Note.objects.filter(owner=self.request.user) + return context + + +class NoteCreateView(CreateView, LoggedInViewWithUser): + model = Note + form_class = NoteForm + template_name = 'notes/notes_create.html' + + def form_valid(self, form): + form.instance.owner = self.request.user + self.object = form.save() + return super(NoteCreateView, self).form_valid(form) + + def get_success_url(self): + return reverse_lazy('notes:note-detail', kwargs={'user': self.request.user.username, 'slug': self.object.slug}) + + class IsOwnerOrDeny(permissions.BasePermission): """ Custom permission to only allow owners to post to their endpoint @@ -52,15 +90,13 @@ class NoteViewSet(viewsets.ModelViewSet): API endpoint that allows notes to be viewed or edited. """ serializer_class = NoteSerializer - permission_classes = (permissions.IsAuthenticated, IsOwnerOrDeny,) + permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): - return Note.objects.filter(created_by=self.request.user).order_by('-date_created') + return Note.objects.filter(owner=self.request.user).order_by('-date_created') - @list_route(methods=['post']) def perform_create(self, serializer): - serializer.save(created_by=self.request.user) - return super(NoteViewSet, self).perform_create(serializer) + serializer.save(owner=self.request.user) def get_object(self): obj = get_object_or_404(self.get_queryset(), pk=self.kwargs["pk"]) @@ -71,14 +107,14 @@ class NoteViewSet(viewsets.ModelViewSet): return obj -class FolderViewSet(viewsets.ModelViewSet): +class NotebookViewSet(viewsets.ModelViewSet): """ - API endpoint that allows folder to be viewed or edited. + API endpoint that allows botebook to be viewed or edited. """ - serializer_class = FolderSerializer + serializer_class = NotebookSerializer def get_queryset(self): - return Folder.objects.filter(created_by=self.request.user).order_by('-date_created') + return Notebook.objects.filter(owner=self.request.user).order_by('-date_created') def perform_create(self, serializer): - serializer.save(created_by=self.request.user) + serializer.save(owner=self.request.user) |