Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions forum/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from .models import Post, Comment
from rest_framework import routers, serializers, viewsets

# Serializers define the API representation.
class CommentSerializer(serializers.ModelSerializer):
author = serializers.CharField(source="author.username")

class Meta:
model = Comment
fields = ["id", "author", "content", "created_at"]

class PostListSerializer(serializers.ModelSerializer):
author = serializers.CharField(source="author.username")

class Meta:
model = Post
fields = ["id", "author", "title", "content", "created_at"]

class PostDetailSerializer(serializers.ModelSerializer):
author = serializers.CharField(source="author.username")
comments = CommentSerializer(many=True, read_only=True)

class Meta:
model = Post
fields = [
"id",
"author",
"title",
"content",
"created_at",
"comments",
]

# ViewSets define the view behavior.
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()

def get_queryset(self):
return Post.objects.select_related("author").prefetch_related("comments__author")

def get_serializer_class(self):
if self.action == "list":
return PostListSerializer
return PostDetailSerializer


# Routers provide a way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r"posts", PostViewSet)

2 changes: 1 addition & 1 deletion forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Meta:
ordering = ['-created_at']

class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
author = models.ForeignKey(User, on_delete=models.CASCADE)
content = MDTextField()
created_at = models.DateTimeField(auto_now_add=True)
Expand Down
5 changes: 3 additions & 2 deletions forum/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import path
from . import views
from django.urls import include, path
from . import views, api

urlpatterns = [
path('', views.index, name='index'),
Expand All @@ -14,4 +14,5 @@
path('register/', views.RegisterView.as_view(), name='register'),
path('about/', views.about_view, name='about'),
path('logout/', views.logout_view, name='logout'),
path('api/', include(api.router.urls)),
]
2 changes: 1 addition & 1 deletion forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get(self, request, post_id):
if request.user.is_authenticated:
forms = MDEditorCommentForm(user=request.user, post=post)

comments = post.comment_set.all().order_by('created_at')
comments = post.comments.all().order_by('created_at')

post.content = markdown.markdown(
post.content, extensions=['extra', 'codehilite', 'toc']
Expand Down
10 changes: 10 additions & 0 deletions lean_forum/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
"rest_framework",
'mdeditor',
'webpush',
'forum',
Expand Down Expand Up @@ -143,4 +144,13 @@
"VAPID_ADMIN_EMAIL": "admin@example.com"
}

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly",
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 20

}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ packaging>=25.0
sqlparse>=0.5.3
django-webpush>=0.3.6
bleach>=6.3.0
djangorestframework
openai