61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
"""Forms for the appreciation module."""
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from apps.appreciation.models import Appreciation
|
|
from apps.organizations.models import Department
|
|
|
|
|
|
class AppreciationForm(forms.ModelForm):
|
|
"""Internal form for creating an appreciation (department-targeted)."""
|
|
|
|
national_id = forms.CharField(
|
|
label=_("National ID/Iqama No."),
|
|
max_length=10,
|
|
required=True,
|
|
widget=forms.TextInput(attrs={
|
|
"class": "w-full px-4 py-3 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 outline-none",
|
|
"placeholder": _("National ID or Iqama number"),
|
|
"inputmode": "numeric",
|
|
"pattern": "[0-9]{10}",
|
|
"maxlength": "10",
|
|
"minlength": "10",
|
|
}),
|
|
help_text=_("Exactly 10 digits, numbers only."),
|
|
)
|
|
|
|
class Meta:
|
|
model = Appreciation
|
|
fields = ["department", "message_en", "national_id"]
|
|
widgets = {
|
|
"message_en": forms.Textarea(
|
|
attrs={
|
|
"rows": 5,
|
|
"class": "w-full px-4 py-3 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 outline-none",
|
|
"placeholder": "Write your message of appreciation...",
|
|
}
|
|
),
|
|
"department": forms.Select(
|
|
attrs={"class": "w-full px-4 py-3 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 outline-none"}
|
|
),
|
|
}
|
|
|
|
def clean_national_id(self):
|
|
national_id = self.cleaned_data.get("national_id", "").replace(" ", "")
|
|
if not national_id.isdigit():
|
|
raise forms.ValidationError(_("National ID/Iqama must contain digits only."))
|
|
if len(national_id) != 10:
|
|
raise forms.ValidationError(_("National ID/Iqama must be exactly 10 digits (you entered %(n)d).") % {"n": len(national_id)})
|
|
return national_id
|
|
|
|
def __init__(self, *args, hospital=None, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if hospital:
|
|
dept_qs = Department.objects.filter(hospital=hospital, status="active").order_by("name")
|
|
else:
|
|
dept_qs = Department.objects.filter(status="active").select_related("hospital").order_by("name")
|
|
self.fields["department"].label_from_instance = lambda obj: (
|
|
f"{obj.name} — {obj.hospital.name}" if obj.hospital else obj.name
|
|
)
|
|
self.fields["department"].queryset = dept_qs
|