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

Feat/adrian/tanyateman: Change Verification Status #86

Merged
merged 4 commits into from
Aug 28, 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
4 changes: 3 additions & 1 deletion main/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import Calculator, Course, ReviewLike, ScoreComponent, Tag, Profile, Review, ReviewTag
from .models import Calculator, Course, ReviewLike, ScoreComponent, Tag, Profile, Review, ReviewTag, Question, Answer, QuestionImageAdmin, AnswerImageAdmin

# Register your models here.
admin.site.register(Course)
Expand All @@ -10,3 +10,5 @@
admin.site.register(ReviewTag)
admin.site.register(Calculator)
admin.site.register(ScoreComponent)
admin.site.register(Question, QuestionImageAdmin)
admin.site.register(Answer, AnswerImageAdmin)
23 changes: 23 additions & 0 deletions main/migrations/0025_auto_20240827_1041.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.2 on 2024-08-27 10:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0024_likepost'),
]

operations = [
migrations.AlterField(
model_name='answer',
name='attachment',
field=models.CharField(blank=True, max_length=120, null=True),
),
migrations.AlterField(
model_name='question',
name='attachment',
field=models.CharField(blank=True, max_length=120, null=True),
),
]
20 changes: 17 additions & 3 deletions main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import environ

from django.utils import timezone
from django.utils.html import format_html
from django.contrib import admin

env = environ.Env()
expires_in = 60*60*7 # 7 Hours
Expand Down Expand Up @@ -196,7 +198,7 @@ class VerificationStatus(models.TextChoices):
question_text = models.TextField()
course = models.ForeignKey(Course, on_delete=CASCADE)
is_anonym = models.IntegerField()
attachment = models.CharField(null=True, max_length=120)
attachment = models.CharField(null=True, max_length=120, blank=True)
like_count = models.IntegerField(default=0)
reply_count = models.IntegerField(default=0)
verification_status = models.TextField(choices=VerificationStatus.choices, default=VerificationStatus.WAITING)
Expand All @@ -216,7 +218,7 @@ class VerificationStatus(models.TextChoices):
question = models.ForeignKey(Question, on_delete=CASCADE)
answer_text = models.TextField()
is_anonym = models.IntegerField()
attachment = models.CharField(null=True, max_length=120)
attachment = models.CharField(null=True, max_length=120, blank=True)
like_count = models.IntegerField(default=0)
verification_status = models.TextField(choices=VerificationStatus.choices, default=VerificationStatus.WAITING)
created_at = models.DateTimeField(default=timezone.now)
Expand Down Expand Up @@ -260,4 +262,16 @@ class Meta:
unique_together = ('user', 'content_type', 'object_id')

def __str__(self):
return f'{self.user.username} liked {self.content_object}: {self.content_type} {self.object_id}'
return f'{self.user.username} liked {self.content_object}: {self.content_type} {self.object_id}'

class QuestionImageAdmin(admin.ModelAdmin):
def image_tag(self, obj):
return format_html('<img src="{}" style="max-width:200px; max-height:200px"/>'.format(get_attachment_presigned_url(obj.attachment)))

list_display = ['question_text','image_tag',]

class AnswerImageAdmin(admin.ModelAdmin):
def image_tag(self, obj):
return format_html('<img src="{}" style="max-width:200px; max-height:200px"/>'.format(get_attachment_presigned_url(obj.attachment)))

list_display = ['answer_text','image_tag',]
16 changes: 11 additions & 5 deletions main/views_tanyateman.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def jawab_teman_paged(request, answers):
return response_paged(data={
'answers': list_answers,
'like_count': like_count,
'reply_count': reply_count
'reply_count': len(answers)
}, total_page=total_page)


Expand All @@ -258,13 +258,11 @@ def filtered_question(request):
keyword = request.query_params.get('keyword')
user = Profile.objects.get(username=str(request.user))

# Note: The filter should be changed to Question.VerificationStatus.APPROVED (previously WAITING, see line 120)
# after implementing the verification flow
questions = Question.objects.all()
if is_history:
questions = questions.filter(user=user)
else :
questions = questions.filter(verification_status=Question.VerificationStatus.WAITING)
questions = questions.filter(verification_status=Question.VerificationStatus.APPROVED)

if course_id != None:
questions = questions.filter(course__pk=course_id)
Expand Down Expand Up @@ -325,5 +323,13 @@ def add_is_liked(user, posts, is_question):
for post in posts:
post_like = LikePost.objects.filter(content_type=content_type, object_id=post['id'], user=user).first()
post['liked_by_user'] = 1 if post_like != None else 0

if is_question:
post['reply_count'] = get_reply_count(user, post['id'])
list_posts.append(post)
return list_posts
return list_posts

def get_reply_count(user, question_id):
return Answer.objects.filter(
Q(question__pk=question_id),
Q(verification_status=Answer.VerificationStatus.APPROVED) | Q(user=user)).count()