Merge pull request #7 from lemeow125/feature/public_notes

Feature/public notes
This commit is contained in:
lemeow125 2023-03-29 20:15:15 +08:00 committed by GitHub
commit c9d25f73c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 4 deletions

View file

@ -28,7 +28,8 @@ 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 = False
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,5 +1,5 @@
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
@ -16,3 +16,8 @@ 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):
serializer_class = NoteSerializer
queryset = Note.objects.filter(public=True)