Skip to content

Commit

Permalink
Fix Active Questionnare Check
Browse files Browse the repository at this point in the history
  • Loading branch information
gigincg committed Jan 7, 2025
1 parent c470f5e commit 4697dd4
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions care/emr/api/viewsets/questionnaire.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django_filters import rest_framework as filters
from pydantic import UUID4, BaseModel
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.response import Response

from care.emr.api.viewsets.base import EMRModelViewSet
Expand All @@ -13,6 +13,7 @@
Patient,
Questionnaire,
QuestionnaireOrganization,
QuestionnaireResponse,
)
from care.emr.resources.organization.spec import OrganizationReadSpec
from care.emr.resources.questionnaire.spec import (
Expand Down Expand Up @@ -60,19 +61,16 @@ def perform_create(self, instance):
)

def validate_data(self, instance, model_obj=None):
pass
# If we're editing an existing questionnaire (model_obj is not None)
# and there are no responses linked to this questionnaire yet
# if (
# model_obj
# and not QuestionnaireResponse.objects.filter(
# questionnaire=model_obj
# ).exists()
# ):
# Prevent editing if the questionnaire has already been used (has responses)
# This ensures data integrity by not allowing changes to questionnaires
# that are actively being used
# raise ValidationError("Cannot edit an active questionnaire")
if (
model_obj
and QuestionnaireResponse.objects.filter(questionnaire=model_obj).exists()
):
# Prevent editing if the questionnaire has already been used (has responses)
# This ensures data integrity by not allowing changes to questionnaires
# that are actively being used
raise ValidationError("Cannot edit an active questionnaire")

def authorize_create(self, instance):
for org in instance.organizations:
Expand Down

0 comments on commit 4697dd4

Please sign in to comment.