<s> import datetime as dt <EOL> from flask_login import UserMixin <EOL> from comport . extensions import bcrypt <EOL> from comport . database import ( Column , db , Model , ReferenceCol , relationship , SurrogatePK ) <EOL> class Role ( SurrogatePK , Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> name = Column ( db . String ( <NUM_LIT> ) , unique = False , nullable = False ) <EOL> user_id = ReferenceCol ( '<STR_LIT>' , nullable = True ) <EOL> user = relationship ( '<STR_LIT>' , backref = '<STR_LIT>' ) <EOL> def __init__ ( self , name , ** kwargs ) : <EOL> db . Model . __init__ ( self , name = name , ** kwargs ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( name = self . name ) <EOL> class Invite_Code ( SurrogatePK , Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( db . Integer , primary_key = True , index = True ) <EOL> department_id = Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> code = Column ( db . String ( <NUM_LIT> ) , unique = True , nullable = False ) <EOL> used = Column ( db . Boolean ( ) , default = False ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> db . Model . __init__ ( self , ** kwargs ) <EOL> class User ( UserMixin , SurrogatePK , Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( db . Integer , primary_key = True , index = True ) <EOL> username = Column ( db . String ( <NUM_LIT> ) , unique = True , nullable = False ) <EOL> email = Column ( db . String ( <NUM_LIT> ) , unique = False , nullable = False ) <EOL> password = Column ( db . String ( <NUM_LIT> ) , nullable = True ) <EOL> created_at = Column ( db . DateTime , nullable = False , default = dt . datetime . utcnow ) <EOL> first_name = Column ( db . String ( <NUM_LIT:30> ) , nullable = True ) <EOL> last_name = Column ( db . String ( <NUM_LIT:30> ) , nullable = True ) <EOL> active = Column ( db . Boolean ( ) , default = False ) <EOL> department_id = Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) , nullable = True ) <EOL> type = Column ( db . String ( <NUM_LIT:50> ) ) <EOL> password_reset_uuid = Column ( db . String ( <NUM_LIT> ) , unique = True , nullable = True ) <EOL> __mapper_args__ = { <EOL> '<STR_LIT>' : type <EOL> } <EOL> def __init__ ( self , username , email , is_admin = False , password = None , ** kwargs ) : <EOL> db . Model . __init__ ( self , username = username , email = email , ** kwargs ) <EOL> if is_admin : <EOL> admin_role = Role ( name = '<STR_LIT>' ) <EOL> admin_role . save ( ) <EOL> self . roles . append ( admin_role ) <EOL> if password : <EOL> self . set_password ( password ) <EOL> else : <EOL> self . password = None <EOL> def set_password ( self , password ) : <EOL> self . password = bcrypt . generate_password_hash ( password ) <EOL> def check_password ( self , value ) : <EOL> return bcrypt . check_password_hash ( self . password , value ) <EOL> def is_admin ( self ) : <EOL> def names ( role ) : <EOL> return role . name <EOL> return "<STR_LIT>" in map ( names , self . roles ) <EOL> def has_department ( self , department_id ) : <EOL> def department_ids ( department ) : <EOL> return department . id <EOL> return department_id in map ( department_ids , self . departments ) <EOL> def first_department ( self ) : <EOL> if len ( self . departments ) > <NUM_LIT:0> : <EOL> return self . departments [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> @ property <EOL> def full_name ( self ) : <EOL> return "<STR_LIT>" . format ( self . first_name , self . last_name ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( username = self . username ) </s>
