From c64f7fbebac944922c07c390d593f5767716d9e2 Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:04:05 +0530 Subject: [PATCH] Send random preset questions to users (#299) * Send random preset questions to users * fix issue with less number of questions * migrations --- .../0042_alter_project_preset_questions.py | 20 +++++++++++ ayushma/models/project.py | 2 +- ayushma/serializers/project.py | 35 ++++++++++++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 ayushma/migrations/0042_alter_project_preset_questions.py diff --git a/ayushma/migrations/0042_alter_project_preset_questions.py b/ayushma/migrations/0042_alter_project_preset_questions.py new file mode 100644 index 00000000..eebe7f35 --- /dev/null +++ b/ayushma/migrations/0042_alter_project_preset_questions.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.1 on 2023-09-04 13:02 + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("ayushma", "0041_merge_20230904_0908"), + ] + + operations = [ + migrations.AlterField( + model_name="project", + name="preset_questions", + field=django.contrib.postgres.fields.ArrayField( + base_field=models.TextField(), blank=True, null=True, size=None + ), + ), + ] diff --git a/ayushma/models/project.py b/ayushma/models/project.py index 83e0947b..b97c29c2 100644 --- a/ayushma/models/project.py +++ b/ayushma/models/project.py @@ -16,7 +16,7 @@ class Project(BaseModel): choices=STTEngine.choices, default=STTEngine.WHISPER ) model = models.IntegerField(choices=ModelType.choices, default=ModelType.GPT_3_5) - preset_questions = ArrayField(models.TextField(), null=True, blank=True, size=100) + preset_questions = ArrayField(models.TextField(), null=True, blank=True) is_default = models.BooleanField(default=False) archived = models.BooleanField(default=False) diff --git a/ayushma/serializers/project.py b/ayushma/serializers/project.py index f6a32523..12e3d185 100644 --- a/ayushma/serializers/project.py +++ b/ayushma/serializers/project.py @@ -1,9 +1,19 @@ +from random import sample + from rest_framework import serializers from ayushma.models import Project class ProjectSerializer(serializers.ModelSerializer): + display_preset_questions = serializers.SerializerMethodField() + + def get_display_preset_questions(self, project_object): + if project_object.preset_questions and len(project_object.preset_questions) > 4: + return sample(project_object.preset_questions, 4) + else: + return project_object.preset_questions + class Meta: model = Project fields = ( @@ -15,22 +25,39 @@ class Meta: "stt_engine", "model", "is_default", + "display_preset_questions", "archived", - "preset_questions", ) - read_only_fields = ("external_id", "created_at", "modified_at") + read_only_fields = ( + "external_id", + "created_at", + "modified_at", + "display_preset_questions", + ) class ProjectUpdateSerializer(serializers.ModelSerializer): + display_preset_questions = serializers.SerializerMethodField() key_set = serializers.SerializerMethodField() + def get_display_preset_questions(self, project_object): + if project_object.preset_questions and len(project_object.preset_questions) > 4: + return sample(project_object.preset_questions, 4) + else: + return project_object.preset_questions + class Meta: model = Project - fields = ProjectSerializer.Meta.fields + ("prompt", "open_ai_key", "key_set") + fields = ProjectSerializer.Meta.fields + ( + "prompt", + "open_ai_key", + "key_set", + "preset_questions", + ) extra_kwargs = { "open_ai_key": {"write_only": True}, } - read_only_fields = ("key_set",) + read_only_fields = ("key_set", "display_preset_questions") def update(self, instance, validated_data): if validated_data.get("is_default", True):