class CategoryFormBase(forms.ModelForm): name = forms.CharField(label=_("Name"), validators=[validate_sluggable()]) description = forms.CharField( label=_("Description"), max_length=2048, required=False, widget=forms.Textarea(attrs={'rows': 3}), help_text=_( "Optional description explaining category intented purpose.")) css_class = forms.CharField( label=_("CSS class"), required=False, help_text=_("Optional CSS class used to customize this category " "appearance from templates.")) is_closed = YesNoSwitch( label=_("Closed category"), required=False, help_text=_("Only members with valid permissions can post in " "closed categories.")) css_class = forms.CharField( label=_("CSS class"), required=False, help_text=_("Optional CSS class used to customize this category " "appearance from templates.")) prune_started_after = forms.IntegerField( label=_("Thread age"), min_value=0, help_text=_("Prune thread if number of days since its creation is " "greater than specified. Enter 0 to disable this " "pruning criteria.")) prune_replied_after = forms.IntegerField( label=_("Last reply"), min_value=0, help_text=_("Prune thread if number of days since last reply is " "greater than specified. Enter 0 to disable this " "pruning criteria.")) class Meta: model = Category fields = [ 'name', 'description', 'css_class', 'is_closed', 'prune_started_after', 'prune_replied_after', 'archive_pruned_in', ] def clean_copy_permissions(self): data = self.cleaned_data['copy_permissions'] if data and data.pk == self.instance.pk: message = _( "Permissions cannot be copied from category into itself.") raise forms.ValidationError(message) return data def clean_archive_pruned_in(self): data = self.cleaned_data['archive_pruned_in'] if data and data.pk == self.instance.pk: message = _("Category cannot act as archive for itself.") raise forms.ValidationError(message) return data def clean(self): data = super(CategoryFormBase, self).clean() self.instance.set_name(data.get('name')) return data
class ForumFormBase(forms.ModelForm): role = forms.ChoiceField(label=_("Type"), choices=FORUM_ROLES) name = forms.CharField(label=_("Name"), validators=[validate_sluggable()]) description = forms.CharField( label=_("Description"), max_length=2048, required=False, widget=forms.Textarea(attrs={'rows': 3}), help_text=_("Optional description explaining forum intented " "purpose.")) redirect_url = forms.URLField( label=_("Redirect URL"), validators=[validate_sluggable()], help_text=_('If forum type is redirect, enter here its URL.'), required=False) css_class = forms.CharField( label=_("CSS class"), required=False, help_text=_("Optional CSS class used to customize this forum " "appearance from templates.")) is_closed = forms.YesNoSwitch( label=_("Closed forum"), required=False, help_text=_("Only members with valid permissions can post in " "closed forums.")) css_class = forms.CharField( label=_("CSS class"), required=False, help_text=_("Optional CSS class used to customize this forum " "appearance from templates.")) prune_started_after = forms.IntegerField( label=_("Thread age"), min_value=0, help_text=_("Prune thread if number of days since its creation is " "greater than specified. Enter 0 to disable this " "pruning criteria.")) prune_replied_after = forms.IntegerField( label=_("Last reply"), min_value=0, help_text=_("Prune thread if number of days since last reply is " "greater than specified. Enter 0 to disable this " "pruning criteria.")) class Meta: model = Forum fields = [ 'role', 'name', 'description', 'redirect_url', 'css_class', 'is_closed', 'prune_started_after', 'prune_replied_after', 'archive_pruned_in', ] def clean_copy_permissions(self): data = self.cleaned_data['copy_permissions'] if data and data.pk == self.instance.pk: message = _("Permissions cannot be copied from forum into itself.") raise forms.ValidationError(message) return data def clean_archive_pruned_in(self): data = self.cleaned_data['archive_pruned_in'] if data and data.pk == self.instance.pk: message = _("Forum cannot act as archive for itself.") raise forms.ValidationError(message) return data def clean(self): data = super(ForumFormBase, self).clean() self.instance.set_name(data.get('name')) if data['role'] != 'category': if not data['new_parent'].level: message = _("Only categories can have no parent category.") raise forms.ValidationError(message) if data['role'] == 'redirect': if not data.get('redirect_url'): message = _("This forum is redirect, yet you haven't " "specified URL to which it should redirect " "after click.") raise forms.ValidationError(message) return data