Browse Source

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.
master
mortezaei 9 months ago
parent
commit
b3d6652e47
  1. 19
      apps/course/admin/course.py

19
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):

Loading…
Cancel
Save