1. Curtis Maloney
  2. gnocchi-blog

Source

gnocchi-blog / gnocchi_blog / forms.py

from django import forms
from gnocchi_blog import models
from taggit.models import TaggedItem
import re
from gnocchi_blog import recaptcha

class PostAdminForm(forms.ModelForm):
    auto = forms.BooleanField(label='Auto-tag?', required=False,
        help_text="Automatically scan content for tags"
    )
    class Meta:
        model = models.Post
    def clean(self):
        data = self.cleaned_data
        if data['auto']:
            tags = set(data['tags'])
            tags.update([
                tag
                for tag in TaggedItem.tags_for(models.Post)
                if re.search(r'\b%s\b' % tag.name, data['content'], re.I|re.M)
            ])
            data['tags'] = list(tags)
        return data

class CommentForm(recaptcha.BaseRecaptchaForm, forms.ModelForm):
    captcha = recaptcha.RecaptchaField()
    class Meta:
        model = models.Comment
        fields = (
            'user_name',
            'user_email',
            'content',
        )