<s> from django . db import models <EOL> from django . conf import settings <EOL> from django . core . mail import send_mail <EOL> from django . template . loader import render_to_string <EOL> from django . contrib . auth . models import User , Permission , Group <EOL> from django . contrib . sites . models import Site <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import datetime <EOL> import random <EOL> import re <EOL> import logging <EOL> import sys , traceback <EOL> SHA1_RE = re . compile ( '<STR_LIT>' ) <EOL> alphabet = '<STR_LIT>' <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class EmailValidationManager ( models . Manager ) : <EOL> @ classmethod <EOL> def send ( self , user ) : <EOL> ev = EmailValidation ( ) <EOL> ev . user = user <EOL> ev . email = user . email <EOL> ev . date_requested = datetime . datetime . now ( ) <EOL> ev . activation_key = '<STR_LIT>' . join ( [ random . sample ( alphabet , <NUM_LIT:1> ) [ <NUM_LIT:0> ] for x in range ( <NUM_LIT> ) ] ) <EOL> ev . save ( ) <EOL> logger . debug ( "<STR_LIT>" , ev . activation_key ) <EOL> current_site = Site . objects . get_current ( ) <EOL> subject = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : current_site } ) <EOL> subject = '<STR_LIT>' . join ( subject . splitlines ( ) ) <EOL> message = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : ev . activation_key , <EOL> '<STR_LIT>' : current_site } ) <EOL> try : <EOL> send_mail ( subject , message , settings . DEFAULT_FROM_EMAIL , [ ev . email ] ) <EOL> except Exception : <EOL> exceptionType , exceptionValue , exceptionTraceback = sys . exc_info ( ) <EOL> logger . error ( "<STR_LIT:%s>" , '<STR_LIT>' . join ( traceback . format_exception ( exceptionType , exceptionValue , exceptionTraceback ) ) ) <EOL> return ev <EOL> class EmailValidation ( models . Model ) : <EOL> user = models . ForeignKey ( User ) <EOL> email = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> date_requested = models . DateField ( ) <EOL> activation_key = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT> ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( str ( self . user ) , str ( self . date_requested ) ) <EOL> objects = EmailValidationManager ( ) <EOL> @ classmethod <EOL> def validate ( self , user , key ) : <EOL> if not SHA1_RE . search ( key ) : <EOL> return ( False , _ ( "<STR_LIT>" ) % { '<STR_LIT:key>' : key } ) <EOL> try : <EOL> ev = self . objects . get ( activation_key = key ) <EOL> if user . email != ev . email : <EOL> return ( False , _ ( "<STR_LIT>" ) ) <EOL> if ev . date_requested + datetime . timedelta ( days = <NUM_LIT:3> ) < datetime . date . today ( ) : <EOL> return ( False , _ ( "<STR_LIT>" ) ) <EOL> try : <EOL> g = Group . objects . get ( name = '<STR_LIT>' ) <EOL> except Group . DoesNotExist : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> g = Group . objects . create ( name = '<STR_LIT>' ) <EOL> g . permissions . add ( Permission . objects . get ( name = '<STR_LIT>' ) ) <EOL> g . permissions . add ( Permission . objects . get ( name = '<STR_LIT>' ) ) <EOL> g . permissions . add ( Permission . objects . get ( name = '<STR_LIT>' ) ) <EOL> ev . user . groups . add ( g ) <EOL> p = user . get_profile ( ) <EOL> p . email_notification = '<STR_LIT:D>' <EOL> p . save ( ) <EOL> ev . activation_key = '<STR_LIT>' <EOL> ev . save ( ) <EOL> return ( True , "<STR_LIT>" ) <EOL> except self . DoesNotExist : <EOL> return ( False , _ ( "<STR_LIT>" ) ) <EOL> except Exception , e : <EOL> exceptionType , exceptionValue , exceptionTraceback = sys . exc_info ( ) <EOL> logger . error ( "<STR_LIT:%s>" , '<STR_LIT>' . join ( traceback . format_exception ( exceptionType , exceptionValue , exceptionTraceback ) ) ) <EOL> return ( False , "<STR_LIT>" ) </s>
