<s> from datetime import datetime <EOL> import re <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext . db import djangoforms <EOL> from django import newforms as forms <EOL> from django_ae_utils . auth import utils <EOL> class User ( db . Model ) : <EOL> first_name = db . StringProperty ( required = True ) <EOL> last_name = db . StringProperty ( required = True ) <EOL> email = db . EmailProperty ( required = True ) <EOL> password = db . StringProperty ( ) <EOL> last_login = db . DateTimeProperty ( ) <EOL> created = db . DateTimeProperty ( auto_now_add = True ) <EOL> modified = db . DateTimeProperty ( auto_now = True ) <EOL> def login ( self , email , password , request = None ) : <EOL> success = False <EOL> if self . authenticate ( email = email , password = password ) : <EOL> self . last_login = datetime . now ( ) <EOL> self . put ( ) <EOL> if request and request . session : <EOL> request . session [ '<STR_LIT:user>' ] = self <EOL> success = True <EOL> return success <EOL> def authenticate ( self , email , password ) : <EOL> success = False <EOL> if email and password : <EOL> try : <EOL> ( algorithm , stored_hash , salt ) = self . password . split ( '<STR_LIT:|>' ) <EOL> new_hash = utils . gen_hash ( password = password , salt = salt ) <EOL> if stored_hash == new_hash : <EOL> success = True <EOL> except Exception : <EOL> pass <EOL> return success <EOL> def set_password ( self , password ) : <EOL> to_store = '<STR_LIT>' <EOL> if password : <EOL> salt = utils . gen_salt ( ) <EOL> hash = utils . gen_hash ( password = password , salt = salt ) <EOL> algorithm = '<STR_LIT>' <EOL> to_store = '<STR_LIT:|>' . join ( ( algorithm , hash , salt ) ) <EOL> self . password = to_store <EOL> return to_store <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . first_name , self . last_name ) <EOL> class UniqueEmailField ( forms . Field ) : <EOL> def clean ( self , value ) : <EOL> email_re = re . compile ( r'<STR_LIT>' ) <EOL> if not email_re . match ( value ) : <EOL> raise forms . ValidationError ( '<STR_LIT>' ) <EOL> query = db . Query ( User ) <EOL> count = query . filter ( '<STR_LIT>' , value ) . count ( ) <EOL> if count > <NUM_LIT:0> : <EOL> raise forms . ValidationError ( '<STR_LIT>' % value ) <EOL> return value <EOL> class UserForm ( djangoforms . ModelForm ) : <EOL> email = UniqueEmailField ( ) <EOL> password = forms . CharField ( widget = forms . PasswordInput ) <EOL> class Meta : <EOL> model = User <EOL> exclude = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] </s>
