diff --git a/main/admin.py b/main/admin.py index 884574d..a9a84c0 100644 --- a/main/admin.py +++ b/main/admin.py @@ -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) @@ -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) \ No newline at end of file diff --git a/main/migrations/0025_auto_20240827_1041.py b/main/migrations/0025_auto_20240827_1041.py new file mode 100644 index 0000000..ce4c502 --- /dev/null +++ b/main/migrations/0025_auto_20240827_1041.py @@ -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), + ), + ] diff --git a/main/models.py b/main/models.py index ccfab7f..6b60023 100644 --- a/main/models.py +++ b/main/models.py @@ -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 @@ -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) @@ -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) @@ -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}' \ No newline at end of file + 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(''.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(''.format(get_attachment_presigned_url(obj.attachment))) + + list_display = ['answer_text','image_tag',] \ No newline at end of file diff --git a/main/views_tanyateman.py b/main/views_tanyateman.py index 64403c6..8d90fd2 100644 --- a/main/views_tanyateman.py +++ b/main/views_tanyateman.py @@ -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) @@ -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) @@ -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 \ No newline at end of file + 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() \ No newline at end of file