Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TanyaTeman: See all questions #82

Merged
merged 4 commits into from
Aug 13, 2024
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
15 changes: 15 additions & 0 deletions main/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 6 additions & 1 deletion main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,9 @@ def delete_semester(semester: UserGPA):
for course_semester in course_semesters:
calculator = course_semester.calculator
calculator.delete()
semester.delete()
semester.delete()

def get_paged_questions(questions, page):
paginator = Paginator(questions, 10)
questions = paginator.get_page(page)
return questions, paginator.num_pages
57 changes: 50 additions & 7 deletions main/views_tanyateman.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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, 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
Expand Down Expand Up @@ -65,12 +65,13 @@ 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)

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))
Expand All @@ -82,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)
}, 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.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.WAITING)

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')