Skip to content

Commit

Permalink
Merge pull request #83 from mnqrt/feat/adrian/tanyateman
Browse files Browse the repository at this point in the history
Feat/adrian/tanyateman
  • Loading branch information
mnqrt authored Aug 20, 2024
2 parents 966fd68 + cb74b94 commit b61faae
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 79 deletions.
23 changes: 23 additions & 0 deletions main/migrations/0021_auto_20240815_0451.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.2 on 2024-08-15 04:51

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('main', '0020_auto_20240809_0024'),
]

operations = [
migrations.RemoveField(
model_name='question',
name='updated_at',
),
migrations.AlterField(
model_name='question',
name='created_at',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
18 changes: 18 additions & 0 deletions main/migrations/0022_auto_20240817_0400.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.2 on 2024-08-17 04:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0021_auto_20240815_0451'),
]

operations = [
migrations.AlterField(
model_name='question',
name='attachment',
field=models.CharField(max_length=120, null=True),
),
]
34 changes: 34 additions & 0 deletions main/migrations/0023_auto_20240818_1418.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.1.2 on 2024-08-18 14:18

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('main', '0022_auto_20240817_0400'),
]

operations = [
migrations.AddField(
model_name='question',
name='reply_count',
field=models.IntegerField(default=0),
),
migrations.CreateModel(
name='Answer',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('answer_text', models.TextField()),
('is_anonym', models.IntegerField()),
('attachment', models.CharField(max_length=120, null=True)),
('like_count', models.IntegerField(default=0)),
('verification_status', models.TextField(choices=[('Menunggu Verifikasi', 'Waiting'), ('Terverifikasi', 'Approved')], default='Menunggu Verifikasi')),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.question')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.profile')),
],
),
]
75 changes: 47 additions & 28 deletions main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,34 +194,53 @@ class VerificationStatus(models.TextChoices):
question_text = models.TextField()
course = models.ForeignKey(Course, on_delete=CASCADE)
is_anonym = models.IntegerField()
attachment = models.CharField(max_length=120)
attachment = models.CharField(null=True, max_length=120)
like_count = models.IntegerField(default=0)
reply_count = models.IntegerField(default=0)
verification_status = models.TextField(choices=VerificationStatus.choices, default=VerificationStatus.WAITING)
created_at = models.DateTimeField(default=timezone.now())
updated_at = models.DateTimeField(default=timezone.now())
created_at = models.DateTimeField(default=timezone.now)

class Answer(models.Model):
"""
This class represent the Answer/Reply for the Question
is_anonym = 1 if the user wants to be anonym in the question, otherwise is_anonym = 0
"""

def save(self, *args, **kwargs):
self.updated_at = timezone.now()
return super(Question, self).save(*args, **kwargs)

def get_attachment_presigned_url(self, expires_in=expires_in):
s3 = boto3.client(
's3',
aws_access_key_id=env("ACCESS_KEY_ID"),
aws_secret_access_key=env("ACCESS_KEY_SECRET"),
region_name=env("AWS_REGION")
)

attachment_type = self.attachment.split('.')[-1]

url = s3.generate_presigned_url(
'get_object',
Params={
'Bucket': env("BUCKET_NAME"),
'Key': self.attachment,
'ResponseContentDisposition': 'inline',
'ResponseContentType': attachment_type
},
ExpiresIn=expires_in
)
return url
class VerificationStatus(models.TextChoices):
WAITING = "Menunggu Verifikasi"
APPROVED = "Terverifikasi"

user = models.ForeignKey(Profile, on_delete=CASCADE)
question = models.ForeignKey(Question, on_delete=CASCADE)
answer_text = models.TextField()
is_anonym = models.IntegerField()
attachment = models.CharField(null=True, max_length=120)
like_count = models.IntegerField(default=0)
verification_status = models.TextField(choices=VerificationStatus.choices, default=VerificationStatus.WAITING)
created_at = models.DateTimeField(default=timezone.now)


def get_attachment_presigned_url(attachment, expires_in=expires_in):
if attachment is None:
return attachment

s3 = boto3.client(
's3',
aws_access_key_id=env("ACCESS_KEY_ID"),
aws_secret_access_key=env("ACCESS_KEY_SECRET"),
region_name=env("AWS_REGION")
)

attachment_type = attachment.split('.')[-1]

