From 3228c845551043cff92b8e7f2ff9501628aec27c Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 12 Aug 2024 12:49:09 +0800 Subject: [PATCH 1/4] feat: add see all question --- main/utils.py | 7 ++++++- main/views_tanyateman.py | 14 ++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/main/utils.py b/main/utils.py index 2fb64e9..69e7c7b 100644 --- a/main/utils.py +++ b/main/utils.py @@ -221,4 +221,9 @@ def delete_semester(semester: UserGPA): for course_semester in course_semesters: calculator = course_semester.calculator calculator.delete() - semester.delete() \ No newline at end of file + semester.delete() + +def get_paged_questions(questions, page): + paginator = Paginator(questions, 10) + questions = paginator.get_page(page) + return questions, paginator.num_pages \ No newline at end of file diff --git a/main/views_tanyateman.py b/main/views_tanyateman.py index d264c77..b4f75b3 100644 --- a/main/views_tanyateman.py +++ b/main/views_tanyateman.py @@ -11,7 +11,7 @@ from main.views_calculator import score_component from .serializers import AddQuestionSerializer, CalculatorSerializer, QuestionSerializer, ScoreComponentSerializer, TanyaTemanProfileSerializer, UserCumulativeGPASerializer, UserGPASerializer, CourseForSemesterSerializer, SemesterWithCourseSerializer -from .utils import get_recommended_score, get_score, response, update_course_score, validate_body, check_notexist_and_create_user_cumulative_gpa, validate_body_minimum, add_semester_gpa, delete_semester_gpa, update_semester_gpa, update_cumulative_gpa, get_fasilkom_courses, add_course_to_semester, validate_params, delete_course_to_semester +from .utils import get_recommended_score, get_score, response, response_paged, update_course_score, validate_body, check_notexist_and_create_user_cumulative_gpa, validate_body_minimum, add_semester_gpa, delete_semester_gpa, update_semester_gpa, update_cumulative_gpa, get_fasilkom_courses, add_course_to_semester, validate_params, delete_course_to_semester, get_paged_questions from .models import Calculator, Course, Profile, Question from django.db.models import F import boto3 @@ -65,12 +65,18 @@ def tanya_teman(request): return response(error=serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': - id = request.query_params.get("id") + id = request.query_params.get('id') if id != None: return tanya_teman_with_id(request, id) - questions = Question.objects.all() - return response(data=QuestionSerializer(questions, many=True).data, status=status.HTTP_200_OK) + # Note: Need to change filter to be Question.VerificationStatus.APPROVED + # after implementing the flow to verify the status + questions = Question.objects.filter(verification_status=Question.VerificationStatus.WAITING).order_by('created_at') + page = request.query_params.get('page') + if page is None: + return response(error='page is required', status=status.HTTP_400_BAD_REQUEST) + questions, total_page = get_paged_questions(questions, page) + return response_paged(data={'questions': QuestionSerializer(questions, many=True).data}, total_page=total_page) def tanya_teman_with_id(request, id): user = Profile.objects.get(username=str(request.user)) From fc612aa48b4215fc4528f4cca18696df3a77d402 Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 12 Aug 2024 18:11:54 +0800 Subject: [PATCH 2/4] feat: add filter and history --- main/views_tanyateman.py | 59 ++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/main/views_tanyateman.py b/main/views_tanyateman.py index b4f75b3..8b36e3c 100644 --- a/main/views_tanyateman.py +++ b/main/views_tanyateman.py @@ -9,7 +9,7 @@ from rest_framework import status from main.views_calculator import score_component -from .serializers import AddQuestionSerializer, CalculatorSerializer, QuestionSerializer, ScoreComponentSerializer, TanyaTemanProfileSerializer, UserCumulativeGPASerializer, UserGPASerializer, CourseForSemesterSerializer, SemesterWithCourseSerializer +from .serializers import AddQuestionSerializer, CalculatorSerializer, QuestionSerializer, ScoreComponentSerializer, TanyaTemanProfileSerializer, UserCumulativeGPASerializer, UserGPASerializer, CourseForSemesterSerializer, SemesterWithCourseSerializer, HideVerificationQuestionSerializer from .utils import get_recommended_score, get_score, response, response_paged, update_course_score, validate_body, check_notexist_and_create_user_cumulative_gpa, validate_body_minimum, add_semester_gpa, delete_semester_gpa, update_semester_gpa, update_cumulative_gpa, get_fasilkom_courses, add_course_to_semester, validate_params, delete_course_to_semester, get_paged_questions from .models import Calculator, Course, Profile, Question @@ -68,15 +68,10 @@ def tanya_teman(request): id = request.query_params.get('id') if id != None: return tanya_teman_with_id(request, id) - - # Note: Need to change filter to be Question.VerificationStatus.APPROVED - # after implementing the flow to verify the status - questions = Question.objects.filter(verification_status=Question.VerificationStatus.WAITING).order_by('created_at') - page = request.query_params.get('page') - if page is None: - return response(error='page is required', status=status.HTTP_400_BAD_REQUEST) - questions, total_page = get_paged_questions(questions, page) - return response_paged(data={'questions': QuestionSerializer(questions, many=True).data}, total_page=total_page) + + is_history = request.query_params.get('is_history') != None + questions = filtered_question(request) + return tanya_teman_paged(request, questions, is_history) def tanya_teman_with_id(request, id): user = Profile.objects.get(username=str(request.user)) @@ -88,4 +83,46 @@ def tanya_teman_with_id(request, id): return response(data={ "question": QuestionSerializer(question).data, "current_user": TanyaTemanProfileSerializer(user).data - }, status=status.HTTP_200_OK) \ No newline at end of file + }, status=status.HTTP_200_OK) + + if request.method == "DELETE": + if question.user.pk != user.pk : + return response(error="You are not allowed to delete other person's question", status=status.HTTP_403_FORBIDDEN) + question.delete() + return response(status=status.HTTP_204_NO_CONTENT) + +def tanya_teman_paged(request, questions, is_history): + page = request.query_params.get('page') + if page is None: + return response(error='page is required', status=status.HTTP_400_BAD_REQUEST) + + questions, total_page = get_paged_questions(questions, page) + return response_paged(data={ + 'questions': QuestionSerializer(questions, many=True).data + if is_history else + HideVerificationQuestionSerializer(questions, many=True).data + }, total_page=total_page) + + +def filtered_question(request): + is_paling_banyak_disukai = request.query_params.get('paling_banyak_disukai') != None + is_terverifikasi = request.query_params.get('terverifikasi') != None + is_menunggu_verifikasi = request.query_params.get('menunggu_verifikasi') != None + is_history = request.query_params.get('is_history') != None + user = Profile.objects.get(username=str(request.user)) + + # Note: Need to change filter to be Question.VerificationStatus.APPROVED (line 244) + # after implementing the flow to verify the status + questions = Question.objects.all() + if is_history: + questions = questions.filter(user=user) + else : + questions = questions.filter(verification_status=Question.VerificationStatus.APPROVED) + + if is_paling_banyak_disukai: + return questions.order_by('like_count') + if is_terverifikasi: + return questions.filter(verification_status=Question.VerificationStatus.APPROVED).order_by('created_at') + if is_menunggu_verifikasi: + return questions.filter(verification_status=Question.VerificationStatus.WAITING).order_by('created_at') + return questions.order_by('created_at') \ No newline at end of file From 95130cec66c42003fe750da0f25f7408526a5b85 Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 12 Aug 2024 18:17:56 +0800 Subject: [PATCH 3/4] feat: add serializer to see all question --- main/serializers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/main/serializers.py b/main/serializers.py index 7990a25..f8142ca 100644 --- a/main/serializers.py +++ b/main/serializers.py @@ -376,5 +376,20 @@ class Meta: def get_user(self, obj): return obj.user.username + def get_attachment_url(self, obj): + return obj.get_attachment_presigned_url() + +class HideVerificationQuestionSerializer(serializers.ModelSerializer): + user = TanyaTemanProfileSerializer() + course = CourseForSemesterSerializer() + attachment_url = serializers.SerializerMethodField('get_attachment_url') + + class Meta: + model = Question + fields = ['id', 'user', 'question_text', 'course', 'is_anonym', 'attachment_url', 'like_count', 'created_at', 'updated_at'] + + def get_user(self, obj): + return obj.user.username + def get_attachment_url(self, obj): return obj.get_attachment_presigned_url() \ No newline at end of file From 5045b951ae0c452abca972ee2ceee3df2c4a0d49 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 13 Aug 2024 10:07:15 +0800 Subject: [PATCH 4/4] chore: set status to WAITING temporarily to facilitate frontend testing --- main/views_tanyateman.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/views_tanyateman.py b/main/views_tanyateman.py index 8b36e3c..71828ae 100644 --- a/main/views_tanyateman.py +++ b/main/views_tanyateman.py @@ -111,13 +111,13 @@ def filtered_question(request): is_history = request.query_params.get('is_history') != None user = Profile.objects.get(username=str(request.user)) - # Note: Need to change filter to be Question.VerificationStatus.APPROVED (line 244) + # Note: Need to change filter to be Question.VerificationStatus.WAITING (line 120) # after implementing the flow to verify the status questions = Question.objects.all() if is_history: questions = questions.filter(user=user) else : - questions = questions.filter(verification_status=Question.VerificationStatus.APPROVED) + questions = questions.filter(verification_status=Question.VerificationStatus.WAITING) if is_paling_banyak_disukai: return questions.order_by('like_count')