# Path: comments_spamfighter/models.py
# class Keyword(models.Model):
# 
#     # Default field choices. These are good inital values for the default
#     # Django comments but you can override it using the settings variable
#     # COMMENTS_CHECK_FIELDS_CHOICES
# 
#     FIELD_CHOICES = (
#         ("user_name", _("Username")),
#         ("user_email", _("User Email")),
#         ("user_url", _("User URL")),
#         ("comment", _("Comment text")),
#         ("ip_address", _("IP Address")),
#     )
#     FIELD_CHOICES = getattr(settings, "COMMENTS_CHECK_FIELDS_CHOICES", FIELD_CHOICES)
# 
#     active = models.BooleanField(_("Active"), default=True)
#     keyword = models.TextField(_("Keyword"))
#     is_regex = models.BooleanField(_("Is a regular expression"), default=False)
#     fields = models.TextField(_("Fields to check"), max_length=255)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
#     modified = models.DateTimeField(_("Modified"), auto_now=True)
# 
#     class Meta:
#         ordering = ("keyword", "created")
#         verbose_name = _("Keyword")
#         verbose_name_plural = _("Keywords")
# 
#     def __str__(self):
#         return self.keyword


import re
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from .models import Keyword
class KeywordAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(KeywordAdminForm, self).__init__(*args, **kwargs)
        self.initial["fields"] = self.instance.fields.split(",")

    keyword = forms.CharField(
        widget=forms.Textarea(
            attrs={
                "style": "height: 1.5em; line-height: 1.5em; width: 40em;",
                "class": "vLargeTextField",
            }
        )
    )
    fields = forms.MultipleChoiceField(label=_("Fields to check"), choices=Keyword.FIELD_CHOICES)

    def clean_fields(self):
        return ",".join(self.cleaned_data["fields"])

    def clean(self):
        # Do a test match like the moderator does to ensure that the regular
        # expression is valid.
        if self.cleaned_data["is_regex"]:
            try:
                re.match(self.cleaned_data["keyword"], "", re.MULTILINE)
            except Exception as e:
                raise forms.ValidationError(_('This regular expression is not valid. Error message was: "%s"' % e))
        return self.cleaned_data

    class Meta:
        fields = "__all__"

# Path: comments_spamfighter/models.py
# class Keyword(models.Model):
# 
#     # Default field choices. These are good inital values for the default
#     # Django comments but you can override it using the settings variable
#     # COMMENTS_CHECK_FIELDS_CHOICES
# 
#     FIELD_CHOICES = (
#         ("user_name", _("Username")),
#         ("user_email", _("User Email")),
#         ("user_url", _("User URL")),
#         ("comment", _("Comment text")),
#         ("ip_address", _("IP Address")),
#     )
#     FIELD_CHOICES = getattr(settings, "COMMENTS_CHECK_FIELDS_CHOICES", FIELD_CHOICES)
# 
#     active = models.BooleanField(_("Active"), default=True)
#     keyword = models.TextField(_("Keyword"))
#     is_regex = models.BooleanField(_("Is a regular expression"), default=False)
#     fields = models.TextField(_("Fields to check"), max_length=255)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
#     modified = models.DateTimeField(_("Modified"), auto_now=True)
# 
#     class Meta:
#         ordering = ("keyword", "created")
#         verbose_name = _("Keyword")
#         verbose_name_plural = _("Keywords")
# 
#     def __str__(self):
#         return self.keyword


# Path: comments_spamfighter/admin.py
import re
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from .models import Keyword

class KeywordAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(KeywordAdminForm, self).__init__(*args, **kwargs)
        self.initial["fields"] = self.instance.fields.split(",")

    keyword = forms.CharField(
        widget=forms.Textarea(
            attrs={
                "style": "height: 1.5em; line-height: 1.5em; width: 40em;",
                "class": "vLargeTextField",
            }
        )
    )
    fields = forms.MultipleChoiceField(label=_("Fields to check"), choices=Keyword.FIELD_CHOICES)

    def clean_fields(self):
        return ",".join(self.cleaned_data["fields"])

    def clean(self):
        # Do a test match like the moderator does to ensure that the regular
        # expression is valid.
        if self.cleaned_data["is_regex"]:
            try:
                re.match(self.cleaned_data["keyword"], "", re.MULTILINE)
            except Exception as e:
                raise forms.ValidationError(_('This regular expression is not valid. Error message was: "%s"' % e))
        return self.cleaned_data

    class Meta:
        fields = "__all__"

        model = Keyword