<s> import datetime <EOL> import random <EOL> import re <EOL> import sha <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . sites . models import Site <EOL> SHA1_RE = re . compile ( '<STR_LIT>' ) <EOL> class RegistrationManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def activate_user ( self , activation_key ) : <EOL> """<STR_LIT>""" <EOL> if SHA1_RE . search ( activation_key ) : <EOL> try : <EOL> profile = self . get ( activation_key = activation_key ) <EOL> except self . model . DoesNotExist : <EOL> return False <EOL> if not profile . activation_key_expired ( ) : <EOL> user = profile . user <EOL> user . is_active = True <EOL> user . save ( ) <EOL> profile . activation_key = "<STR_LIT>" <EOL> profile . save ( ) <EOL> return user <EOL> return False <EOL> def create_inactive_user ( self , username , password , email , <EOL> send_email = True , profile_callback = None ) : <EOL> """<STR_LIT>""" <EOL> new_user = User . objects . create_user ( username , email , password ) <EOL> new_user . is_active = False <EOL> new_user . save ( ) <EOL> registration_profile = self . create_profile ( new_user ) <EOL> if profile_callback is not None : <EOL> profile_callback ( user = new_user ) <EOL> if send_email : <EOL> from django . core . mail import send_mail <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>' : registration_profile . activation_key , <EOL> '<STR_LIT>' : settings . ACCOUNT_ACTIVATION_DAYS , <EOL> '<STR_LIT>' : current_site } ) <EOL> send_mail ( subject , message , settings . DEFAULT_FROM_EMAIL , [ new_user . email ] ) <EOL> return new_user <EOL> def create_profile ( self , user ) : <EOL> """<STR_LIT>""" <EOL> salt = sha . new ( str ( random . random ( ) ) ) . hexdigest ( ) [ : <NUM_LIT:5> ] <EOL> activation_key = sha . new ( salt + user . username ) . hexdigest ( ) <EOL> return self . create ( user = user , <EOL> activation_key = activation_key ) <EOL> def delete_expired_users ( self ) : <EOL> """<STR_LIT>""" <EOL> for profile in self . all ( ) : <EOL> if profile . activation_key_expired ( ) : <EOL> user = profile . user <EOL> if not user . is_active : <EOL> user . delete ( ) <EOL> class RegistrationProfile ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> user = models . ForeignKey ( User , unique = True , verbose_name = _ ( '<STR_LIT:user>' ) ) <EOL> activation_key = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT> ) <EOL> objects = RegistrationManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % self . user <EOL> def activation_key_expired ( self ) : <EOL> """<STR_LIT>""" <EOL> expiration_date = datetime . timedelta ( days = settings . ACCOUNT_ACTIVATION_DAYS ) <EOL> return self . activation_key == "<STR_LIT>" or ( self . user . date_joined + expiration_date <= datetime . datetime . now ( ) ) <EOL> activation_key_expired . boolean = True </s>
