92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
"""
|
|
Survey forms for CRUD operations
|
|
"""
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from .models import 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
|
|
)
|