diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/accounts/models.py | 2 | ||||
-rw-r--r-- | apps/accounts/signals.py | 3 | ||||
-rw-r--r-- | apps/notes/migrations/0014_auto_20190104_1945.py | 56 | ||||
-rw-r--r-- | apps/notes/migrations/0015_auto_20190104_1946.py | 31 | ||||
-rw-r--r-- | apps/notes/models.py | 32 | ||||
-rw-r--r-- | apps/notes/views.py | 32 | ||||
-rw-r--r-- | apps/utils/widgets.py | 2 |
7 files changed, 140 insertions, 18 deletions
diff --git a/apps/accounts/models.py b/apps/accounts/models.py index d000e3e..1b62ffd 100644 --- a/apps/accounts/models.py +++ b/apps/accounts/models.py @@ -26,4 +26,4 @@ class UserProfile(models.Model): @cached_property def get_notebook_list(self): - return Notebook.objects.filter(owner=self.user).select_related()[:8] + return Notebook.objects.filter(owner=self.user).select_related().annotate(note_count=models.Count('note'))[:8] diff --git a/apps/accounts/signals.py b/apps/accounts/signals.py index 837a7ed..7c7f7c9 100644 --- a/apps/accounts/signals.py +++ b/apps/accounts/signals.py @@ -2,6 +2,7 @@ from django.db.models.signals import post_save from django.dispatch import receiver from .models import User, UserProfile +from notes.models import Notebook @receiver(post_save, sender=User) @@ -10,3 +11,5 @@ def create_profile(sender, update_fields, created, instance, **kwargs): if created: user_profile = UserProfile.objects.create(user=instance) user_profile.save() + user_trash_notebook = Notebook.objects.create(owner=instance, name="Trash") + user_trash_notebook.save() diff --git a/apps/notes/migrations/0014_auto_20190104_1945.py b/apps/notes/migrations/0014_auto_20190104_1945.py new file mode 100644 index 0000000..42398a3 --- /dev/null +++ b/apps/notes/migrations/0014_auto_20190104_1945.py @@ -0,0 +1,56 @@ +# Generated by Django 2.1.2 on 2019-01-05 01:45 + +from django.db import migrations, models +import django.db.models.manager + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0013_remove_luxtag_owner'), + ] + + operations = [ + migrations.AlterModelManagers( + name='note', + managers=[ + ('include_trash', django.db.models.manager.Manager()), + ], + ), + migrations.AlterModelManagers( + name='notebook', + managers=[ + ('include_trash', django.db.models.manager.Manager()), + ], + ), + migrations.AlterField( + model_name='annotation', + name='body_html', + field=models.TextField(blank=True, default=''), + preserve_default=False, + ), + migrations.AlterField( + model_name='annotation', + name='body_text', + field=models.TextField(blank=True, default=''), + preserve_default=False, + ), + migrations.AlterField( + model_name='annotation', + name='highlight_text', + field=models.TextField(blank=True, default=''), + preserve_default=False, + ), + migrations.AlterField( + model_name='note', + name='body_html', + field=models.TextField(blank=True, default=''), + preserve_default=False, + ), + migrations.AlterField( + model_name='note', + name='body_text', + field=models.TextField(blank=True, default=''), + preserve_default=False, + ), + ] diff --git a/apps/notes/migrations/0015_auto_20190104_1946.py b/apps/notes/migrations/0015_auto_20190104_1946.py new file mode 100644 index 0000000..542dfc6 --- /dev/null +++ b/apps/notes/migrations/0015_auto_20190104_1946.py @@ -0,0 +1,31 @@ +# Generated by Django 2.1.2 on 2019-01-05 01:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0014_auto_20190104_1945'), + ] + + operations = [ + migrations.AlterField( + model_name='luxtag', + name='color_rgb', + field=models.CharField(blank=True, default='', max_length=20), + preserve_default=False, + ), + migrations.AlterField( + model_name='note', + name='url', + field=models.CharField(blank=True, default='', max_length=250), + preserve_default=False, + ), + migrations.AlterField( + model_name='notebook', + name='color_rgb', + field=models.CharField(blank=True, default='', max_length=20), + preserve_default=False, + ), + ] diff --git a/apps/notes/models.py b/apps/notes/models.py index 9dc4f13..bfcb8ba 100644 --- a/apps/notes/models.py +++ b/apps/notes/models.py @@ -19,7 +19,7 @@ from taggit.models import TagBase, GenericTaggedItemBase class LuxTag(TagBase): - color_rgb = models.CharField(max_length=20, null=True, blank=True) + color_rgb = models.CharField(max_length=20, blank=True) class Meta: verbose_name = _("Tag") @@ -34,16 +34,24 @@ class TaggedNotes(GenericTaggedItemBase): tag = models.ForeignKey(LuxTag, related_name="%(app_label)s_%(class)s_items", on_delete=models.CASCADE) +class NotebookManager(models.Manager): + def get_queryset(self): + return super().get_queryset().exclude(name='Trash') + + class Notebook(models.Model): """ Notebook model for Notes """ unique_id = models.UUIDField(default=uuid.uuid4, editable=False) owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) name = models.CharField(max_length=250) - color_rgb = models.CharField(max_length=20, null=True, blank=True) + color_rgb = models.CharField(max_length=20, blank=True) slug = models.SlugField(blank=True) date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False) date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False) + include_trash = models.Manager() + objects = NotebookManager() # The default manager never shows the Notebook 'Trash' + class Meta: unique_together = ("owner", "name") @@ -69,21 +77,29 @@ class Notebook(models.Model): return rgba +class NoteManager(models.Manager): + def get_queryset(self): + return super().get_queryset().exclude(notebook__name='Trash') + + class Note(models.Model): unique_id = models.UUIDField(default=uuid.uuid4, editable=False) owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False) date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False) title = models.CharField(max_length=250) - body_text = models.TextField(null=True) - body_html = models.TextField(null=True, blank=True) + body_text = models.TextField(blank=True) + body_html = models.TextField(blank=True) body_qjson = JSONField(null=True, blank=True) - url = models.CharField(max_length=250, null=True, blank=True) + url = models.CharField(max_length=250, blank=True) slug = models.SlugField(blank=True) notebook = models.ForeignKey(Notebook, null=True, blank=True, on_delete=models.SET_NULL) tags = TaggableManager(through=TaggedNotes, blank=True, help_text='Tags') is_public = models.BooleanField(default=False) + include_trash = models.Manager() + objects = NoteManager() # The default manager never shows the notes in 'Trash' + class Meta: unique_together = ("owner", "slug") ordering = ("-date_created", "-date_updated") @@ -108,9 +124,9 @@ class Annotation(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False) date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False) - highlight_text = models.TextField(null=True) - body_text = models.TextField(null=True) - body_html = models.TextField(null=True, blank=True) + highlight_text = models.TextField(blank=True) + body_text = models.TextField(blank=True) + body_html = models.TextField(blank=True) body_qjson = JSONField(null=True, blank=True) note = models.ForeignKey(Note, null=True, blank=True, on_delete=models.SET_NULL) is_public = models.BooleanField(default=False) 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) diff --git a/apps/utils/widgets.py b/apps/utils/widgets.py index 2745932..6dc28bf 100644 --- a/apps/utils/widgets.py +++ b/apps/utils/widgets.py @@ -142,10 +142,12 @@ class RelatedFieldWidgetCanAdd(widgets.Select): rel_to = related_model info = (rel_to._meta.app_label, rel_to._meta.object_name.lower()) related_url = 'admin:%s_%s_add' % info + print(related_url) self.related_url = related_url def render(self, name, value, *args, **kwargs): self.related_url = reverse(self.related_url) + print(self.related_url) output = [super(RelatedFieldWidgetCanAdd, self).render(name, value, *args, **kwargs)] output.append('<a class="circle plus small-circle modal-open" href="%s" id="add_id_%s" data-modal-hed-class="%s" data-modal-hed="Add a New %s">New</a>' % (self.related_url, name, name, name.capitalize())) return mark_safe(u''.join(output)) |