url = s3.generate_presigned_url(
'get_object',
Params={
'Bucket': env("BUCKET_NAME"),
'Key': attachment,
'ResponseContentDisposition': 'inline',
'ResponseContentType': attachment_type
},
ExpiresIn=expires_in
)
return url
33 changes: 27 additions & 6 deletions main/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework import serializers
from django.db.models import Avg

from .models import Calculator, Course, Profile, Question, Review, ScoreComponent, Tag, Bookmark, UserCumulativeGPA, UserGPA, CourseSemester, ScoreSubcomponent
from .models import Answer, Calculator, Course, Profile, Question, Review, ScoreComponent, Tag, Bookmark, UserCumulativeGPA, UserGPA, CourseSemester, ScoreSubcomponent, get_attachment_presigned_url

# class CurriculumSerializer(serializers.ModelSerializer):
# class Meta:
Expand Down Expand Up @@ -338,11 +338,17 @@ def get_semester(self, obj):
return UserGPASerializer(obj).data

class AddQuestionSerializer(serializers.Serializer):
attachment_file = serializers.FileField()
attachment_file = serializers.FileField(required=False, allow_null=True)
course_id = serializers.IntegerField()
question_text = serializers.CharField()
is_anonym = serializers.IntegerField()

class AnswerQuestionSerializer(serializers.Serializer):
attachment_file = serializers.FileField(required=False, allow_null=True)
question_id = serializers.IntegerField()
answer_text = serializers.CharField()
is_anonym = serializers.IntegerField()

class TanyaTemanProfileSerializer(serializers.ModelSerializer):
program = serializers.SerializerMethodField('get_cleaned_program')
generation = serializers.SerializerMethodField('get_generation')
Expand Down Expand Up @@ -371,13 +377,13 @@ class QuestionSerializer(serializers.ModelSerializer):

class Meta:
model = Question
fields = ['id', 'user', 'question_text', 'course', 'is_anonym', 'attachment_url', 'like_count', 'verification_status', 'created_at', 'updated_at']
fields = ['id', 'user', 'question_text', 'course', 'is_anonym', 'attachment_url', 'like_count', 'reply_count', 'verification_status', 'created_at']

def get_user(self, obj):
return obj.user.username

def get_attachment_url(self, obj):
return obj.get_attachment_presigned_url()
return get_attachment_presigned_url(attachment=obj.attachment)

class HideVerificationQuestionSerializer(serializers.ModelSerializer):
user = TanyaTemanProfileSerializer()
Expand All @@ -386,10 +392,25 @@ class HideVerificationQuestionSerializer(serializers.ModelSerializer):

class Meta:
model = Question
fields = ['id', 'user', 'question_text', 'course', 'is_anonym', 'attachment_url', 'like_count', 'created_at', 'updated_at']
fields = ['id', 'user', 'question_text', 'course', 'is_anonym', 'attachment_url', 'like_count', 'reply_count', 'created_at']

def get_user(self, obj):
return obj.user.username

def get_attachment_url(self, obj):
return obj.get_attachment_presigned_url()
return get_attachment_presigned_url(attachment=obj.attachment)

class AnswerSerializer(serializers.ModelSerializer):
user = TanyaTemanProfileSerializer()
question_id = serializers.SerializerMethodField('get_question_id')
attachment_url = serializers.SerializerMethodField('get_attachment_url')

class Meta:
model = Answer
fields = ['id', 'user', 'answer_text', 'question_id', 'is_anonym', 'attachment_url', 'like_count', 'created_at']

def get_question_id(self, obj):
return obj.question.pk

def get_attachment_url(self, obj):
return get_attachment_presigned_url(attachment=obj.attachment)
5 changes: 3 additions & 2 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import routers
from django.urls import path, include

from main.views_tanyateman import tanya_teman
from main.views_tanyateman import jawab_teman, tanya_teman
from .views_gpa_calculator import course_semester, gpa_calculator, gpa_calculator_with_semester, course_semester_with_course_id, course_component, course_subcomponent
from .views_calculator import calculator, score_component
from .views import like, tag, bookmark, account, leaderboard
Expand All @@ -29,6 +29,7 @@
path('course-semester/<str:course_id>', course_semester_with_course_id, name="course-with-id"),
path('course-component', course_component, name="course-component"),
path('course-subcomponent', course_subcomponent, name="course-subcomponent"),
path('tanya-teman', tanya_teman, name='tanya-teman')
path('tanya-teman', tanya_teman, name='tanya-teman'),
path('jawab-teman', jawab_teman, name='jawab-teman')
] + router.urls

Loading

0 comments on commit b61faae

Please sign in to comment.