Ejemplo n.º 1
0
class BanUsersForm(forms.Form):
    ban_type = forms.MultipleChoiceField(
        label=_("Values to ban"),
        widget=forms.CheckboxSelectMultiple,
        choices=(('usernames', _('Usernames')), ('emails', _('E-mails')),
                 ('domains', _('E-mail domains')), ('ip', _('IP addresses')),
                 ('ip_first', _('First segment of IP addresses')),
                 ('ip_two', _('First two segments of IP addresses'))))
    user_message = forms.CharField(
        label=_("User message"),
        required=False,
        max_length=1000,
        help_text=_("Optional message displayed to users "
                    "instead of default one."),
        widget=forms.Textarea(attrs={'rows': 3}),
        error_messages={
            'max_length': _("Message can't be longer than 1000 characters.")
        })
    staff_message = forms.CharField(
        label=_("Team message"),
        required=False,
        max_length=1000,
        help_text=_("Optional ban message for moderators and administrators."),
        widget=forms.Textarea(attrs={'rows': 3}),
        error_messages={
            'max_length': _("Message can't be longer than 1000 characters.")
        })
    expires_on = forms.IsoDateTimeField(
        label=_("Expires on"),
        required=False,
        help_text=_('Leave this field empty for set bans to never expire.'))
Ejemplo n.º 2
0
class BanForm(forms.ModelForm):
    check_type = forms.TypedChoiceField(label=_("Check type"),
                                        coerce=int,
                                        choices=BANS_CHOICES)
    banned_value = forms.CharField(
        label=_("Banned value"),
        max_length=250,
        help_text=_('This value is case-insensitive and accepts asterisk (*) '
                    'for rought matches. For example, making IP ban for value '
                    '"83.*" will ban all IP addresses beginning with "83.".'),
        error_messages={
            'max_length': _("Banned value can't be longer "
                            "than 250 characters.")
        })
    user_message = forms.CharField(
        label=_("User message"),
        required=False,
        max_length=1000,
        help_text=_("Optional message displayed to user "
                    "instead of default one."),
        widget=forms.Textarea(attrs={'rows': 3}),
        error_messages={
            'max_length': _("Message can't be longer than 1000 characters.")
        })
    staff_message = forms.CharField(
        label=_("Team message"),
        required=False,
        max_length=1000,
        help_text=_("Optional ban message for moderators and administrators."),
        widget=forms.Textarea(attrs={'rows': 3}),
        error_messages={
            'max_length': _("Message can't be longer than 1000 characters.")
        })
    expires_on = forms.IsoDateTimeField(
        label=_("Expires on"),
        required=False,
        help_text=_('Leave this field empty for this ban to never expire.'))

    class Meta:
        model = Ban
        fields = [
            'check_type',
            'banned_value',
            'user_message',
            'staff_message',
            'expires_on',
        ]

    def clean_banned_value(self):
        data = self.cleaned_data['banned_value']
        while '**' in data:
            data = data.replace('**', '*')

        if data == '*':
            raise forms.ValidationError(_("Banned value is too vague."))

        return data