From efcc50c5ecff2b62b80f0621752e2098014f9b08 Mon Sep 17 00:00:00 2001 From: luxagraf Date: Mon, 24 Jul 2023 13:31:20 -0500 Subject: notes: split notes off into its own app --- app/notes/admin.py | 27 +++++++++++++ app/notes/forms.py | 15 +++++++ app/notes/migrations/0001_initial.py | 35 ++++++++++++++++ app/notes/migrations/__init__.py | 0 app/notes/models.py | 36 +++++++++++++++++ app/notes/urls.py | 28 +++++++++++++ app/notes/views.py | 46 ++++++++++++++++++++++ app/posts/forms.py | 3 +- .../0014_alter_note_post_alter_note_user.py | 26 ++++++++++++ app/posts/models.py | 4 +- app/posts/note_urls.py | 33 ---------------- config/base_urls.py | 2 +- 12 files changed, 218 insertions(+), 37 deletions(-) create mode 100644 app/notes/admin.py create mode 100644 app/notes/forms.py create mode 100644 app/notes/migrations/0001_initial.py create mode 100644 app/notes/migrations/__init__.py create mode 100644 app/notes/models.py create mode 100644 app/notes/urls.py create mode 100644 app/notes/views.py create mode 100644 app/posts/migrations/0014_alter_note_post_alter_note_user.py delete mode 100644 app/posts/note_urls.py diff --git a/app/notes/admin.py b/app/notes/admin.py new file mode 100644 index 0000000..ffe391e --- /dev/null +++ b/app/notes/admin.py @@ -0,0 +1,27 @@ +from django.contrib import admin + +from .models import Note +from utils.widgets import AdminImageWidget, LGEntryForm + +from django.contrib.auth.models import User +from django.contrib.admin import SimpleListFilter + + +@admin.register(Note) +class NoteAdmin(admin.ModelAdmin): + def get_form(self, request, obj=None, change=False, **kwargs): + form = super().get_form(request, obj=obj, change=change, **kwargs) + form.base_fields['post'].queryset = Post.objects.filter( + user=request.user + ) + return form + + list_display = ('date_created', 'title', 'post') + search_fields = ['title'] + list_filter = ['date_created'] + + class Media: + js = ('image-loader.js', 'next-prev-links.js') + css = { + "all": ("my_styles.css",) + } diff --git a/app/notes/forms.py b/app/notes/forms.py new file mode 100644 index 0000000..5cde108 --- /dev/null +++ b/app/notes/forms.py @@ -0,0 +1,15 @@ +from django.forms import ModelForm + +from .models import Note +from posts.models import Post + +class NoteCreateForm(ModelForm): + + def __init__(self,*args,**kwargs): + user = kwargs.pop('user') + super(NoteCreateForm,self).__init__(*args,**kwargs) + self.fields['post'].queryset = Post.objects.filter(user=user).order_by("title") + + class Meta: + model = Note + fields = ['title', 'url', 'body_markdown', 'post'] diff --git a/app/notes/migrations/0001_initial.py b/app/notes/migrations/0001_initial.py new file mode 100644 index 0000000..4851ac9 --- /dev/null +++ b/app/notes/migrations/0001_initial.py @@ -0,0 +1,35 @@ +# Generated by Django 4.2.2 on 2023-07-24 18:28 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('posts', '0014_alter_note_post_alter_note_user'), + ] + + operations = [ + migrations.CreateModel( + name='Note', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=400)), + ('url', models.CharField(blank=True, max_length=400, null=True)), + ('body_markdown', models.TextField(blank=True, null=True)), + ('date_created', models.DateTimeField(default=django.utils.timezone.now)), + ('status', models.IntegerField(choices=[(0, 'Call In'), (1, 'Coming'), (2, 'Testing'), (3, 'Done'), (4, 'Live')], default=0)), + ('post', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='posts.post')), + ('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'ordering': ('date_created',), + }, + ), + ] diff --git a/app/notes/migrations/__init__.py b/app/notes/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/notes/models.py b/app/notes/models.py new file mode 100644 index 0000000..e3dd312 --- /dev/null +++ b/app/notes/models.py @@ -0,0 +1,36 @@ +from django.db import models +from django.urls import reverse +from django.utils.html import format_html, format_html_join +from django.utils import timezone +import settings + +from posts.models import Post + + +class Note(models.Model): + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL) + title = models.CharField(max_length=400) + url = models.CharField(max_length=400, blank=True, null=True) + body_markdown = models.TextField(blank=True, null=True) + date_created = models.DateTimeField(default=timezone.now) + post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True) + STATUS = ( + (0, 'Call In'), + (1, 'Coming'), + (2, 'Testing'), + (3, 'Done'), + (4, 'Live'), + ) + status = models.IntegerField(choices=STATUS, default=0) + + class Meta: + ordering = ('date_created',) + + def __str__(self): + return self.title + + def get_absolute_url(self): + return reverse('notes:edit', kwargs={"pk": self.pk}) + + def save(self, *args, **kwargs): + super(Note, self).save() diff --git a/app/notes/urls.py b/app/notes/urls.py new file mode 100644 index 0000000..760f0c3 --- /dev/null +++ b/app/notes/urls.py @@ -0,0 +1,28 @@ +from django.urls import path, re_path + +from . import views + +app_name = "notes" + +urlpatterns = [ + path( + r'', + views.NoteListView.as_view(), + name="list" + ), + path( + r'create', + views.NoteCreateView.as_view(), + name="create" + ), + path( + r'', + views.NoteListView.as_view(), + name="todo" + ), + path( + r'/edit', + views.NoteUpdateView.as_view(), + name="edit" + ), +] diff --git a/app/notes/views.py b/app/notes/views.py new file mode 100644 index 0000000..70f20c6 --- /dev/null +++ b/app/notes/views.py @@ -0,0 +1,46 @@ +from django.views.generic import UpdateView, DetailView, ListView +from django.views.generic.edit import CreateView, DeleteView, UpdateView +from django.utils.decorators import method_decorator +from django.contrib.auth.decorators import login_required +from django.urls import reverse + +from .models import Note +from .forms import NoteCreateForm + + +class NoteCreateView(CreateView): + model = Note + form_class = NoteCreateForm + + def get_form_kwargs(self): + kwargs = super(NoteCreateView, self).get_form_kwargs() + kwargs.update({'user': self.request.user}) + return kwargs + + def get_success_url(self): + return reverse('posts:detail', kwargs={"pk": self.object.post.pk}) + + +class NoteUpdateView(UpdateView): + model = Note + fields = ['title', 'url', 'body_markdown', 'post', 'status', 'user'] + + def get_context_data(self, **kwargs): + context = super(NoteUpdateView, self).get_context_data(**kwargs) + context['is_update'] = True + return context + + def get_success_url(self): + return reverse('posts:detail', kwargs={"pk": self.object.post.pk}) + + +class NoteListView(ListView): + model = Note + + def get_queryset(self): + if self.kwargs['status'] == "todo": + status = "Call In" + else: + status = self.kwargs['status'] + status_reverse = dict((v, k) for k, v in Note.STATUS) + return Note.objects.filter(user=self.request.user).filter(status=status_reverse[status]) diff --git a/app/posts/forms.py b/app/posts/forms.py index 8f98980..5cde108 100644 --- a/app/posts/forms.py +++ b/app/posts/forms.py @@ -1,6 +1,7 @@ from django.forms import ModelForm -from .models import Note, Post +from .models import Note +from posts.models import Post class NoteCreateForm(ModelForm): diff --git a/app/posts/migrations/0014_alter_note_post_alter_note_user.py b/app/posts/migrations/0014_alter_note_post_alter_note_user.py new file mode 100644 index 0000000..2275418 --- /dev/null +++ b/app/posts/migrations/0014_alter_note_post_alter_note_user.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.2 on 2023-07-24 18:28 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('posts', '0013_note_user_alter_note_status'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='post', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='oldpost', to='posts.post'), + ), + migrations.AlterField( + model_name='note', + name='user', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='olduser', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/app/posts/models.py b/app/posts/models.py index a3cb3af..0d1acf9 100644 --- a/app/posts/models.py +++ b/app/posts/models.py @@ -93,12 +93,12 @@ class Post(models.Model): class Note(models.Model): - user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL) + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL, related_name="olduser") title = models.CharField(max_length=400) url = models.CharField(max_length=400, blank=True, null=True) body_markdown = models.TextField(blank=True, null=True) date_created = models.DateTimeField(default=timezone.now) - post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True) + post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True, related_name="oldpost") STATUS = ( (0, 'Call In'), (1, 'Coming'), diff --git a/app/posts/note_urls.py b/app/posts/note_urls.py deleted file mode 100644 index 94a030e..0000000 --- a/app/posts/note_urls.py +++ /dev/null @@ -1,33 +0,0 @@ -from django.urls import path, re_path - -from . import views - -app_name = "notes" - -urlpatterns = [ - path( - r'', - views.NoteListView.as_view(), - name="list" - ), - path( - r'create', - views.NoteCreateView.as_view(), - name="create" - ), - path( - r'', - views.NoteListView.as_view(), - name="todo" - ), - path( - r'/edit', - views.NoteUpdateView.as_view(), - name="edit" - ), - path( - r'/notes', - views.PostNotesView.as_view(), - name="detail" - ), -] diff --git a/config/base_urls.py b/config/base_urls.py index 7825bb4..9f7f890 100644 --- a/config/base_urls.py +++ b/config/base_urls.py @@ -17,6 +17,6 @@ urlpatterns = [ path(r'post/', include('posts.urls')), re_path(r'^post/$', RedirectView.as_view(url='/posts/')), path(r'posts/', include('posts.urls', namespace='posts')), - path(r'notes/', include('posts.note_urls')), + path(r'notes/', include('notes.urls', namespace='notes')), path('__debug__/', include(debug_toolbar.urls)), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) -- cgit v1.2.3-70-g09d2