<s> from werkzeug . security import generate_password_hash , check_password_hash <EOL> from itsdangerous import TimedJSONWebSignatureSerializer as Serializer <EOL> from flask import current_app <EOL> from flask . ext . login import UserMixin , AnonymousUserMixin <EOL> from . import db , login_manager <EOL> class Permission : <EOL> FOLLOW = <NUM_LIT> <EOL> COMMENT = <NUM_LIT> <EOL> WRITE_ARTICLES = <NUM_LIT> <EOL> MODERATE_COMMENTS = <NUM_LIT> <EOL> ADMINISTER = <NUM_LIT> <EOL> class Role ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> name = db . Column ( db . String ( <NUM_LIT:64> ) , unique = True ) <EOL> default = db . Column ( db . Boolean , default = False , index = True ) <EOL> permissions = db . Column ( db . Integer ) <EOL> users = db . relationship ( '<STR_LIT>' , backref = '<STR_LIT>' , lazy = '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def insert_roles ( ) : <EOL> roles = { <EOL> '<STR_LIT>' : ( Permission . FOLLOW | <EOL> Permission . COMMENT | <EOL> Permission . WRITE_ARTICLES , True ) , <EOL> '<STR_LIT>' : ( Permission . FOLLOW | <EOL> Permission . COMMENT | <EOL> Permission . WRITE_ARTICLES | <EOL> Permission . MODERATE_COMMENTS , False ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , False ) <EOL> } <EOL> for r in roles : <EOL> role = Role . query . filter_by ( name = r ) . first ( ) <EOL> if role is None : <EOL> role = Role ( name = r ) <EOL> role . permissions = roles [ r ] [ <NUM_LIT:0> ] <EOL> role . default = roles [ r ] [ <NUM_LIT:1> ] <EOL> db . session . add ( role ) <EOL> db . session . commit ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> class User ( UserMixin , db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> email = db . Column ( db . String ( <NUM_LIT:64> ) , unique = True , index = True ) <EOL> username = db . Column ( db . String ( <NUM_LIT:64> ) , unique = True , index = True ) <EOL> role_id = db . Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> password_hash = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> confirmed = db . Column ( db . Boolean , default = False ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( User , self ) . __init__ ( ** kwargs ) <EOL> if self . role is None : <EOL> if self . email == current_app . config [ '<STR_LIT>' ] : <EOL> self . role = Role . query . filter_by ( permissions = <NUM_LIT> ) . first ( ) <EOL> if self . role is None : <EOL> self . role = Role . query . filter_by ( default = True ) . first ( ) <EOL> @ property <EOL> def password ( self ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> @ password . setter <EOL> def password ( self , password ) : <EOL> self . password_hash = generate_password_hash ( password ) <EOL> def verify_password ( self , password ) : <EOL> return check_password_hash ( self . password_hash , password ) <EOL> def generate_confirmation_token ( self , expiration = <NUM_LIT> ) : <EOL> s = Serializer ( current_app . config [ '<STR_LIT>' ] , expiration ) <EOL> return s . dumps ( { '<STR_LIT>' : self . id } ) <EOL> def confirm ( self , token ) : <EOL> s = Serializer ( current_app . config [ '<STR_LIT>' ] ) <EOL> try : <EOL> data = s . loads ( token ) <EOL> except : <EOL> return False <EOL> if data . get ( '<STR_LIT>' ) != self . id : <EOL> return False <EOL> self . confirmed = True <EOL> db . session . add ( self ) <EOL> return True <EOL> def generate_reset_token ( self , expiration = <NUM_LIT> ) : <EOL> s = Serializer ( current_app . config [ '<STR_LIT>' ] , expiration ) <EOL> return s . dumps ( { '<STR_LIT>' : self . id } ) <EOL> def reset_password ( self , token , new_password ) : <EOL> s = Serializer ( current_app . config [ '<STR_LIT>' ] ) <EOL> try : <EOL> data = s . loads ( token ) <EOL> except : <EOL> return False <EOL> if data . get ( '<STR_LIT>' ) != self . id : <EOL> return False <EOL> self . password = new_password <EOL> db . session . add ( self ) <EOL> return True <EOL> def generate_email_change_token ( self , new_email , expiration = <NUM_LIT> ) : <EOL> s = Serializer ( current_app . config [ '<STR_LIT>' ] , expiration ) <EOL> return s . dumps ( { '<STR_LIT>' : self . id , '<STR_LIT>' : new_email } ) <EOL> def change_email ( self , token ) : <EOL> s = Serializer ( current_app . config [ '<STR_LIT>' ] ) <EOL> try : <EOL> data = s . loads ( token ) <EOL> except : <EOL> return False <EOL> if data . get ( '<STR_LIT>' ) != self . id : <EOL> return False <EOL> new_email = data . get ( '<STR_LIT>' ) <EOL> if new_email is None : <EOL> return False <EOL> if self . query . filter_by ( email = new_email ) . first ( ) is not None : <EOL> return False <EOL> self . email = new_email <EOL> db . session . add ( self ) <EOL> return True <EOL> def can ( self , permissions ) : <EOL> return self . role is not None and ( self . role . permissions & permissions ) == permissions <EOL> def is_administrator ( self ) : <EOL> return self . can ( Permission . ADMINISTER ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . username <EOL> class AnonymousUser ( AnonymousUserMixin ) : <EOL> def can ( self , permissions ) : <EOL> return False <EOL> def is_administrator ( self ) : <EOL> return False <EOL> login_manager . anonymous_user = AnonymousUser <EOL> @ login_manager . user_loader <EOL> def load_user ( user_id ) : <EOL> return User . query . get ( int ( user_id ) ) </s>
