From 390040d460e3a942ae1e725ae3c48a44a5ad9cd0 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Wed, 29 Mar 2023 19:37:42 +0800 Subject: [PATCH] Added logic to support public notes --- project/config/settings.py | 5 +++-- project/db.sqlite3 | Bin 241664 -> 241664 bytes project/notes/migrations/0014_note_public.py | 18 ++++++++++++++++++ project/notes/models.py | 1 + project/notes/serializers.py | 2 +- project/notes/urls.py | 2 +- project/notes/views.py | 10 ++++++++-- 7 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 project/notes/migrations/0014_note_public.py diff --git a/project/config/settings.py b/project/config/settings.py index eb769e6..30edc1c 100644 --- a/project/config/settings.py +++ b/project/config/settings.py @@ -26,9 +26,10 @@ BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = False +DEBUG = True -ALLOWED_HOSTS = ['lemeow125.github.io', 'keannu126.pythonanywhere.com'] +ALLOWED_HOSTS = ['lemeow125.github.io', + 'keannu126.pythonanywhere.com', '127.0.0.1', 'localhost'] # Application definition diff --git a/project/db.sqlite3 b/project/db.sqlite3 index be67ebffec369e1b9a2488949b76098513b8083b..340949e4a6aee97d862a9e347749546e9e765af9 100644 GIT binary patch delta 890 zcmah{O-vI(6yBL-Z9&`NPnxKq+g1ewEwkOfc1?g*F^c?#R=Jh_EVQAvm9{A$iHO9j znyTy3M50DdSd#T<^b)*q@-}NOB7{E=EJc&iBwe;@24!qzVyOhC$GYZF0aV*qCk?& zaaC1uD*Z^yJ52Fpi}{m|CxD#>etj$oGy~|;zWP81ymv*z(_q0N`3)AE4J^yKa3rZ7 zWORVqv~SbkC+cMF`=#62NIIR=za+sfs9WkI{EiN=?9(<=zyaFyUnu~^oEYk^?e> z4}(6EUrdna#o4=IHO9~9Gf_M%dGhi3tcQ<_5v)NgT`I(P>!?=A+3QBcd8E6#Rw#NeWKRU7h59+a)YXUVXC_J^;)`l8{YX zWuadmc}_or+GiGC)d-w9^sc~maJ-?yYkq-YjS(W9LR&~Di}C?oI8P6PmG*)E*8uhHVQ8->t=^!}26{<;dyG`kYOVFvhO@CF_wZJ83Xc^m ox;!$6NeBK>+86`X)I{iUwQ`P7Bt>+)+#ZSJq(>R}CPVlB2BzczYXATM delta 549 zcmZp8z}N7AZ-TVoVFm^UYamtzVlg0&ov35Xcz9#N5`JbS_L9l$0znWkGAlA7#JDD}(hp@++GHSPFJoY+ zU}$D#WN2k-qGxGhVPI*Tn39{B2PP)h=ZQ~t&99L$FjX)xw=y)fGBMFJGcYhRHUt`9 z0wF+(Hh;-C|G>JMTaWL;c7p^)C1ys4?ZL^6)7cr_r=NCYbY`h`WEYIq zo>R_&U0hL-v4ws5+-yckMz!hv35<=?bMhIhx1Y{oe9H(@CBfCC3sk19&DbJ1{ZJmG zGNan|XL*c_jNAYFGcpQn`oN;FoMi!H0tbTt0|WnW{;!(_8{Y5>u`zQ3LyMJ*7f3Sl z-D2Ro1q`PPeA{oyF*P!>sslw8r*BkXa@rm+m3a!&^aBb^F4KX`A|yVDsliBC0HjZ3 zngJ6F8zcW62L3xh2VCNxe#f3kgM*R(5d#pZZ0B)c+Ri_5f&BJI{!B^&Y+$!O0mjy0 meq%F5MNY?(#NrePg0ZAnSs8#~1LGE_AXtoT1x(8dm>mFUS)&dB diff --git a/project/notes/migrations/0014_note_public.py b/project/notes/migrations/0014_note_public.py new file mode 100644 index 0000000..535c09c --- /dev/null +++ b/project/notes/migrations/0014_note_public.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.7 on 2023-03-29 10:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0013_remove_note_last_updated_note_date_created'), + ] + + operations = [ + migrations.AddField( + model_name='note', + name='public', + field=models.BooleanField(default=False), + ), + ] diff --git a/project/notes/models.py b/project/notes/models.py index 82416b3..3ea6888 100644 --- a/project/notes/models.py +++ b/project/notes/models.py @@ -10,6 +10,7 @@ class Note(models.Model): content = models.TextField() date_created = models.DateTimeField(default=now, editable=False) owner = models.ForeignKey(User, on_delete=models.CASCADE) + public = models.BooleanField(default=False) def __str__(self): return self.title diff --git a/project/notes/serializers.py b/project/notes/serializers.py index b4189a4..ce97bbb 100644 --- a/project/notes/serializers.py +++ b/project/notes/serializers.py @@ -9,5 +9,5 @@ class NoteSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Note - fields = ('id', 'title', 'content', 'date_created', 'owner') + fields = ('id', 'title', 'content', 'date_created', 'owner', 'public') read_only_fields = ('id', 'date_created', 'owner') diff --git a/project/notes/urls.py b/project/notes/urls.py index dd22763..45b83d6 100644 --- a/project/notes/urls.py +++ b/project/notes/urls.py @@ -9,5 +9,5 @@ router.register(r'notes', views.NoteViewSet) # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), - + path('public_notes/', views.PublicNoteViewSet.as_view()) ] diff --git a/project/notes/views.py b/project/notes/views.py index ee53847..4b475ff 100644 --- a/project/notes/views.py +++ b/project/notes/views.py @@ -1,11 +1,11 @@ from rest_framework.permissions import IsAuthenticated -from rest_framework import viewsets +from rest_framework import viewsets, generics from .serializers import NoteSerializer from .models import Note class NoteViewSet(viewsets.ModelViewSet): - permission_classes = [IsAuthenticated] + # permission_classes = [IsAuthenticated] serializer_class = NoteSerializer queryset = Note.objects.all() @@ -16,3 +16,9 @@ class NoteViewSet(viewsets.ModelViewSet): def perform_create(self, serializer): serializer.save(owner=self.request.user) + + +class PublicNoteViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] + serializer_class = NoteSerializer + queryset = Note.objects.filter(public=True)