Mango / forms.py

import urllib2

from django import forms

import mango.settings
from mango.utils import akismet_request, normalizelinebreaks


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', widget=forms.Textarea)
    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):
        message = self.cleaned_data.get('message', '').strip()
        return normalizelinebreaks(message)

    def clean(self):
        if mango.settings.AKISMET_API_KEY and self.is_valid():
            args = {
                'blog': mango.settings.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', widget=forms.Textarea)
    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):
        message = self.cleaned_data.get('message', '').strip()
        return normalizelinebreaks(message)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.