diff --git a/Pipfile b/Pipfile index 2498513..d1a423d 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,7 @@ djoser = "*" djangorestframework-simplejwt = "*" django-cors-headers = "*" python-decouple = "*" +pytz = "*" [dev-packages] autopep8 = "*" diff --git a/Pipfile.lock b/Pipfile.lock index 87e90aa..ddda774 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "2a24356df675b56c84eb1a0af12b896f6fc152d487c390124b2ed9d68d884ea0" + "sha256": "7c9c7e35e728e8bab00dae7fa5cf2fc35306cd5de74686274d85e5833c8769f4" }, "pipfile-spec": 6, "requires": { @@ -210,32 +210,32 @@ }, "cryptography": { "hashes": [ - "sha256:0f8da300b5c8af9f98111ffd512910bc792b4c77392a9523624680f7956a99d4", - "sha256:35f7c7d015d474f4011e859e93e789c87d21f6f4880ebdc29896a60403328f1f", - "sha256:4789d1e3e257965e960232345002262ede4d094d1a19f4d3b52e48d4d8f3b885", - "sha256:5aa67414fcdfa22cf052e640cb5ddc461924a045cacf325cd164e65312d99502", - "sha256:5d2d8b87a490bfcd407ed9d49093793d0f75198a35e6eb1a923ce1ee86c62b41", - "sha256:6687ef6d0a6497e2b58e7c5b852b53f62142cfa7cd1555795758934da363a965", - "sha256:6f8ba7f0328b79f08bdacc3e4e66fb4d7aab0c3584e0bd41328dce5262e26b2e", - "sha256:706843b48f9a3f9b9911979761c91541e3d90db1ca905fd63fee540a217698bc", - "sha256:807ce09d4434881ca3a7594733669bd834f5b2c6d5c7e36f8c00f691887042ad", - "sha256:83e17b26de248c33f3acffb922748151d71827d6021d98c70e6c1a25ddd78505", - "sha256:96f1157a7c08b5b189b16b47bc9db2332269d6680a196341bf30046330d15388", - "sha256:aec5a6c9864be7df2240c382740fcf3b96928c46604eaa7f3091f58b878c0bb6", - "sha256:b0afd054cd42f3d213bf82c629efb1ee5f22eba35bf0eec88ea9ea7304f511a2", - "sha256:c5caeb8188c24888c90b5108a441c106f7faa4c4c075a2bcae438c6e8ca73cef", - "sha256:ced4e447ae29ca194449a3f1ce132ded8fcab06971ef5f618605aacaa612beac", - "sha256:d1f6198ee6d9148405e49887803907fe8962a23e6c6f83ea7d98f1c0de375695", - "sha256:e124352fd3db36a9d4a21c1aa27fd5d051e621845cb87fb851c08f4f75ce8be6", - "sha256:e422abdec8b5fa8462aa016786680720d78bdce7a30c652b7fadf83a4ba35336", - "sha256:ef8b72fa70b348724ff1218267e7f7375b8de4e8194d1636ee60510aae104cd0", - "sha256:f0c64d1bd842ca2633e74a1a28033d139368ad959872533b1bab8c80e8240a0c", - "sha256:f24077a3b5298a5a06a8e0536e3ea9ec60e4c7ac486755e5fb6e6ea9b3500106", - "sha256:fdd188c8a6ef8769f148f88f859884507b954cc64db6b52f66ef199bb9ad660a", - "sha256:fe913f20024eb2cb2f323e42a64bdf2911bb9738a15dba7d3cce48151034e3a8" + "sha256:103e8f7155f3ce2ffa0049fe60169878d47a4364b277906386f8de21c9234aa1", + "sha256:23df8ca3f24699167daf3e23e51f7ba7334d504af63a94af468f468b975b7dd7", + "sha256:2725672bb53bb92dc7b4150d233cd4b8c59615cd8288d495eaa86db00d4e5c06", + "sha256:30b1d1bfd00f6fc80d11300a29f1d8ab2b8d9febb6ed4a38a76880ec564fae84", + "sha256:35d658536b0a4117c885728d1a7032bdc9a5974722ae298d6c533755a6ee3915", + "sha256:50cadb9b2f961757e712a9737ef33d89b8190c3ea34d0fb6675e00edbe35d074", + "sha256:5f8c682e736513db7d04349b4f6693690170f95aac449c56f97415c6980edef5", + "sha256:6236a9610c912b129610eb1a274bdc1350b5df834d124fa84729ebeaf7da42c3", + "sha256:788b3921d763ee35dfdb04248d0e3de11e3ca8eb22e2e48fef880c42e1f3c8f9", + "sha256:8bc0008ef798231fac03fe7d26e82d601d15bd16f3afaad1c6113771566570f3", + "sha256:8f35c17bd4faed2bc7797d2a66cbb4f986242ce2e30340ab832e5d99ae60e011", + "sha256:b49a88ff802e1993b7f749b1eeb31134f03c8d5c956e3c125c75558955cda536", + "sha256:bc0521cce2c1d541634b19f3ac661d7a64f9555135e9d8af3980965be717fd4a", + "sha256:bc5b871e977c8ee5a1bbc42fa8d19bcc08baf0c51cbf1586b0e87a2694dde42f", + "sha256:c43ac224aabcbf83a947eeb8b17eaf1547bce3767ee2d70093b461f31729a480", + "sha256:d15809e0dbdad486f4ad0979753518f47980020b7a34e9fc56e8be4f60702fac", + "sha256:d7d84a512a59f4412ca8549b01f94be4161c94efc598bf09d027d67826beddc0", + "sha256:e029b844c21116564b8b61216befabca4b500e6816fa9f0ba49527653cae2108", + "sha256:e8a0772016feeb106efd28d4a328e77dc2edae84dfbac06061319fdb669ff828", + "sha256:e944fe07b6f229f4c1a06a7ef906a19652bdd9fd54c761b0ff87e83ae7a30354", + "sha256:eb40fe69cfc6f5cdab9a5ebd022131ba21453cf7b8a7fd3631f45bbf52bed612", + "sha256:fa507318e427169ade4e9eccef39e9011cdc19534f55ca2f36ec3f388c1f70f3", + "sha256:ffd394c7896ed7821a6d13b24657c6a34b6e2650bd84ae063cf11ccffa4f1a97" ], "markers": "python_version >= '3.6'", - "version": "==39.0.1" + "version": "==39.0.2" }, "defusedxml": { "hashes": [ @@ -255,11 +255,11 @@ }, "django-cors-headers": { "hashes": [ - "sha256:37e42883b5f1f2295df6b4bba96eb2417a14a03270cb24b2a07f021cd4487cf4", - "sha256:f9dc6b4e3f611c3199700b3e5f3398c28757dcd559c2f82932687f3d0443cfdf" + "sha256:5fbd58a6fb4119d975754b2bc090f35ec160a8373f276612c675b00e8a138739", + "sha256:684180013cc7277bdd8702b80a3c5a4b3fcae4abb2bf134dceb9f5dfe300228e" ], "index": "pypi", - "version": "==3.13.0" + "version": "==3.14.0" }, "django-templated-mail": { "hashes": [ @@ -396,11 +396,11 @@ }, "python-decouple": { "hashes": [ - "sha256:1596dad2670cca5b1f87d087d9adb6a1958c590df346b85d4b19a9d6f0d52cef", - "sha256:e88a8d6bdf3b07d471a854099e455e20a6fa7a4d6ecf8631b250e3db654336e6" + "sha256:ba6e2657d4f376ecc46f77a3a615e058d93ba5e465c01bbe57289bfb7cce680f", + "sha256:d0d45340815b25f4de59c974b855bb38d03151d81b037d9e3f463b0c9f8cbd66" ], "index": "pypi", - "version": "==3.7" + "version": "==3.8" }, "python3-openid": { "hashes": [ @@ -414,6 +414,7 @@ "sha256:01a0681c4b9684a28304615eba55d1ab31ae00bf68ec157ec3708a8182dbbcd0", "sha256:78f4f37d8198e0627c5f1143240bb0206b8691d8d7ac6d78fee88b78733f8c4a" ], + "index": "pypi", "version": "==2022.7.1" }, "requests": { @@ -492,11 +493,11 @@ "develop": { "autopep8": { "hashes": [ - "sha256:be5bc98c33515b67475420b7b1feafc8d32c1a69862498eda4983b45bffd2687", - "sha256:d27a8929d8dcd21c0f4b3859d2d07c6c25273727b98afc984c039df0f0d86566" + "sha256:86e9303b5e5c8160872b2f5ef611161b2893e9bfe8ccc7e2f76385947d57a2f1", + "sha256:f9849cdd62108cb739dbcdbfb7fdcc9a30d1b63c4cc3e1c1f893b5360941b61c" ], "index": "pypi", - "version": "==2.0.1" + "version": "==2.0.2" }, "pycodestyle": { "hashes": [ diff --git a/project/accounts/serializers.py b/project/accounts/serializers.py new file mode 100644 index 0000000..2d990c4 --- /dev/null +++ b/project/accounts/serializers.py @@ -0,0 +1,13 @@ +from django.contrib.auth.models import User +from rest_framework import serializers +from notes.models import Note + + +class CustomUserSerializer(serializers.ModelSerializer): + notes = serializers.PrimaryKeyRelatedField( + many=True, allow_null=True, queryset=Note.objects.all()) + + class Meta: + model = User + fields = ['id', 'username', 'notes',] + read_only_fields = ['id', 'notes'] diff --git a/project/config/settings.py b/project/config/settings.py index 3a92483..452fe0b 100644 --- a/project/config/settings.py +++ b/project/config/settings.py @@ -41,7 +41,8 @@ INSTALLED_APPS = [ 'rest_framework.authtoken', 'notes.apps.NotesConfig', 'corsheaders', - 'djoser' + 'djoser', + 'accounts', ] MIDDLEWARE = [ @@ -111,7 +112,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'Asia/Manila' USE_I18N = True @@ -146,6 +147,9 @@ DJOSER = { 'SEND_ACTIVATION_EMAIL': True, 'SEND_CONFIRMATION_EMAIL': True, 'ACTIVATION_URL': 'activation/{uid}/{token}', + 'SERIALIZERS': { + 'user': 'accounts.serializers.CustomUserSerializer' + }, } EMAIL_HOST = 'sandbox.smtp.mailtrap.io' diff --git a/project/db.sqlite3 b/project/db.sqlite3 index 4b4b685..be67ebf 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/migrations/0009_alter_note_content.py b/project/notes/migrations/0009_alter_note_content.py new file mode 100644 index 0000000..b038538 --- /dev/null +++ b/project/notes/migrations/0009_alter_note_content.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.7 on 2023-03-03 15:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0008_alter_note_owner'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='content', + field=models.TextField(), + ), + ] diff --git a/project/notes/migrations/0010_alter_note_owner.py b/project/notes/migrations/0010_alter_note_owner.py new file mode 100644 index 0000000..3b6843d --- /dev/null +++ b/project/notes/migrations/0010_alter_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-04 11:06 + +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', '0009_alter_note_content'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='owner', + field=models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/migrations/0011_alter_note_owner.py b/project/notes/migrations/0011_alter_note_owner.py new file mode 100644 index 0000000..ffbbe8e --- /dev/null +++ b/project/notes/migrations/0011_alter_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-04 11:16 + +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', '0010_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/migrations/0012_remove_note_date_created_note_last_updated.py b/project/notes/migrations/0012_remove_note_date_created_note_last_updated.py new file mode 100644 index 0000000..48c0740 --- /dev/null +++ b/project/notes/migrations/0012_remove_note_date_created_note_last_updated.py @@ -0,0 +1,22 @@ +# Generated by Django 4.1.7 on 2023-03-04 13:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0011_alter_note_owner'), + ] + + operations = [ + migrations.RemoveField( + model_name='note', + name='date_created', + ), + migrations.AddField( + model_name='note', + name='last_updated', + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/project/notes/migrations/0013_remove_note_last_updated_note_date_created.py b/project/notes/migrations/0013_remove_note_last_updated_note_date_created.py new file mode 100644 index 0000000..5b16873 --- /dev/null +++ b/project/notes/migrations/0013_remove_note_last_updated_note_date_created.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.7 on 2023-03-04 13:58 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0012_remove_note_date_created_note_last_updated'), + ] + + operations = [ + migrations.RemoveField( + model_name='note', + name='last_updated', + ), + migrations.AddField( + model_name='note', + name='date_created', + field=models.DateTimeField(default=django.utils.timezone.now, editable=False), + ), + ] diff --git a/project/notes/models.py b/project/notes/models.py index 78a5a74..82416b3 100644 --- a/project/notes/models.py +++ b/project/notes/models.py @@ -1,12 +1,15 @@ 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. class Note(models.Model): title = models.CharField(max_length=20) - content = models.CharField(max_length=1024) + content = models.TextField() 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/serializers.py b/project/notes/serializers.py index c4c74cd..b4189a4 100644 --- a/project/notes/serializers.py +++ b/project/notes/serializers.py @@ -3,6 +3,11 @@ from .models import Note class NoteSerializer(serializers.HyperlinkedModelSerializer): + owner = serializers.ReadOnlyField(source='owner.username') + date_created = serializers.DateTimeField( + format="%d-%m-%Y %I:%M%p", read_only=True) + class Meta: model = Note - fields = ('id', 'title', 'content', 'date_created') + fields = ('id', 'title', 'content', 'date_created', 'owner') + read_only_fields = ('id', 'date_created', 'owner') diff --git a/project/notes/views.py b/project/notes/views.py index a7165a7..ee53847 100644 --- a/project/notes/views.py +++ b/project/notes/views.py @@ -1,9 +1,18 @@ -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 + + def perform_create(self, serializer): + serializer.save(owner=self.request.user)