Skip to content

Commit

Permalink
modified frontend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
el-agua committed Sep 8, 2024
1 parent 1cdf8fa commit 8258892
Show file tree
Hide file tree
Showing 9 changed files with 17,784 additions and 29,053 deletions.
5 changes: 5 additions & 0 deletions backend/courses/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from review.management.commands.mergeinstructors import resolve_duplicates



logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -740,12 +741,16 @@ def get_section_from_course_instructor_semester(course_code, professors, semeste
sections = Section.objects.prefetch_related("instructors").filter(
course__topic__most_recent__full_code=course_topic_parent, course__semester=semester
)
print(sections.first())
print(sections[0].instructors)
print("HIGH")
professors_query = Q(instructors__name=professors[0])
for professor in professors[1:]:
professors_query &= Q(instructors__name=professor)
matching_sections = sections.filter(professors_query).distinct()
if matching_sections.count() == 1:
return matching_sections.first()
return matching_sections.first()
raise ValueError(
f"""No section exists with course code ({course_code}), professor ({professors[0]}),
semester ({semester})"""
Expand Down
31 changes: 20 additions & 11 deletions backend/review/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,10 @@ class CommentList(generics.ListAPIView):
)
serializer_class = CommentSerializer
http_method_names = ["get"]
permission_classes = [IsAuthenticated]
# permission_classes = [IsAuthenticated]

def get(self, request, semester, course_code):
print("HELLO")
semester_arg = request.query_params.get("semester") or "all"
instructor = request.query_params.get("instructor") or "all"
sort_by = request.query_params.get("sort_by") or "oldest"
Expand All @@ -894,18 +895,22 @@ def get(self, request, semester, course_code):
queryset = queryset.annotate(
base_votes=Count("base__upvotes") - Count("base__downvotes")
).order_by("-base_votes", "base_id", "path")
queryset = queryset.annotate(
semester=F("section__course__semester"),
)
elif sort_by == "oldest":
queryset = queryset.all().order_by("path")
elif sort_by == "newest":
queryset = queryset.all().order_by("-base_id", "path")

print(queryset)
# 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]
if queryset:
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":
Expand All @@ -922,7 +927,7 @@ def get_queryset(self):
if semester == "all":
course = Course.objects.filter(full_code=course_code).latest("semester")
else:
course = get_course_from_code_semester(course_code, semester)
course = get_course_from_code_semester(course_code, None)
except Http404:
return Response({"message": "Course not found."}, status=status.HTTP_404_NOT_FOUND)
topic = course.topic
Expand Down Expand Up @@ -993,7 +998,7 @@ class CommentViewSet(viewsets.ModelViewSet):

serializer_class = CommentSerializer
http_method_names = ["get", "post", "delete", "put"]
permission_classes = [IsAuthenticated]
# permission_classes = [IsAuthenticated]
queryset = Comment.objects.all()

def retrieve(self, request, pk=None):
Expand All @@ -1019,16 +1024,20 @@ def create(self, request):
request.data.get("instructor"),
request.data.get("semester"),
)
print(section)
except Exception as e:
print(e)
print("hi")
return Response({"message": "Section not found."}, status=status.HTTP_404_NOT_FOUND)

# create comment and send response
parent_id = request.data.get("parent")
parent = get_object_or_404(Comment, pk=parent_id) if parent_id is None else None
print(parent_id)
parent = get_object_or_404(Comment, pk=parent_id) if parent_id is not None else None
comment = Comment.objects.create(
text=request.data.get("text"), author=request.user, section=section, parent=parent
)
print("this is a commnet lol")
base = parent.base if parent else comment
prefix = parent.path + "." if parent else ""
path = prefix + "{:0{}d}".format(comment.id, 10)
Expand Down
2 changes: 1 addition & 1 deletion frontend/review/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"workerize-loader": "^1.3.0"
},
"scripts": {
"dev": "NODE_OPTIONS=--openssl-legacy-provider react-scripts start",
"dev": "react-scripts start",
"start": "NODE_OPTIONS=--openssl-legacy-provider react-scripts start",
"build": "NODE_OPTIONS=--openssl-legacy-provider react-scripts build",
"test": "exit 0",
Expand Down
8 changes: 4 additions & 4 deletions frontend/review/src/components/Comments/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
export const Comment = forwardRef(({ comment, isReply, isUserComment }, ref) => {
const [showReplies, setShowReplies] = useState(false);
const [replies, setReplies] = useState([]);
const [seeMore, setSeeMore] = useState(comment.content.length < 150);
const [seeMore, setSeeMore] = useState(comment.text.length < 150);

const [liked, setLiked] = useState(false);
const [disliked, setDisliked] = useState(false);
Expand All @@ -27,10 +27,10 @@ export const Comment = forwardRef(({ comment, isReply, isUserComment }, ref) =>
<sub>{formatDate(comment.modified_at)}</sub>
</div>
{seeMore ? (
<p>{comment.content}</p>
<p>{comment.text}</p>
) : (
<>
<p>{truncateText(comment.content, 150)}</p>
<p>{truncateText(comment.text, 150)}</p>
<button
className=" btn-borderless btn"
onClick={() => setSeeMore(true)}
Expand All @@ -41,7 +41,7 @@ export const Comment = forwardRef(({ comment, isReply, isUserComment }, ref) =>
)}
<div className="icon-wrapper">
<button className={`btn icon ${liked ? "active" : ""}`} onClick={() => {setLiked(!liked); disliked && setDisliked(false)}}><FontAwesomeIcon icon={faThumbsUp} /></button>
<span>{comment.likes + liked - disliked}</span>
<span>{comment.votes + liked - disliked}</span>
<button className={`btn icon ${disliked ? "active" : ""}`} onClick={() => {setDisliked(!disliked); liked && setLiked(false)}}><FontAwesomeIcon icon={faThumbsDown} /></button>
</div>
{comment.replies > 0 && (
Expand Down
36 changes: 19 additions & 17 deletions frontend/review/src/components/Comments/WriteComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ export const WriteComment = forwardRef(({ course, setUserComment }, ref) => {

const handleSubmit = () => {
console.log("Comment submitted");
// apiPostComment(course, semester, content).then(res => {
// setUserComment(res)
// });
setUserComment({
content: content,
id: 1,
created_at: new Date(),
modified_at: new Date(),
author_name: "Engineering Student",
likes: 0,
course: course,
semester: semester,
professorId: [130],
parent_id: null,
path: "1",
replies: 0
})
apiPostComment(course, semester, content).then(res => {
console.log(res)
res = {...res, created_at: new Date(res.created_at), modified_at: new Date(res.modified_at)}
setUserComment(res)
});
// setUserComment({
// content: content,
// id: 1,
// created_at: new Date(),
// modified_at: new Date(),
// author_name: "Engineering Student",
// likes: 0,
// course: course,
// semester: semester,
// professorId: [130],
// parent_id: null,
// path: "1",
// replies: 0
// })
}

return(
Expand Down
4 changes: 2 additions & 2 deletions frontend/review/src/components/DetailsBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ const CommentsTab = forwardRef(

useEffect(() => {
setIsLoading(true);
apiComments(course, null, null, null)
apiComments(course, "2022A", null, null)
.then(res => {
console.log("fetching comments", res);
setComments(res.comments);
setComments(res.comments.map(c => ({ ...c, created_at: new Date(c.created_at), modified_at: new Date(c.modified_at) })));
setSemesterList(res.semesters);
})
.finally(() => {
Expand Down
Loading

0 comments on commit 8258892

Please sign in to comment.