diff --git a/project/db.sqlite3 b/project/db.sqlite3 index 4b4b685..a15a291 100644 Binary files a/project/db.sqlite3 and b/project/db.sqlite3 differ diff --git a/project/notes/migrations/0006_note_owner.py b/project/notes/migrations/0006_note_owner.py new file mode 100644 index 0000000..4ffa86d --- /dev/null +++ b/project/notes/migrations/0006_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-01 13:47 + +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), + ('notes', '0005_delete_historicalnote'), + ] + + operations = [ + migrations.AddField( + model_name='note', + name='owner', + field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/migrations/0007_alter_note_owner.py b/project/notes/migrations/0007_alter_note_owner.py new file mode 100644 index 0000000..c307cfd --- /dev/null +++ b/project/notes/migrations/0007_alter_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-01 13:51 + +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), + ('notes', '0006_note_owner'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='owner', + field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/migrations/0008_alter_note_owner.py b/project/notes/migrations/0008_alter_note_owner.py new file mode 100644 index 0000000..cfc1777 --- /dev/null +++ b/project/notes/migrations/0008_alter_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-01 14:48 + +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), + ('notes', '0007_alter_note_owner'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='owner', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/models.py b/project/notes/models.py index 78a5a74..b2363f6 100644 --- a/project/notes/models.py +++ b/project/notes/models.py @@ -1,5 +1,7 @@ from django.db import models from django.utils.timezone import now +from django.contrib.auth import get_user_model +User = get_user_model() # Create your models here. @@ -7,6 +9,7 @@ class Note(models.Model): title = models.CharField(max_length=20) content = models.CharField(max_length=1024) date_created = models.DateTimeField(default=now, editable=False) + owner = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): return self.title diff --git a/project/notes/views.py b/project/notes/views.py index a7165a7..957dcea 100644 --- a/project/notes/views.py +++ b/project/notes/views.py @@ -1,9 +1,15 @@ -from django.shortcuts import render +from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets from .serializers import NoteSerializer from .models import Note class NoteViewSet(viewsets.ModelViewSet): - queryset = Note.objects.all().order_by('date_created') + permission_classes = [IsAuthenticated] serializer_class = NoteSerializer + queryset = Note.objects.all() + + def get_queryset(self): + user = self.request.user + queryset = Note.objects.filter(owner=user).order_by('date_created') + return queryset