From b3d6652e47a844f161aea0ebab00dc871a288e8c Mon Sep 17 00:00:00 2001 From: mortezaei Date: Sat, 6 Sep 2025 22:43:09 +0330 Subject: [PATCH] feat(course): require thumbnail in CourseForm and enforce validation rules - Made thumbnail field required in CourseForm. - Added validation to prevent clearing existing thumbnail. - Enforced requirement for uploading a thumbnail on creation or when no existing thumbnail is present. --- apps/course/admin/course.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/course/admin/course.py b/apps/course/admin/course.py index 81ed23e..afaaaf8 100644 --- a/apps/course/admin/course.py +++ b/apps/course/admin/course.py @@ -112,6 +112,25 @@ class CourseForm(forms.ModelForm): super().__init__(*args, **kwargs) # Make short_description required self.fields['short_description'].required = True + # Make thumbnail required (show required star in add/change forms) + if 'thumbnail' in self.fields: + self.fields['thumbnail'].required = True + + def clean(self): + cleaned_data = super().clean() + thumbnail = cleaned_data.get('thumbnail') + has_existing_thumbnail = bool(getattr(self.instance, 'thumbnail', None)) + + # Disallow clearing the existing thumbnail (must always have a value) + if thumbnail is False: + self.add_error('thumbnail', _('This field is required and cannot be cleared.')) + return cleaned_data + + # On create or when no existing thumbnail, require uploading one + if (thumbnail is None or thumbnail == '') and not has_existing_thumbnail: + self.add_error('thumbnail', _('This field is required.')) + + return cleaned_data class CourseAttachmentInline(StackedInline):