diff options
author | luxagraf <sng@luxagraf.net> | 2018-11-29 16:57:26 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2018-11-29 16:57:26 -0600 |
commit | 4974eb58480f413c67f5f6e8fac430186eda2b62 (patch) | |
tree | 7402ecba682dbda38db4b6db221cc8378a390977 /apps | |
parent | 0c2a092e8d8ad33a1c306ee9efca0da96eb56415 (diff) |
uploading all recent changes ahead of sys upgrade
Diffstat (limited to 'apps')
-rw-r--r-- | apps/accounts/models.py | 9 | ||||
-rw-r--r-- | apps/accounts/views.py | 4 | ||||
-rw-r--r-- | apps/notes/forms.py | 11 | ||||
-rw-r--r-- | apps/notes/models.py | 4 | ||||
-rw-r--r-- | apps/notes/serializers.py | 4 | ||||
-rw-r--r-- | apps/notes/urls.py | 6 | ||||
-rw-r--r-- | apps/notes/views.py | 62 | ||||
-rw-r--r-- | apps/utils/util.py | 5 |
8 files changed, 98 insertions, 7 deletions
diff --git a/apps/accounts/models.py b/apps/accounts/models.py index 5ed7634..d000e3e 100644 --- a/apps/accounts/models.py +++ b/apps/accounts/models.py @@ -1,5 +1,8 @@ from django.db import models from django.contrib.auth.models import AbstractUser +from django.utils.functional import cached_property + +from notes.models import Notebook class User(AbstractUser): @@ -10,7 +13,7 @@ class User(AbstractUser): class UserProfile(models.Model): - user = models.OneToOneField(User, on_delete=models.CASCADE) + user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile') photo = models.ImageField(upload_to='profile', null=True, blank=True) website = models.CharField(max_length=300, null=True, blank=True, default='') location = models.CharField(max_length=300, null=True, blank=True, default='') @@ -20,3 +23,7 @@ class UserProfile(models.Model): def __str__(self): return self.user.username + + @cached_property + def get_notebook_list(self): + return Notebook.objects.filter(owner=self.user).select_related()[:8] diff --git a/apps/accounts/views.py b/apps/accounts/views.py index e51c60e..d463522 100644 --- a/apps/accounts/views.py +++ b/apps/accounts/views.py @@ -21,7 +21,7 @@ class ProfileView(UpdateViewWithUser): template_name = "accounts/profile.html" def get_object(self): - return self.request.user.userprofile + return self.request.user.profile class SettingsListView(DetailView): @@ -29,4 +29,4 @@ class SettingsListView(DetailView): template_name = "accounts/profile.html" def get_object(self): - return self.request.user.userprofile + return self.request.user.profile diff --git a/apps/notes/forms.py b/apps/notes/forms.py index a7a4f8d..c1df176 100644 --- a/apps/notes/forms.py +++ b/apps/notes/forms.py @@ -1,7 +1,7 @@ from django import forms from django.utils.translation import ugettext_lazy as _ -from .models import Note +from .models import Note, Notebook class NoteForm(forms.ModelForm): @@ -15,3 +15,12 @@ class NoteForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.user = kwargs.pop("user", None) super(NoteForm, self).__init__(*args, **kwargs) + + +class NotebookForm(NoteForm): + class Meta: + model = Notebook + fields = ['name'] + labels = { + "name": _("Notebook Name"), + } diff --git a/apps/notes/models.py b/apps/notes/models.py index 83766a9..c0b23ce 100644 --- a/apps/notes/models.py +++ b/apps/notes/models.py @@ -33,6 +33,10 @@ class Notebook(models.Model): self.slug = unique_slug_generator(self) super(Notebook, self).save() + @cached_property + def get_absolute_url(self): + return reverse("notes:notebook-detail", kwargs={"user": self.owner.username, "slug": self.slug}) + class Note(models.Model): unique_id = models.UUIDField(default=uuid.uuid4, editable=False) diff --git a/apps/notes/serializers.py b/apps/notes/serializers.py index 2fbcc1f..daef52e 100644 --- a/apps/notes/serializers.py +++ b/apps/notes/serializers.py @@ -13,6 +13,8 @@ class NoteSerializer(TaggitSerializer, serializers.ModelSerializer): class NotebookSerializer(serializers.HyperlinkedModelSerializer): + json_absolute_url = serializers.URLField(source='get_absolute_url', read_only=True) + class Meta: model = Notebook - fields = ('name',) + fields = ('name', 'json_absolute_url') diff --git a/apps/notes/urls.py b/apps/notes/urls.py index ccfcc9e..a79c7cd 100644 --- a/apps/notes/urls.py +++ b/apps/notes/urls.py @@ -5,12 +5,18 @@ from .views import ( NoteCreateView, NoteListView, NoteListRedirectView, + NotebookListView, + NotebookDetailView, + NoteTagView, ) app_name = "notes" urlpatterns = [ path(r'create/', NoteCreateView.as_view(), name='note-create',), + path(r'<str:user>/tagged/<slug>', NoteTagView.as_view(), name='note-tag',), + path(r'<str:user>/notebooks/<slug>', NotebookDetailView.as_view(), name='notebook-detail',), + path(r'<str:user>/notebooks/', NotebookListView.as_view(), name='notebook-list',), path(r'<str:user>/<slug>', NoteDetailView.as_view(), name='note-detail',), path(r'<str:user>/', NoteListView.as_view(), name='note-list',), path(r'', NoteListRedirectView.as_view(), name='note-redirect',), 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] diff --git a/apps/utils/util.py b/apps/utils/util.py index 899b73f..415704c 100644 --- a/apps/utils/util.py +++ b/apps/utils/util.py @@ -95,7 +95,10 @@ def unique_slug_generator(instance, new_slug=None): if new_slug is not None: slug = new_slug else: - slug = slugify(instance.title) + try: + slug = slugify(instance.title) + except AttributeError: + slug = slugify(instance.name) Klass = instance.__class__ qs_exists = Klass.objects.filter(slug=slug).exists() if qs_exists: |