Fix syntax errors in forms.py files

This commit is contained in:
ismail 2026-03-11 01:26:19 +03:00
parent da0ca4ee19
commit 3b9468748c
2 changed files with 499 additions and 652 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,7 @@
"""
RCA (Root Cause Analysis) forms
"""
RCA (Root Cause Analysis) forms
"""
from django import forms
from django.utils import timezone
from apps.core.models import PriorityChoices
@ -26,59 +25,46 @@ class RootCauseAnalysisForm(HospitalFieldMixin, forms.ModelForm):
class Meta:
model = RootCauseAnalysis
fields = [
'title',
'description',
'background',
'hospital',
'department',
'status',
'severity',
'priority',
'assigned_to',
'target_completion_date',
'root_cause_summary',
"title",
"description",
"background",
"hospital",
"department",
"status",
"severity",
"priority",
"assigned_to",
"target_completion_date",
"root_cause_summary",
]
widgets = {
'title': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter RCA title'
}),
'description': forms.Textarea(attrs={
'class': 'form-control',
'rows': 4,
'placeholder': 'Describe the incident or issue'
}),
'background': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Provide background information and context'
}),
'hospital': forms.Select(attrs={'class': 'form-select'}),
'department': forms.Select(attrs={'class': 'form-select'}),
'status': forms.Select(attrs={'class': 'form-select'}),
'severity': forms.Select(attrs={'class': 'form-select'}),
'priority': forms.Select(attrs={'class': 'form-select'}),
'assigned_to': forms.Select(attrs={'class': 'form-select'}),
'target_completion_date': forms.DateInput(attrs={
'class': 'form-control',
'type': 'date'
}),
'root_cause_summary': forms.Textarea(attrs={
'class': 'form-control',
'rows': 4,
'placeholder': 'Summary of root cause analysis findings'
}),
"title": forms.TextInput(attrs={"class": "form-control", "placeholder": "Enter RCA title"}),
"description": forms.Textarea(
attrs={"class": "form-control", "rows": 4, "placeholder": "Describe the incident or issue"}
),
"background": forms.Textarea(
attrs={"class": "form-control", "rows": 3, "placeholder": "Provide background information and context"}
),
"hospital": forms.Select(attrs={"class": "form-select"}),
"department": forms.Select(attrs={"class": "form-select"}),
"status": forms.Select(attrs={"class": "form-select"}),
"severity": forms.Select(attrs={"class": "form-select"}),
"priority": forms.Select(attrs={"class": "form-select"}),
"assigned_to": forms.Select(attrs={"class": "form-select"}),
"target_completion_date": forms.DateInput(attrs={"class": "form-control", "type": "date"}),
"root_cause_summary": forms.Textarea(
attrs={"class": "form-control", "rows": 4, "placeholder": "Summary of root cause analysis findings"}
),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Filter assigned_to to show only active users
if 'assigned_to' in self.fields:
if "assigned_to" in self.fields:
from apps.accounts.models import User
self.fields['assigned_to'].queryset = User.objects.filter(
is_active=True
).order_by('email')
self.fields["assigned_to"].queryset = User.objects.filter(is_active=True).order_by("email")
class RCARootCauseForm(forms.ModelForm):
@ -87,54 +73,38 @@ class RCARootCauseForm(forms.ModelForm):
class Meta:
model = RCARootCause
fields = [
'description',
'category',
'contributing_factors',
'likelihood',
'impact',
'evidence',
"description",
"category",
"contributing_factors",
"likelihood",
"impact",
"evidence",
]
widgets = {
'description': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Describe the root cause'
}),
'category': forms.Select(attrs={'class': 'form-select'}),
'contributing_factors': forms.Textarea(attrs={
'class': 'form-control',
'rows': 2,
'placeholder': 'Factors that contributed to this root cause'
}),
'likelihood': forms.NumberInput(attrs={
'class': 'form-control',
'min': 1,
'max': 5,
'placeholder': '1-5'
}),
'impact': forms.NumberInput(attrs={
'class': 'form-control',
'min': 1,
'max': 5,
'placeholder': '1-5'
}),
'evidence': forms.Textarea(attrs={
'class': 'form-control',
'rows': 2,
'placeholder': 'Evidence supporting this root cause'
}),
"description": forms.Textarea(
attrs={"class": "form-control", "rows": 3, "placeholder": "Describe the root cause"}
),
"category": forms.Select(attrs={"class": "form-select"}),
"contributing_factors": forms.Textarea(
attrs={"class": "form-control", "rows": 2, "placeholder": "Factors that contributed to this root cause"}
),
"likelihood": forms.NumberInput(attrs={"class": "form-control", "min": 1, "max": 5, "placeholder": "1-5"}),
"impact": forms.NumberInput(attrs={"class": "form-control", "min": 1, "max": 5, "placeholder": "1-5"}),
"evidence": forms.Textarea(
attrs={"class": "form-control", "rows": 2, "placeholder": "Evidence supporting this root cause"}
),
}
def clean_likelihood(self):
likelihood = self.cleaned_data.get('likelihood')
likelihood = self.cleaned_data.get("likelihood")
if likelihood and (likelihood < 1 or likelihood > 5):
raise forms.ValidationError('Likelihood must be between 1 and 5')
raise forms.ValidationError("Likelihood must be between 1 and 5")
return likelihood
def clean_impact(self):
impact = self.cleaned_data.get('impact')
impact = self.cleaned_data.get("impact")
if impact and (impact < 1 or impact > 5):
raise forms.ValidationError('Impact must be between 1 and 5')
raise forms.ValidationError("Impact must be between 1 and 5")
return impact
@ -144,209 +114,159 @@ class RCACorrectiveActionForm(forms.ModelForm):
class Meta:
model = RCACorrectiveAction
fields = [
'description',
'action_type',
'root_cause',
'responsible_person',
'target_date',
'completion_date',
'status',
'effectiveness_measure',
'effectiveness_assessment',
'effectiveness_score',
'obstacles',
"description",
"action_type",
"root_cause",
"responsible_person",
"target_date",
"completion_date",
"status",
"effectiveness_measure",
"effectiveness_assessment",
"effectiveness_score",
"obstacles",
]
widgets = {
'description': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Describe the corrective action'
}),
'action_type': forms.Select(attrs={'class': 'form-select'}),
'root_cause': forms.Select(attrs={'class': 'form-select'}),
'responsible_person': forms.Select(attrs={'class': 'form-select'}),
'target_date': forms.DateInput(attrs={
'class': 'form-control',
'type': 'date'
}),
'completion_date': forms.DateInput(attrs={
'class': 'form-control',
'type': 'date'
}),
'status': forms.Select(attrs={'class': 'form-select'}),
'effectiveness_measure': forms.Textarea(attrs={
'class': 'form-control',
'rows': 2,
'placeholder': 'How will effectiveness be measured?'
}),
'effectiveness_assessment': forms.Textarea(attrs={
'class': 'form-control',
'rows': 2,
'placeholder': 'Assessment of action effectiveness'
}),
'effectiveness_score': forms.NumberInput(attrs={
'class': 'form-control',
'min': 1,
'max': 5,
'placeholder': '1-5'
}),
'obstacles': forms.Textarea(attrs={
'class': 'form-control',
'rows': 2,
'placeholder': 'Obstacles encountered during implementation'
}),
"description": forms.Textarea(
attrs={"class": "form-control", "rows": 3, "placeholder": "Describe the corrective action"}
),
"action_type": forms.Select(attrs={"class": "form-select"}),
"root_cause": forms.Select(attrs={"class": "form-select"}),
"responsible_person": forms.Select(attrs={"class": "form-select"}),
"target_date": forms.DateInput(attrs={"class": "form-control", "type": "date"}),
"completion_date": forms.DateInput(attrs={"class": "form-control", "type": "date"}),
"status": forms.Select(attrs={"class": "form-select"}),
"effectiveness_measure": forms.Textarea(
attrs={"class": "form-control", "rows": 2, "placeholder": "How will effectiveness be measured?"}
),
"effectiveness_assessment": forms.Textarea(
attrs={"class": "form-control", "rows": 2, "placeholder": "Assessment of action effectiveness"}
),
"effectiveness_score": forms.NumberInput(
attrs={"class": "form-control", "min": 1, "max": 5, "placeholder": "1-5"}
),
"obstacles": forms.Textarea(
attrs={"class": "form-control", "rows": 2, "placeholder": "Obstacles encountered during implementation"}
),
}
def __init__(self, *args, **kwargs):
rca = kwargs.pop('rca', None)
rca = kwargs.pop("rca", None)
super().__init__(*args, **kwargs)
# Filter root_cause to show only for this RCA
if 'root_cause' in self.fields and rca:
self.fields['root_cause'].queryset = rca.root_causes.all()
if "root_cause" in self.fields and rca:
self.fields["root_cause"].queryset = rca.root_causes.all()
def clean_effectiveness_score(self):
score = self.cleaned_data.get('effectiveness_score')
score = self.cleaned_data.get("effectiveness_score")
if score and (score < 1 or score > 5):
raise forms.ValidationError('Effectiveness score must be between 1 and 5')
raise forms.ValidationError("Effectiveness score must be between 1 and 5")
return score
class RCAFilterForm(forms.Form):
"""Form for filtering RCA list"""
status = forms.ChoiceField(
required=False,
choices=[('', 'All Statuses')] + list(RCAStatus.choices),
widget=forms.Select(attrs={'class': 'form-select'})
choices=[("", "All Statuses")] + list(RCAStatus.choices),
widget=forms.Select(attrs={"class": "form-select"}),
)
severity = forms.ChoiceField(
required=False,
choices=[('', 'All Severities')] + list(RCASeverity.choices),
widget=forms.Select(attrs={'class': 'form-select'})
choices=[("", "All Severities")] + list(RCASeverity.choices),
widget=forms.Select(attrs={"class": "form-select"}),
)
priority = forms.ChoiceField(
required=False,
choices=[('', 'All Priorities')] + list(PriorityChoices.choices),
widget=forms.Select(attrs={'class': 'form-select'})
choices=[("", "All Priorities")] + list(PriorityChoices.choices),
widget=forms.Select(attrs={"class": "form-select"}),
)
hospital = forms.ChoiceField(
required=False,
choices=[('', 'All Hospitals')],
widget=forms.Select(attrs={'class': 'form-select'})
required=False, choices=[("", "All Hospitals")], widget=forms.Select(attrs={"class": "form-select"})
)
department = forms.ChoiceField(
required=False,
choices=[('', 'All Departments')],
widget=forms.Select(attrs={'class': 'form-select'})
required=False, choices=[("", "All Departments")], widget=forms.Select(attrs={"class": "form-select"})
)
search = forms.CharField(
required=False,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Search RCAs...'
})
)
date_from = forms.DateField(
required=False,
widget=forms.DateInput(attrs={'class': 'form-control', 'type': 'date'})
)
date_to = forms.DateField(
required=False,
widget=forms.DateInput(attrs={'class': 'form-control', 'type': 'date'})
required=False, widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "Search RCAs..."})
)
date_from = forms.DateField(required=False, widget=forms.DateInput(attrs={"class": "form-control", "type": "date"}))
date_to = forms.DateField(required=False, widget=forms.DateInput(attrs={"class": "form-control", "type": "date"}))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Populate hospital choices
from apps.organizations.models import Hospital
hospital_choices = [('', 'All Hospitals')]
hospital_choices.extend([
(h.id, h.name)
for h in Hospital.objects.all()
])
self.fields['hospital'].choices = hospital_choices
hospital_choices = [("", "All Hospitals")]
hospital_choices.extend([(h.id, h.name) for h in Hospital.objects.all()])
self.fields["hospital"].choices = hospital_choices
class RCAStatusChangeForm(forms.Form):
"""Form for changing RCA status"""
new_status = forms.ChoiceField(
choices=RCAStatus.choices,
widget=forms.Select(attrs={'class': 'form-select'})
)
new_status = forms.ChoiceField(choices=RCAStatus.choices, widget=forms.Select(attrs={"class": "form-select"}))
notes = forms.CharField(
required=False,
widget=forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Add notes about this status change'
})
widget=forms.Textarea(
attrs={"class": "form-control", "rows": 3, "placeholder": "Add notes about this status change"}
),
)
class RCAApprovalForm(forms.Form):
"""Form for approving RCA"""
approval_notes = forms.CharField(
required=False,
widget=forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Add approval notes'
})
widget=forms.Textarea(attrs={"class": "form-control", "rows": 3, "placeholder": "Add approval notes"}),
)
class RCAClosureForm(forms.Form):
"""Form for closing RCA"""
closure_notes = forms.CharField(
required=True,
widget=forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Provide closure notes and summary'
})
widget=forms.Textarea(
attrs={"class": "form-control", "rows": 3, "placeholder": "Provide closure notes and summary"}
),
)
actual_completion_date = forms.DateField(
required=True,
widget=forms.DateInput(attrs={
'class': 'form-control',
'type': 'date'
})
required=True, widget=forms.DateInput(attrs={"class": "form-control", "type": "date"})
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Set default to today
self.fields['actual_completion_date'].initial = timezone.now().date()
self.fields["actual_completion_date"].initial = timezone.now().date()
class RCAAttachmentForm(forms.ModelForm):
"""Form for uploading RCA attachments"""
class Meta:
model = RCAAttachment
fields = ['file', 'description']
fields = ["file", "description"]
widgets = {
'file': forms.FileInput(attrs={'class': 'form-control'}),
'description': forms.Textarea(attrs={
'class': 'form-control',
'rows': 2,
'placeholder': 'Describe this attachment'
}),
"file": forms.FileInput(attrs={"class": "form-control"}),
"description": forms.Textarea(
attrs={"class": "form-control", "rows": 2, "placeholder": "Describe this attachment"}
),
}
class RCANoteForm(forms.ModelForm):
"""Form for adding RCA notes"""
class Meta:
model = RCANote
fields = ['note', 'is_internal']
fields = ["note", "is_internal"]
widgets = {
'note': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Add a note'
}),
'is_internal': forms.CheckboxInput(attrs={
'class': 'form-check-input'
}),
"note": forms.Textarea(attrs={"class": "form-control", "rows": 3, "placeholder": "Add a note"}),
"is_internal": forms.CheckboxInput(attrs={"class": "form-check-input"}),
}