""" Physicians Forms Forms for doctor rating imports and filtering. """ from django import forms from apps.organizations.models import Hospital from apps.core.form_mixins import HospitalFieldMixin class DoctorRatingImportForm(HospitalFieldMixin, forms.Form): """ Form for importing doctor ratings from CSV. Hospital field visibility: - PX Admins: See dropdown with all hospitals - Others: Hidden field, auto-set to user's hospital """ hospital = forms.ModelChoiceField( queryset=Hospital.objects.filter(status="active"), label="Hospital", help_text="Select the hospital these ratings belong to", ) csv_file = forms.FileField( label="CSV File", help_text="Upload the Doctor Rating Report CSV file", widget=forms.FileInput(attrs={"accept": ".csv"}), ) skip_header_rows = forms.IntegerField( label="Skip Header Rows", initial=6, min_value=0, max_value=20, help_text="Number of rows to skip before the column headers (Doctor Rating Report typically has 6 header rows)", ) def clean_csv_file(self): csv_file = self.cleaned_data["csv_file"] # Check file extension if not csv_file.name.endswith(".csv"): raise forms.ValidationError("File must be a CSV file (.csv extension)") # Check file size (max 10MB) if csv_file.size > 10 * 1024 * 1024: raise forms.ValidationError("File size must be less than 10MB") return csv_file class DoctorRatingFetchForm(HospitalFieldMixin, forms.Form): """ Form for fetching doctor ratings from HIS API by date range. Hospital field visibility: - PX Admins: See dropdown with all hospitals - Others: Hidden field, auto-set to user's hospital """ hospital = forms.ModelChoiceField( queryset=Hospital.objects.filter(status="active"), label="Hospital", help_text="Select the hospital for tracking the import job", ) from_date = forms.DateField( label="From Date", widget=forms.DateInput( attrs={ "type": "date", "class": "w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none transition", } ), help_text="Start date for fetching ratings", ) to_date = forms.DateField( label="To Date", widget=forms.DateInput( attrs={ "type": "date", "class": "w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none transition", } ), help_text="End date for fetching ratings", ) def clean(self): cleaned_data = super().clean() from_date = cleaned_data.get("from_date") to_date = cleaned_data.get("to_date") if from_date and to_date and from_date > to_date: raise forms.ValidationError("From date must be before or equal to to date.") return cleaned_data class DoctorRatingFilterForm(forms.Form): """ Form for filtering individual doctor ratings. """ hospital = forms.ModelChoiceField( queryset=Hospital.objects.filter(status="active"), required=False, label="Hospital" ) doctor_id = forms.CharField( required=False, label="Doctor ID", widget=forms.TextInput(attrs={"placeholder": "e.g., 10738"}) ) doctor_name = forms.CharField( required=False, label="Doctor Name", widget=forms.TextInput(attrs={"placeholder": "Search by doctor name"}) ) rating_min = forms.IntegerField( required=False, min_value=1, max_value=5, label="Min Rating", widget=forms.NumberInput(attrs={"placeholder": "1-5"}), ) rating_max = forms.IntegerField( required=False, min_value=1, max_value=5, label="Max Rating", widget=forms.NumberInput(attrs={"placeholder": "1-5"}), ) date_from = forms.DateField(required=False, label="From Date", widget=forms.DateInput(attrs={"type": "date"})) date_to = forms.DateField(required=False, label="To Date", widget=forms.DateInput(attrs={"type": "date"})) source = forms.ChoiceField( required=False, label="Source", choices=[("", "All Sources")] + [("his_api", "HIS API"), ("csv_import", "CSV Import"), ("manual", "Manual Entry")], ) def __init__(self, user, *args, **kwargs): super().__init__(*args, **kwargs) # Filter hospital choices based on user role if user.is_px_admin(): self.fields["hospital"].queryset = Hospital.objects.filter(status="active") elif user.hospital: self.fields["hospital"].queryset = Hospital.objects.filter(id=user.hospital.id) self.fields["hospital"].initial = user.hospital else: self.fields["hospital"].queryset = Hospital.objects.none()