Skip to content

Commit

Permalink
Add additional filters and field in Question
Browse files Browse the repository at this point in the history
TODO: Complete group include childs filter
  • Loading branch information
thenav56 committed Aug 17, 2023
1 parent 87387a6 commit b74c7f5
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 19 deletions.
22 changes: 22 additions & 0 deletions apps/questionnaire/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import strawberry
import strawberry_django

from .enums import QuestionTypeEnum
from .models import (
Questionnaire,
Question,
Expand All @@ -20,6 +21,7 @@ class QuestionnaireFilter:
class QuestionGroupFilter:
id: strawberry.auto
questionnaire: strawberry.auto
parent: strawberry.auto
name: strawberry.auto
label: strawberry.auto

Expand All @@ -36,5 +38,25 @@ class QuestionChoiceCollectionFilter:
class QuestionFilter:
id: strawberry.auto
questionnaire: strawberry.auto
choice_collection: strawberry.auto
type: QuestionTypeEnum
name: strawberry.auto
label: strawberry.auto
group: strawberry.auto
include_child_group: bool | None = False

def filter_group(self, queryset):
# NOTE: logic is in filter_include_child_group
return queryset

def filter_include_child_group(self, queryset):
if self.group is strawberry.UNSET:
# Nothing to do here
return queryset
if not self.include_child_group:
return queryset.filter(group=self.group.pk)
all_groups = [
self.group.pk,
# TODO: *get_child_groups_id(self.group.pk),
]
return queryset.filter(group__in=all_groups)
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Generated by Django 4.2.1 on 2023-08-17 05:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('questionnaire', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='question',
name='appearance',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='calculation',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='choice_filter',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='constraint',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='default',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='question',
name='guidance_hint',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='question',
name='image',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='is_or_other',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='question',
name='or_other_label',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='question',
name='parameters',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='readonly',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='relevant',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='required',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='question',
name='required_message',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='trigger',
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name='question',
name='video',
field=models.CharField(blank=True, max_length=255),
),
migrations.AlterField(
model_name='questiongroup',
name='relevant',
field=models.CharField(blank=True, max_length=255),
),
]
36 changes: 18 additions & 18 deletions apps/questionnaire/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class QuestionGroup(UserResource):
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=255)
label = models.CharField(max_length=255)
relevant = models.CharField(max_length=255) # ${has_child} = 'yes'
relevant = models.CharField(max_length=255, blank=True) # ${has_child} = 'yes'
# # Repeat attributes
# is_repeat = models.BooleanField(default=False)
# repeat_count = models.CharField(max_length=255) # Eg: static: 3, formula: ${num_hh_members}
Expand Down Expand Up @@ -151,23 +151,23 @@ class Type(models.IntegerChoices):
hint = models.TextField(blank=True)

# NOTE: Need to check if TextField or CharField should be used
# default = models.TextField(blank=True)
# guidance_hint = models.TextField(blank=True)
# trigger = models.CharField(max_length=255, blank=True)
# readonly = models.CharField(max_length=255, blank=True)
# required = models.BooleanField(default=False)
# required_message = models.CharField(max_length=255, blank=True)
# relevant = models.CharField(max_length=255, blank=True)
# constraint = models.CharField(max_length=255, blank=True)
# appearance = models.CharField(max_length=255, blank=True)
# calculation = models.CharField(max_length=255, blank=True)
# parameters = models.CharField(max_length=255, blank=True)
# choice_filter = models.CharField(max_length=255, blank=True)
# image = models.CharField(max_length=255, blank=True)
# video = models.CharField(max_length=255, blank=True)
# -- Or Other
# is_or_other = models.BooleanField(default=False)
# or_other_label = models.TextField(blank=True)
default = models.TextField(blank=True)
guidance_hint = models.TextField(blank=True)
trigger = models.CharField(max_length=255, blank=True)
readonly = models.CharField(max_length=255, blank=True)
required = models.BooleanField(default=False)
required_message = models.CharField(max_length=255, blank=True)
relevant = models.CharField(max_length=255, blank=True)
constraint = models.CharField(max_length=255, blank=True)
appearance = models.CharField(max_length=255, blank=True)
calculation = models.CharField(max_length=255, blank=True)
parameters = models.CharField(max_length=255, blank=True)
choice_filter = models.CharField(max_length=255, blank=True)
image = models.CharField(max_length=255, blank=True)
video = models.CharField(max_length=255, blank=True)
# -- Or Other: https://xlsform.org/en/#specify-other
is_or_other = models.BooleanField(default=False)
or_other_label = models.TextField(blank=True)

