Skip to content

Commit

Permalink
Additional View Functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
shiva-menta committed Apr 26, 2024
1 parent 6d62314 commit a2b60d0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
26 changes: 25 additions & 1 deletion backend/courses/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,28 @@ def get_parent(self, obj):

class Meta:
model = Comment
fields = ['id', 'text', 'created_at', 'modified_at', 'author_name', 'votes', 'section', 'base', 'parent', 'path']
fields = ['id', 'text', 'created_at', 'modified_at', 'author_name', 'votes', 'section', 'base', 'parent', 'path']

class CommentListSerializer(serializers.ModelSerializer):
author_name = serializers.CharField(source="author.username", read_only=True)
votes = serializers.SerializerMethodField()
section = serializers.CharField(source="section.full_code", read_only=True)
base = serializers.SerializerMethodField()
parent = serializers.SerializerMethodField()
user_upvoted = serializers.BooleanField()
user_downvoted = serializers.BooleanField()

def get_votes(self, obj):
return len(obj.upvotes.values_list('id')) - len(obj.downvotes.values_list('id'))
def get_base(self, obj):
if obj.base is None:
return None
return obj.base.id
def get_parent(self, obj):
if obj.parent is None:
return None
return obj.parent.id

class Meta:
model = Comment
fields = ['id', 'text', 'created_at', 'modified_at', 'author_name', 'votes', 'section', 'base', 'parent', 'path', 'user_upvoted', 'user_downvoted']
10 changes: 8 additions & 2 deletions backend/review/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
instructor_for_course_reviews,
instructor_reviews,
handle_vote,
get_comment_children,
CommentList,
CommentViewSet
)
Expand Down Expand Up @@ -57,12 +58,17 @@
),
path(
"comment/<slug:pk>",
CommentViewSet.as_view(actions={"delete": "destroy", "put": "update"}),
CommentViewSet.as_view(actions={'get': 'retrieve', "delete": "destroy", "put": "update"}),
name="comment"
),
path(
"comment/children/<slug:pk>",
get_comment_children,
name="comment-children"
),
path(
"comment",
CommentViewSet.as_view(actions={'get': 'list', "post": "create"}),
CommentViewSet.as_view(actions={"post": "create"}),
name="comment"
)
]
26 changes: 22 additions & 4 deletions backend/review/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import Counter, defaultdict

from dateutil.tz import gettz
from django.db.models import F, Max, OuterRef, Q, Subquery, Value, IntegerField, Count
from django.db.models import F, Max, OuterRef, Q, Subquery, Value, IntegerField, Count, Exists
from django.db.models.functions import Concat, Substr, StrIndex, Cast
from django.http import Http404
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -43,7 +43,7 @@
get_status_updates_map,
make_subdict,
)
from courses.serializers import CommentSerializer
from courses.serializers import CommentSerializer, CommentListSerializer

"""
You might be wondering why these API routes are using the @api_view function decorator
Expand Down Expand Up @@ -868,7 +868,7 @@ def get(self, request, semester, course_code):
page = request.query_params.get("page") or 0
page_size = request.query_params.get("page_size") or 10

queryset = self.get_queryset()
queryset = og_queryset = self.get_queryset()

# add filters
if semester_arg != "all":
Expand All @@ -888,9 +888,19 @@ def get(self, request, semester, course_code):
queryset = queryset.all().order_by("-base_id", "path")

# apply pagination (not sure how django handles OOB errors)
user_upvotes = queryset.filter(upvotes=request.user, id=OuterRef('id'))
user_downvotes = queryset.filter(downvotes=request.user, id=OuterRef('id'))
queryset = queryset.annotate(
user_upvoted=Exists(user_upvotes),
user_downvoted=Exists(user_downvotes)
)
queryset = queryset.all()[page*page_size:(page+1)*page_size]

response_body = {"comments": CommentListSerializer(queryset, many=True).data}
if semester_arg == "all":
response_body["semesters"] = list(og_queryset.values_list("section__course__semester", flat=True).distinct())

return Response(CommentSerializer(queryset, many=True).data, status=status.HTTP_200_OK)
return Response(response_body, status=status.HTTP_200_OK)

def get_queryset(self):
course_code = self.kwargs["course_code"]
Expand Down Expand Up @@ -1085,3 +1095,11 @@ def handle_vote(request):
comment.downvotes.remove(user)

return Response(CommentSerializer(comment).data, status=status.HTTP_201_CREATED)

@api_view(["GET"])
def get_comment_children(request, pk):
"""
Gets all DIRECT children for a comment.
"""
queryset = Comment.objects.filter(parent__id=pk)
return Response(CommentSerializer(queryset, many=True).data, status=status.HTTP_200_OK)
13 changes: 12 additions & 1 deletion backend/tests/courses/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,13 @@ def get_comments(self, semester, code, ordering):
self.client.force_login(self.user1)
response = self.client.get(f"{base_url}?{encoded_params}")
self.client.logout()
return response.data["comments"]

def get_comment_children(self, id):
base_url = reverse("comment-children", kwargs={"pk": id})
self.client.force_login(self.user1)
response = self.client.get(base_url)
self.client.logout()
return response.data

def create_comment(self, username, instructor, code, semester, parent_id):
Expand Down Expand Up @@ -1505,4 +1512,8 @@ def test_edit_comment(self):
for comment in comments:
if comment["text"] == "new comment!":
return
self.assertFalse(True)
self.assertFalse(True)

def test_get_comment_children(self):
comments = self.get_comment_children(self.id1)
self.assertEqual(len(comments), 2)

0 comments on commit a2b60d0

Please sign in to comment.