1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
from django.views.generic import CreateView, ListView, UpdateView, DeleteView
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView, ModelFormMixin
from django.http import JsonResponse
from django.core import serializers
from django.forms import modelformset_factory
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
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, NotebookSerializer, NoteTagSerializer
from .models import Note, Notebook, LuxTag
from .forms import NoteForm, NotebookForm
from utils.views import AjaxableResponseMixin
##################
# Base Views
##################
@method_decorator(login_required, name='dispatch')
class BaseListView(ListView):
pass
@method_decorator(login_required, name='dispatch')
class BaseDetailView(DetailView):
pass
@method_decorator(login_required, name='dispatch')
class LoggedInViewWithUser(FormView):
def get_form_kwargs(self, **kwargs):
kwargs = super().get_form_kwargs(**kwargs)
kwargs.update({'user': self.request.user})
return kwargs
##################
# Note Views
##################
class NoteListView(BaseListView):
model = Note
def get_queryset(self):
if not self.request.user.is_anonymous:
return Note.objects.prefetch_related('tags').filter(owner=self.request.user).select_related('notebook')
def get_context_data(self, **kwargs):
context = super(NoteListView, self).get_context_data(**kwargs)
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
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 NoteDetailView(LoggedInViewWithUser, AjaxableResponseMixin, UpdateView):
'''
POST only works as AJAX
'''
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['notebook_form'] = NotebookForm
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()
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):
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:detail', kwargs={'slug': self.object.slug, 'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['notebook_form'] = NotebookForm
# context['notes_list'] = Note.objects.filter(owner=self.request.user).select_related()
return context
class NoteTagView(BaseListView):
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:
try:
tags = self.kwargs['slug'].split("+")
except ValueError:
tags = self.kwargs['slug']
self.tag_list = [x.strip() for x in tags]
qs = Note.objects.prefetch_related('tags').filter(owner=self.request.user).select_related('notebook')
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'] = 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
##################
# Notebook Views
##################
class NotebookListView(LoggedInViewWithUser, CreateView):
model = Notebook
form_class = NotebookForm
template_name = 'notes/notebook_list.html'
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
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)
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
class NotebookUpdateView(LoggedInViewWithUser, UpdateView):
model = Notebook
form_class = NotebookForm
template_name = 'notes/notebook_list.html'
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:
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().prefetch_related('note_set').prefetch_related('note_set__tags'), owner=self.request.user, slug=self.kwargs["slug"])
self.form = NotebookForm(instance=notebook)
return notebook
def get_context_data(self, **kwargs):
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
##################
# API Views
##################
class IsOwnerOrDeny(permissions.BasePermission):
"""
Custom permission to only allow owners to post to their endpoint
"""
def has_object_permission(self, request, view, obj):
# Write permissions are only allowed to the owner of the snippet.
return obj.owner == request.user
class NoteViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows notes to be viewed or edited.
"""
serializer_class = NoteSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_queryset(self):
return Note.objects.filter(owner=self.request.user).order_by('-date_created')
def perform_create(self, serializer):
serializer.save(owner=self.request.user, tags__owner=self.request.user)
def get_object(self):
obj = get_object_or_404(self.get_queryset(), pk=self.kwargs["pk"])
if obj.is_public:
return obj
else:
self.check_object_permissions(self.request, obj)
return obj
class NotebookViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows botebook to be viewed or edited.
"""
serializer_class = NotebookSerializer
def get_queryset(self):
return Notebook.objects.filter(owner=self.request.user).order_by('-date_created')
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]
class TagViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows notes to be viewed or edited.
"""
serializer_class = NoteTagSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_queryset(self):
return LuxTag.objects.filter(owner=self.request.user)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
|