32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
from django import forms
|
|
|
|
from .models import StaffProfile
|
|
|
|
|
|
class StaffProfileAdminForm(forms.ModelForm):
|
|
"""Upload/replace avatars without exposing a direct public media URL."""
|
|
role = forms.ChoiceField(
|
|
choices=StaffProfile.Role.choices,
|
|
required=False,
|
|
initial=StaffProfile.Role.SALES_EXPERT,
|
|
label="نقش سازمانی",
|
|
)
|
|
avatar = forms.ImageField(
|
|
required=False,
|
|
widget=forms.FileInput(attrs={"accept": "image/jpeg,image/png,image/webp"}),
|
|
help_text="تصویر JPG، PNG یا WebP انتخاب کنید. تصویر جدید جایگزین تصویر فعلی میشود.",
|
|
)
|
|
|
|
class Meta:
|
|
model = StaffProfile
|
|
fields = "__all__"
|
|
|
|
def clean_avatar(self):
|
|
avatar = self.cleaned_data.get("avatar")
|
|
if avatar and avatar.size > 5 * 1024 * 1024:
|
|
raise forms.ValidationError("حجم تصویر پروفایل نباید بیشتر از ۵ مگابایت باشد.")
|
|
return avatar
|
|
|
|
def clean_role(self):
|
|
return self.cleaned_data.get("role") or StaffProfile.Role.SALES_EXPERT
|