Login

FormMail Clone

Author:
baumer1122
Posted:
March 25, 2008
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

A quickie clone of good old fashion Formmail.pl any form that is a subclass of FormMail will have its conents emailed to all staff members on the site.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django import newforms as forms
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail

class FormMail(forms.Form):
    def notify(self):
        """
        Sends an email to all members of the staff with ordered list of fields
        and values for any form that subclasses FormMail
        """
        site_name = Site.objects.get_current().name
        form_name = self.__class__.__name__
        subject = '%s %s Submission' % (site_name, form_name)
        user_list = [u for u in User.objects.filter(is_staff=True).exclude(email="").order_by('id')]
        message = ""
        for k in self.base_fields.keyOrder:
            message = message + '%s: %s\n\n' % (self[k].label, self.cleaned_data[k])
        send_mail(subject, message, user_list[0], user_list)
        
## Example form
class ContactForm(FormMail):
    name = forms.CharField()
    phone = forms.CharField(required=False)
    email = forms.EmailField()
    comment = forms.CharField(widget=forms.Textarea())

## Example view code

form = ContactForm(request.POST)
if form.is_valid()
    form.notify()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

rainleader (on May 20, 2008):

Line 14 should be:

user_list = [u.email for u in User.objects.filter(is_staff=True).exclude(email="").order_by('id')]

#

Please login first before commenting.