Added logic to support public notes

This commit is contained in:
keannu125 2023-03-29 19:37:42 +08:00
parent 53dc50c55a
commit 390040d460
7 changed files with 32 additions and 6 deletions

View file

@ -26,9 +26,10 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY') SECRET_KEY = os.environ.get('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production! # 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 # Application definition

Binary file not shown.

View file

@ -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),
),
]

View file

@ -10,6 +10,7 @@ class Note(models.Model):
content = models.TextField() content = models.TextField()
date_created = models.DateTimeField(default=now, editable=False) date_created = models.DateTimeField(default=now, editable=False)
owner = models.ForeignKey(User, on_delete=models.CASCADE) owner = models.ForeignKey(User, on_delete=models.CASCADE)
public = models.BooleanField(default=False)
def __str__(self): def __str__(self):
return self.title return self.title

View file

@ -9,5 +9,5 @@ class NoteSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = Note model = Note
fields = ('id', 'title', 'content', 'date_created', 'owner') fields = ('id', 'title', 'content', 'date_created', 'owner', 'public')
read_only_fields = ('id', 'date_created', 'owner') read_only_fields = ('id', 'date_created', 'owner')

View file

@ -9,5 +9,5 @@ router.register(r'notes', views.NoteViewSet)
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.
urlpatterns = [ urlpatterns = [
path('', include(router.urls)), path('', include(router.urls)),
path('public_notes/', views.PublicNoteViewSet.as_view())
] ]

View file

@ -1,11 +1,11 @@
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets from rest_framework import viewsets, generics
from .serializers import NoteSerializer from .serializers import NoteSerializer
from .models import Note from .models import Note
class NoteViewSet(viewsets.ModelViewSet): class NoteViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
serializer_class = NoteSerializer serializer_class = NoteSerializer
queryset = Note.objects.all() queryset = Note.objects.all()
@ -16,3 +16,9 @@ class NoteViewSet(viewsets.ModelViewSet):
def perform_create(self, serializer): def perform_create(self, serializer):
serializer.save(owner=self.request.user) serializer.save(owner=self.request.user)
class PublicNoteViewSet(generics.ListAPIView):
# permission_classes = [IsAuthenticated]
serializer_class = NoteSerializer
queryset = Note.objects.filter(public=True)