Skip to content

Commit

Permalink
Merge Branches
Browse files Browse the repository at this point in the history
  • Loading branch information
shiva-menta committed Apr 26, 2024
1 parent 4dd214e commit 8ba8e2e
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 143 deletions.
53 changes: 53 additions & 0 deletions backend/courses/migrations/0065_auto_20240425_2031.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 3.2.23 on 2024-04-26 00:31

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('courses', '0064_auto_20240120_1914'),
]

operations = [
migrations.RemoveField(
model_name='comment',
name='course',
),
migrations.RemoveField(
model_name='comment',
name='likes',
),
migrations.RemoveField(
model_name='comment',
name='parent_id',
),
migrations.AddField(
model_name='comment',
name='base',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.comment'),
),
migrations.AddField(
model_name='comment',
name='downvotes',
field=models.ManyToManyField(help_text='The number of downvotes a comment gets.', related_name='downvotes', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='comment',
name='parent',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='courses.comment'),
),
migrations.AddField(
model_name='comment',
name='section',
field=models.ForeignKey(help_text='\nThe section with which a comment is associated. Section was chosen instead of topics for\nhosting comments because topics are SOFT STATE and are recomputed regularly.\n', null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.section'),
),
migrations.AddField(
model_name='comment',
name='upvotes',
field=models.ManyToManyField(help_text='The number of upvotes a comment gets.', related_name='upvotes', to=settings.AUTH_USER_MODEL),
),
]
45 changes: 30 additions & 15 deletions backend/courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,37 +1508,52 @@ class Comment(models.Model):
null=True,
related_name="comments"
)
likes = models.ManyToManyField(
upvotes = models.ManyToManyField(
get_user_model(),
help_text="The number of likes a comment gets."
related_name="upvotes",
help_text="The number of upvotes a comment gets."
)
course = models.ForeignKey(
Course,
downvotes = models.ManyToManyField(
get_user_model(),
related_name="downvotes",
help_text="The number of downvotes a comment gets."
)
section = models.ForeignKey(
Section,
on_delete=models.CASCADE,
help_text=dedent(
"""
The course with which a comment is associated. Course was chosen instead of topics for
The section with which a comment is associated. Section was chosen instead of topics for
hosting comments because topics are SOFT STATE and are recomputed regularly.
"""
)
),
null=True
)

parent_id = models.ForeignKey(
base = models.ForeignKey(
"self",
on_delete=models.SET_NULL, # redundant due to special deletion conditions
null=True
null=True,
)
parent = models.ForeignKey(
"self",
on_delete=models.SET_NULL, # similarly redundant
null=True,
related_name="children"
)
path = models.TextField(db_index=True)

def level(self):
return len(self.path.split('.'))
#def save(self, **kwargs):
# parent_comment = Comment.objects.filter(id=self.parent_id).first()
#prefix = parent_comment.path + '.' if parent_comment else ''
# super().save(**kwargs)
#print(self.id)
# self.path = prefix + '{:0{}d}'.format(self.id, self._N)
# super().save(**kwargs)

def save(self, **kwargs):
parent_comment = Comment.objects.filter(id=self.parent_id).first()
prefix = parent_comment.path + '.' if parent_comment else ''
super().save(**kwargs)
self.path = prefix + '{:0{}d}'.format(self.id, self._N)
if self.base is None:
self.base = self
super().save(**kwargs)

def delete(self, **kwargs):
if Comment.objects.filter(parent_id=self).exists():
Expand Down
19 changes: 19 additions & 0 deletions backend/courses/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,22 @@ def get_semesters(semesters: str = None) -> list[str]:
if s not in possible_semesters:
raise ValueError(f"Provided semester {s} was not found in the db.")
return sorted(semesters)

def get_section_from_course_instructor_semester(course_code, professors, semester):
"""
Attempts to return a course section that matches the given parameters.
ValueError is raised if the section does not exist.
"""
sections = Section.objects.prefetch_related('instructors').filter(
course__full_code=course_code,
course__semester=semester
)

professors_query = Q(instructors__username=professors[0])
for professor in professors[1:]:
professors_query &= Q(instructors__username=professor)
matching_sections = sections.filter(professors_query).distinct()

if matching_sections.count() == 1:
return matching_sections.first()
raise ValueError(f"No section exists with course code ({course_code}), professor ({professor}), semester ({semester})")
72 changes: 0 additions & 72 deletions backend/courses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,77 +570,5 @@ def delete(self, request):
return JsonResponse(res, status=status.HTTP_409_CONFLICT)
return JsonResponse(res, status=status.HTTP_200_OK)

class CommentsView(generics.ListAPIView):
"""
get: Get a list of all comments for the specified section.
"""

schema = PcxAutoSchema(
response_codes={
"comments": {
"GET": {
200: "Comments retrieved successfully.",
},
}
},
custom_path_parameter_desc={
"comments": {
"GET": {
"full_code": (
"The code of the section which this comment applies to, in the "
"form '{dept code}-{course code}', e.g. `CIS-120` for "
"CIS-120."
),
"semester": (
"The semester of the course (of the form YYYYx where x is A [for spring], "
"B [summer], or C [fall]), e.g. '2019C' for fall 2019. Alternatively, you "
"can just pass 'current' for the current semester."
)
},
"POST": {
"full_code": (
"The code of the section which this comment applies to, in the "
"form '{dept code}-{course code}', e.g. `CIS-120` for "
"CIS-120."
),
"semester": (
"The semester of the course (of the form YYYYx where x is A [for spring], "
"B [summer], or C [fall]), e.g. '2019C' for fall 2019. Alternatively, you "
"can just pass 'current' for the current semester."
),
"text": "The text of the comment.",
"parent_id": "The id of the parent comment, if this is a reply.",
}
}
},
)

serializer_class = CommentSerializer
http_method_names = ["get"]
lookup_field = "courses__full_code"

def get(self, request, *args, **kwargs):
queryset = Comment.objects.filter(
courses__full_code=kwargs["full_code"],
courses__semester=kwargs["semester"]
)
serializer = CommentSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

def post(self, request, *args, **kwargs):
author = request.user
full_code = kwargs["full_code"]
semester = kwargs["semester"]
text = request.data.get("text")
parent_id = request.data.get("parent_id")
if parent_id:
parent = Comment.objects.get(id=parent_id)
comment = Comment(author=author, text=text, parent=parent)
else:
comment = Comment(author=author, text=text, parent=null)
comment.save()





16 changes: 11 additions & 5 deletions backend/review/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
department_reviews,
instructor_for_course_reviews,
instructor_reviews,
handle_vote,
CommentList,
CommentViewSet
)
Expand Down Expand Up @@ -45,13 +46,18 @@
),
path("autocomplete", cache_page(MONTH_IN_SECONDS)(autocomplete), name="review-autocomplete"),
path(
"course_comments/<slug:course_code>/<slug:sort_by>",
cache_page(DAY_IN_SECONDS)(CommentList.as_view()),
"<slug:semester>/course_comments/<slug:course_code>",
CommentList.as_view(),
name="course-comments"
),
path(
"comments",
cache_page(DAY_IN_SECONDS)(CommentViewSet.as_view(actions={'get': 'list', "post": "create", "delete": "destroy", "put": "update"})),
name="course-comments"
"comment/vote/",
handle_vote,
name="comment-vote"
),
path(
"comment",
CommentViewSet.as_view(),
name="comment"
)
]
Loading

0 comments on commit 8ba8e2e

Please sign in to comment.