166 lines
5.3 KiB
Python
166 lines
5.3 KiB
Python
"""
|
|
Survey forms for CRUD operations
|
|
"""
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from apps.organizations.models import Patient, Staff, Hospital
|
|
from .models import SurveyInstance, SurveyTemplate, SurveyQuestion
|
|
|
|
|
|
class SurveyTemplateForm(forms.ModelForm):
|
|
"""Form for creating/editing survey templates"""
|
|
|
|
class Meta:
|
|
model = SurveyTemplate
|
|
fields = [
|
|
'name', 'name_ar', 'hospital', 'survey_type',
|
|
'scoring_method', 'negative_threshold', 'is_active'
|
|
]
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'e.g., MD Consultation Feedback'
|
|
}),
|
|
'name_ar': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'الاسم بالعربية'
|
|
}),
|
|
'hospital': forms.Select(attrs={'class': 'form-select'}),
|
|
'survey_type': forms.Select(attrs={'class': 'form-select'}),
|
|
'scoring_method': forms.Select(attrs={'class': 'form-select'}),
|
|
'negative_threshold': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.1',
|
|
'min': '1',
|
|
'max': '5'
|
|
}),
|
|
'is_active': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
}
|
|
|
|
|
|
class SurveyQuestionForm(forms.ModelForm):
|
|
"""Form for creating/editing survey questions"""
|
|
|
|
class Meta:
|
|
model = SurveyQuestion
|
|
fields = [
|
|
'text', 'text_ar', 'question_type', 'order',
|
|
'is_required', 'choices_json'
|
|
]
|
|
widgets = {
|
|
'text': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 2,
|
|
'placeholder': 'Enter question in English'
|
|
}),
|
|
'text_ar': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 2,
|
|
'placeholder': 'أدخل السؤال بالعربية'
|
|
}),
|
|
'question_type': forms.Select(attrs={'class': 'form-select'}),
|
|
'order': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '0'
|
|
}),
|
|
'is_required': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
'choices_json': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 5,
|
|
'placeholder': '[{"value": "1", "label": "Option 1", "label_ar": "خيار 1"}]'
|
|
}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['choices_json'].required = False
|
|
self.fields['choices_json'].help_text = _(
|
|
'JSON array of choices for multiple choice questions. '
|
|
'Format: [{"value": "1", "label": "Option 1", "label_ar": "خيار 1"}]'
|
|
)
|
|
|
|
|
|
SurveyQuestionFormSet = forms.inlineformset_factory(
|
|
SurveyTemplate,
|
|
SurveyQuestion,
|
|
form=SurveyQuestionForm,
|
|
extra=1,
|
|
can_delete=True,
|
|
min_num=1,
|
|
validate_min=True
|
|
)
|
|
|
|
|
|
class ManualSurveySendForm(forms.Form):
|
|
"""Form for manually sending surveys to patients or staff"""
|
|
|
|
RECIPIENT_TYPE_CHOICES = [
|
|
('patient', _('Patient')),
|
|
('staff', _('Staff')),
|
|
]
|
|
|
|
DELIVERY_CHANNEL_CHOICES = [
|
|
('email', _('Email')),
|
|
('sms', _('SMS')),
|
|
]
|
|
|
|
survey_template = forms.ModelChoiceField(
|
|
queryset=SurveyTemplate.objects.filter(is_active=True),
|
|
label=_('Survey Template'),
|
|
widget=forms.Select(attrs={
|
|
'class': 'form-select',
|
|
'data-placeholder': _('Select a survey template')
|
|
})
|
|
)
|
|
|
|
recipient_type = forms.ChoiceField(
|
|
choices=RECIPIENT_TYPE_CHOICES,
|
|
label=_('Recipient Type'),
|
|
widget=forms.RadioSelect(attrs={
|
|
'class': 'form-check-input'
|
|
})
|
|
)
|
|
|
|
recipient = forms.CharField(
|
|
label=_('Recipient'),
|
|
widget=forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': _('Search by name or ID...'),
|
|
'data-search-url': '/api/recipients/search/'
|
|
}),
|
|
help_text=_('Start typing to search for patient or staff')
|
|
)
|
|
|
|
delivery_channel = forms.ChoiceField(
|
|
choices=DELIVERY_CHANNEL_CHOICES,
|
|
label=_('Delivery Channel'),
|
|
widget=forms.Select(attrs={
|
|
'class': 'form-select'
|
|
})
|
|
)
|
|
|
|
custom_message = forms.CharField(
|
|
label=_('Custom Message (Optional)'),
|
|
required=False,
|
|
widget=forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': _('Add a custom message to the survey invitation...')
|
|
})
|
|
)
|
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.user = user
|
|
|
|
# Filter templates based on user's hospital
|
|
queryset = SurveyTemplate.objects.filter(is_active=True)
|
|
if user.hospital:
|
|
queryset = queryset.filter(hospital=user.hospital)
|
|
self.fields['survey_template'].queryset = queryset
|
|
|
|
# Set default recipient type
|
|
self.fields['recipient_type'].initial = 'patient'
|
|
self.fields['delivery_channel'].initial = 'email'
|