<s> from datetime import datetime , timedelta <EOL> from random import random <EOL> import sha <EOL> from django . conf import settings <EOL> from django . db import models , IntegrityError <EOL> from django . template . loader import render_to_string <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . sites . models import Site <EOL> from django . contrib . auth . models import User <EOL> from emailconfirmation . utils import get_send_mail <EOL> send_mail = get_send_mail ( ) <EOL> class EmailAddressManager ( models . Manager ) : <EOL> def add_email ( self , user , email ) : <EOL> try : <EOL> email_address = self . create ( user = user , email = email ) <EOL> EmailConfirmation . objects . send_confirmation ( email_address ) <EOL> return email_address <EOL> except IntegrityError : <EOL> return None <EOL> def get_primary ( self , user ) : <EOL> try : <EOL> return self . get ( user = user , primary = True ) <EOL> except EmailAddress . DoesNotExist : <EOL> return None <EOL> def get_users_for ( self , email ) : <EOL> """<STR_LIT>""" <EOL> return [ address . user for address in EmailAddress . objects . filter ( verified = True , email = email ) ] <EOL> class EmailAddress ( models . Model ) : <EOL> user = models . ForeignKey ( User ) <EOL> email = models . EmailField ( ) <EOL> verified = models . BooleanField ( default = False ) <EOL> primary = models . BooleanField ( default = False ) <EOL> objects = EmailAddressManager ( ) <EOL> def set_as_primary ( self , conditional = False ) : <EOL> old_primary = EmailAddress . objects . get_primary ( self . user ) <EOL> if old_primary : <EOL> if conditional : <EOL> return False <EOL> old_primary . primary = False <EOL> old_primary . save ( ) <EOL> self . primary = True <EOL> self . save ( ) <EOL> self . user . email = self . email <EOL> self . user . save ( ) <EOL> return True <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . email , self . user ) <EOL> class Meta : <EOL> unique_together = ( <EOL> ( "<STR_LIT:user>" , "<STR_LIT:email>" ) , <EOL> ) <EOL> class EmailConfirmationManager ( models . Manager ) : <EOL> def confirm_email ( self , confirmation_key ) : <EOL> try : <EOL> confirmation = self . get ( confirmation_key = confirmation_key ) <EOL> except self . model . DoesNotExist : <EOL> return None <EOL> if not confirmation . key_expired ( ) : <EOL> email_address = confirmation . email_address <EOL> email_address . verified = True <EOL> email_address . set_as_primary ( conditional = True ) <EOL> email_address . save ( ) <EOL> return email_address <EOL> def send_confirmation ( self , email_address ) : <EOL> salt = sha . new ( str ( random ( ) ) ) . hexdigest ( ) [ : <NUM_LIT:5> ] <EOL> confirmation_key = sha . new ( salt + email_address . email ) . hexdigest ( ) <EOL> current_site = Site . objects . get_current ( ) <EOL> activate_url = u"<STR_LIT>" % ( <EOL> unicode ( current_site . domain ) , <EOL> reverse ( "<STR_LIT>" , args = ( confirmation_key , ) ) <EOL> ) <EOL> context = { <EOL> "<STR_LIT:user>" : email_address . user , <EOL> "<STR_LIT>" : activate_url , <EOL> "<STR_LIT>" : current_site , <EOL> "<STR_LIT>" : confirmation_key , <EOL> } <EOL> subject = render_to_string ( "<STR_LIT>" , context ) <EOL> message = render_to_string ( "<STR_LIT>" , context ) <EOL> send_mail ( subject , message , settings . DEFAULT_FROM_EMAIL , [ email_address . email ] , priority = "<STR_LIT>" ) <EOL> return self . create ( email_address = email_address , sent = datetime . now ( ) , confirmation_key = confirmation_key ) <EOL> def delete_expired_confirmations ( self ) : <EOL> for confirmation in self . all ( ) : <EOL> if confirmation . key_expired ( ) : <EOL> confirmation . delete ( ) <EOL> class EmailConfirmation ( models . Model ) : <EOL> email_address = models . ForeignKey ( EmailAddress ) <EOL> sent = models . DateTimeField ( ) <EOL> confirmation_key = models . CharField ( max_length = <NUM_LIT> ) <EOL> objects = EmailConfirmationManager ( ) <EOL> def key_expired ( self ) : <EOL> expiration_date = self . sent + timedelta ( days = settings . EMAIL_CONFIRMATION_DAYS ) <EOL> return expiration_date <= datetime . now ( ) <EOL> key_expired . boolean = True <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % self . email_address </s>