class Meta:
unique_together = ('questionnaire', 'name')
17 changes: 17 additions & 0 deletions apps/questionnaire/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ class Meta:
'label',
'hint',
'choice_collection',
# XLSForm fields
'default',
'guidance_hint',
'trigger',
'readonly',
'required',
'required_message',
'relevant',
'constraint',
'appearance',
'calculation',
'parameters',
'choice_filter',
'image',
'video',
'is_or_other',
'or_other_label',
)

instance: Question
Expand Down
22 changes: 22 additions & 0 deletions apps/questionnaire/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ class QuestionType(UserResourceTypeMixin):
label: strawberry.auto
hint: strawberry.auto

default: strawberry.auto
guidance_hint: strawberry.auto
trigger: strawberry.auto
readonly: strawberry.auto
required: strawberry.auto
required_message: strawberry.auto
relevant: strawberry.auto
constraint: strawberry.auto
appearance: strawberry.auto
calculation: strawberry.auto
parameters: strawberry.auto
choice_filter: strawberry.auto
image: strawberry.auto
video: strawberry.auto
is_or_other: strawberry.auto
or_other_label: strawberry.auto

type: QuestionTypeEnum

@staticmethod
Expand All @@ -129,6 +146,11 @@ def get_queryset(_, queryset: models.QuerySet | None, info: Info):
def questionnaire_id(self) -> strawberry.ID:
return strawberry.ID(str(self.questionnaire_id))

@strawberry.field
def group_id(self) -> typing.Optional[strawberry.ID]:
if self.group_id:
return strawberry.ID(str(self.group_id))

@strawberry.field
@sync_to_async
def choice_collection(self) -> typing.Optional[QuestionChoiceCollectionType]:
Expand Down
56 changes: 55 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -333,26 +333,47 @@ input QuestionCreateInput {
group: ID
hint: String
choiceCollection: ID
default: String
guidanceHint: String
trigger: String
readonly: String
required: Boolean
requiredMessage: String
relevant: String
constraint: String
appearance: String
calculation: String
parameters: String
choiceFilter: String
image: String
video: String
isOrOther: Boolean
orOtherLabel: String
}

input QuestionFilter {
id: IDFilterLookup
questionnaire: DjangoModelFilterInput
choiceCollection: DjangoModelFilterInput
type: QuestionTypeEnum
name: StrFilterLookup
label: StrFilterLookup
group: DjangoModelFilterInput
includeChildGroup: Boolean = false
}

input QuestionGroupCreateInput {
questionnaire: ID!
name: String!
label: String!
relevant: String!
parent: ID
relevant: String
}

input QuestionGroupFilter {
id: IDFilterLookup
questionnaire: DjangoModelFilterInput
parent: DjangoModelFilterInput
name: StrFilterLookup
label: StrFilterLookup
}
Expand Down Expand Up @@ -408,9 +429,26 @@ type QuestionType {
name: String!
label: String!
hint: String!
default: String!
guidanceHint: String!
trigger: String!
readonly: String!
required: Boolean!
requiredMessage: String!
relevant: String!
constraint: String!
appearance: String!
calculation: String!
parameters: String!
choiceFilter: String!
image: String!
video: String!
isOrOther: Boolean!
orOtherLabel: String!
type: QuestionTypeEnum!
choiceCollection: QuestionChoiceCollectionType
createdBy: UserType!
groupId: ID
modifiedBy: UserType!
questionnaireId: ID!
}
Expand Down Expand Up @@ -456,6 +494,22 @@ input QuestionUpdateInput {
label: String
hint: String
choiceCollection: ID
default: String
guidanceHint: String
trigger: String
readonly: String
required: Boolean
requiredMessage: String
relevant: String
constraint: String
appearance: String
calculation: String
parameters: String
choiceFilter: String
image: String
video: String
isOrOther: Boolean
orOtherLabel: String
}

input QuestionnaireCreateInput {
Expand Down

0 comments on commit b74c7f5

Please sign in to comment.