FormMail Clone

 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. Clone method for Django models by nside 5 years, 9 months ago
  2. Clone model mixin by zakj 5 years, 3 months ago
  3. ROR link_to_remote clone. simple django ajax by qingfeng 5 years, 8 months ago
  4. Email validation by mulamvenky 11 months, 2 weeks ago
  5. Get email and push on couchdb - utils by magik_cypress 2 years, 1 month 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')]

#

(Forgotten your password?)