From 620fcc5a7fc83fb672cd49d105cd7371c6718b84 Mon Sep 17 00:00:00 2001 From: James Tauber Date: Sat, 26 Apr 2008 02:33:54 +0000 Subject: [PATCH] email address model and manager git-svn-id: http://django-email-confirmation.googlecode.com/svn/trunk@9 e143efbd-a74b-0410-b764-bd10452ab0ba --- devproject/emailconfirmation/models.py | 42 ++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/devproject/emailconfirmation/models.py b/devproject/emailconfirmation/models.py index 71a8362..75ab823 100644 --- a/devproject/emailconfirmation/models.py +++ b/devproject/emailconfirmation/models.py @@ -1,3 +1,41 @@ -from django.db import models +from django.db import models, IntegrityError +from django.template.loader import render_to_string +from django.core.mail import send_mail +from django.conf import settings -# Create your models here. +from django.contrib.auth.models import User + +# this code based in-part on django-registration + +class EmailAddressManager(models.Manager): + + def add_email(self, user, email): + try: + email_address = self.create(user=user, email=email) + subject = render_to_string("emailconfirmation/email_confirmation_subject.txt") + message = render_to_string("emailconfirmation/email_confirmation_message.txt") + # @@@ eventually use django-mailer + send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email]) + print "sent %s '%s' to %s" % (subject, message, email) + return email_address + except IntegrityError: + return None + +class EmailAddress(models.Model): + + user = models.ForeignKey(User) + email = models.EmailField() + verified = models.BooleanField(default=False) + + objects = EmailAddressManager() + + def __unicode__(self): + return u"%s (%s)" % (self.email, self.user) + + class Meta: + unique_together = ( + ("user", "email"), + ) + + class Admin: + pass