Source

Mango / forms.py

import urllib2

from django import forms
from django.conf import settings

from mango.settings import *
from mango.utils import akismet_request


class CommentForm(forms.Form):

    author_name = forms.CharField(label='Name', initial='name', max_length=100)
    author_email = forms.EmailField(label='E-mail', initial='e-mail address', max_length=100)
    author_url = forms.URLField(label='Website', initial='Web address (optional)', max_length=100, required=False)
    message = forms.CharField(label='Comment')
    subscribe = forms.BooleanField(label='Notify me of follow-up comments via e-mail', required=False)

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(CommentForm, self).__init__(*args, **kwargs)

    def clean_author_name(self):
        return self.cleaned_data.get('author_name', '').strip()

    def clean_author_email(self):
        return self.cleaned_data.get('author_email', '').strip().lower()

    def clean_author_url(self):
        return self.cleaned_data.get('author_url', '').strip()

    def clean_message(self):
        return self.cleaned_data.get('message', '').strip().replace('\r\n', '\n')

    def clean(self):
        if AKISMET_API_KEY and self.is_valid():
            args = {
                'blog': BASE_URL,
                'user_ip': self.request.META['REMOTE_ADDR'],
                'user_agent': self.request.META['HTTP_USER_AGENT'],
                'referrer': self.request.META['HTTP_REFERER'],
                'permalink': self.request.META['HTTP_REFERER'],
                'comment_type': 'comment',
                'comment_author': self.cleaned_data['author_name'],
                'comment_content': self.cleaned_data['message'],
            }
            if self.cleaned_data['author_url']:
                args['comment_author_url'] = self.cleaned_data['author_url']

            f = urllib2.urlopen(akismet_request('comment-check', args))
            spam = f.read().strip() == 'true'
            f.close()

            if spam:
                raise forms.ValidationError('Comment appears to be spam!')

        return self.cleaned_data


class ContactForm(forms.Form):

    sender_name = forms.CharField(label='Name', initial='your name', max_length=100)
    sender_email = forms.EmailField(label='E-mail', initial='your e-mail address', max_length=100)
    subject = forms.CharField(label='Subject', initial='subject', max_length=100, required=False)
    message = forms.CharField(label='Message')
    cc_sender = forms.BooleanField(label='Send me a copy of this message', required=False)

    def clean_sender_name(self):
        return self.cleaned_data.get('sender_name', '').strip()

    def clean_sender_email(self):
        return self.cleaned_data.get('sender_email', '').strip().lower()

    def clean_subject(self):
        return self.cleaned_data.get('subject', '').strip()

    def clean_message(self):
        return self.cleaned_data.get('message', '').strip().replace('\r\n', '\n')