Skip to content

Commit

Permalink
Add is_hidden in Question
Browse files Browse the repository at this point in the history
- Update tests
- Remove buggy filter
- Add questionnaire arg in bulk mutations
  • Loading branch information
thenav56 committed Sep 6, 2023
1 parent fb96451 commit d1f5aae
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 89 deletions.
2 changes: 1 addition & 1 deletion apps/questionnaire/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@


@strawberry.enum
class QuestionLeafGroupVisibilityActionEnum(Enum):
class VisibilityActionEnum(Enum):
SHOW = auto(), 'Show'
HIDE = auto(), 'Hide'
9 changes: 9 additions & 0 deletions apps/questionnaire/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ class QuestionFactory(DjangoModelFactory):

class Meta:
model = Question

@classmethod
def _create(cls, model_class, *args, **kwargs):
obj = model_class(*args, **kwargs)
obj.questionnaire = obj.leaf_group.questionnaire
if getattr(obj, 'choice_collection', None):
assert obj.choice_collection.questionnaire == obj.questionnaire
obj.save()
return obj
14 changes: 1 addition & 13 deletions apps/questionnaire/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,8 @@ class QuestionFilter:
name: strawberry.auto
label: strawberry.auto
leaf_group: strawberry.auto
include_child_group: bool | None = False
is_hidden: strawberry.auto

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

def filter_include_child_group(self, queryset):
if self.leaf_group is strawberry.UNSET:
# Nothing to do here
return queryset
if not self.include_child_group:
return queryset.filter(group=self.leaf_group.pk)
all_groups = [
self.leaf_group.pk,
# TODO: *get_child_groups_id(self.leaf_group.pk),
]
return queryset.filter(group__in=all_groups)
18 changes: 18 additions & 0 deletions apps/questionnaire/migrations/0006_question_is_hidden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.1 on 2023-09-06 09:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('questionnaire', '0005_alter_questionleafgroup_unique_together'),
]

operations = [
migrations.AddField(
model_name='question',
name='is_hidden',
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions apps/questionnaire/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ class Type(models.IntegerChoices):
leaf_group = models.ForeignKey(QuestionLeafGroup, on_delete=models.CASCADE)
type = models.PositiveSmallIntegerField(choices=Type.choices)
order = models.PositiveSmallIntegerField(default=0)
is_hidden = models.BooleanField(default=False)

# XXX: This needs to be also unique within Questionnaire & Question Bank
# TODO: Make sure this is also unique within questions and groups
Expand Down
31 changes: 27 additions & 4 deletions apps/questionnaire/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
QuestionLeafGroupType,
QuestionChoiceCollectionType,
)
from .enums import QuestionLeafGroupVisibilityActionEnum
from .enums import VisibilityActionEnum

QuestionnaireMutation = ModelMutation('Questionnaire', QuestionnaireSerializer)
QuestionMutation = ModelMutation('Question', QuestionSerializer)
Expand Down Expand Up @@ -147,14 +147,37 @@ def bulk_update_questionnair_question_groups_leaf_order(
@sync_to_async
def update_question_groups_leaf_visibility(
self,
questionnaire_id: strawberry.ID,
ids: list[strawberry.ID],
visibility: QuestionLeafGroupVisibilityActionEnum,
visibility: VisibilityActionEnum,
info: Info,
) -> BulkBasicMutationResponseType[QuestionLeafGroupType]:
if errors := ModelMutation.check_permissions(info, Project.Permission.UPDATE_QUESTION_GROUP):
return BulkBasicMutationResponseType(errors=[errors])
queryset = QuestionLeafGroupType.get_queryset(None, None, info).filter(id__in=ids)
is_hidden = visibility == QuestionLeafGroupVisibilityActionEnum.HIDE
queryset = QuestionLeafGroupType.get_queryset(None, None, info).filter(
questionnaire=questionnaire_id,
id__in=ids,
)
is_hidden = visibility == VisibilityActionEnum.HIDE
queryset.update(is_hidden=is_hidden)
return BulkBasicMutationResponseType(results=[i for i in queryset])

@strawberry.mutation
@sync_to_async
def update_questions_visibility(
self,
questionnaire_id: strawberry.ID,
ids: list[strawberry.ID],
visibility: VisibilityActionEnum,
info: Info,
) -> BulkBasicMutationResponseType[QuestionType]:
if errors := ModelMutation.check_permissions(info, Project.Permission.UPDATE_QUESTION):
return BulkBasicMutationResponseType(errors=[errors])
queryset = QuestionType.get_queryset(None, None, info).filter(
questionnaire=questionnaire_id,
id__in=ids,
)
is_hidden = visibility == VisibilityActionEnum.HIDE
queryset.update(is_hidden=is_hidden)
return BulkBasicMutationResponseType(results=[i for i in queryset])

Expand Down
Loading

0 comments on commit d1f5aae

Please sign in to comment.