<s> import datetime <EOL> import hashlib <EOL> import random <EOL> import string <EOL> from elixir import Unicode , Integer , Field , DateTime <EOL> from elixir import ManyToOne , ManyToMany , OneToOne , OneToMany <EOL> from elixir import using_options <EOL> from sqlalchemy import and_ , func <EOL> from sqlalchemy . orm import aliased <EOL> from sqlalchemy . dialects . mysql import VARCHAR <EOL> from sqlalchemy . ext . associationproxy import AssociationProxy <EOL> from nagare import database <EOL> from kansha . models import Entity <EOL> class DataToken ( Entity ) : <EOL> using_options ( tablename = '<STR_LIT>' ) <EOL> token = Field ( Unicode ( <NUM_LIT:255> ) , unique = True , nullable = False , primary_key = True ) <EOL> username = Field ( Unicode ( <NUM_LIT:255> ) , nullable = True ) <EOL> date = Field ( DateTime , nullable = False ) <EOL> action = Field ( Unicode ( <NUM_LIT:255> ) , nullable = True ) <EOL> board = ManyToOne ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def delete_by_username ( cls , username , action ) : <EOL> token = cls . get_by ( username = username , action = action ) <EOL> if token : <EOL> token . delete ( ) <EOL> @ classmethod <EOL> def new ( cls , token , username , action ) : <EOL> obj = cls ( <EOL> token = token , <EOL> username = username , <EOL> action = action , <EOL> date = datetime . datetime . now ( ) <EOL> ) <EOL> return obj <EOL> class DataBoardMember ( Entity ) : <EOL> using_options ( tablename = '<STR_LIT>' ) <EOL> board = ManyToOne ( '<STR_LIT>' , primary_key = True , ondelete = '<STR_LIT>' ) <EOL> member = ManyToOne ( '<STR_LIT>' , primary_key = True , ondelete = '<STR_LIT>' , colname = [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> notify = Field ( Integer , default = lambda : <NUM_LIT:1> ) <EOL> class DataBoardManager ( Entity ) : <EOL> using_options ( tablename = '<STR_LIT>' ) <EOL> board = ManyToOne ( '<STR_LIT>' , primary_key = True , ondelete = '<STR_LIT>' ) <EOL> member = ManyToOne ( '<STR_LIT>' , primary_key = True , ondelete = '<STR_LIT>' , colname = [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class DataUser ( Entity ) : <EOL> """<STR_LIT>""" <EOL> using_options ( tablename = '<STR_LIT:user>' ) <EOL> username = Field ( <EOL> VARCHAR ( <NUM_LIT:255> , binary = True ) , unique = True , <EOL> primary_key = True , nullable = False ) <EOL> source = Field ( Unicode ( <NUM_LIT:255> ) , nullable = False , primary_key = True ) <EOL> fullname = Field ( Unicode ( <NUM_LIT:255> ) , nullable = False ) <EOL> email = Field ( Unicode ( <NUM_LIT:255> ) , nullable = True ) <EOL> picture = Field ( Unicode ( <NUM_LIT:255> ) , nullable = True ) <EOL> language = Field ( Unicode ( <NUM_LIT:255> ) , default = u"<STR_LIT>" , nullable = True ) <EOL> email_to_confirm = Field ( Unicode ( <NUM_LIT:255> ) ) <EOL> _salt = Field ( Unicode ( <NUM_LIT:255> ) , colname = '<STR_LIT>' , nullable = False ) <EOL> _password = Field ( Unicode ( <NUM_LIT:255> ) , colname = '<STR_LIT:password>' , nullable = True ) <EOL> registration_date = Field ( DateTime , nullable = False ) <EOL> last_login = Field ( DateTime , nullable = True ) <EOL> board_members = OneToMany ( '<STR_LIT>' ) <EOL> boards = AssociationProxy ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> creator = lambda board : DataBoardMember ( board = board ) ) <EOL> board_managers = OneToMany ( '<STR_LIT>' ) <EOL> managed_boards = AssociationProxy ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> creator = lambda board : DataBoardManager ( board = board ) ) <EOL> last_board = OneToOne ( '<STR_LIT>' , inverse = '<STR_LIT>' ) <EOL> cards = ManyToMany ( '<STR_LIT>' , inverse = '<STR_LIT>' , lazy = '<STR_LIT>' ) <EOL> history = OneToMany ( '<STR_LIT>' ) <EOL> def __init__ ( self , username , password , fullname , email , <EOL> source = u'<STR_LIT>' , picture = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> super ( DataUser , self ) . __init__ ( username = username , <EOL> fullname = fullname , <EOL> email = None , <EOL> email_to_confirm = email , <EOL> source = source , <EOL> picture = picture , <EOL> registration_date = datetime . datetime . utcnow ( ) , ** kw ) <EOL> if source == "<STR_LIT>" : <EOL> self . change_password ( password ) <EOL> else : <EOL> self . change_password ( '<STR_LIT>' ) <EOL> self . email_to_confirm = None <EOL> @ property <EOL> def id ( self ) : <EOL> return self . username <EOL> def update ( self , fullname , email , picture = None ) : <EOL> self . fullname = fullname <EOL> if email : <EOL> self . email = email <EOL> self . picture = picture <EOL> def check_password ( self , clear_password ) : <EOL> """<STR_LIT>""" <EOL> encrypted_password = self . _encrypt_password ( self . _salt , clear_password ) <EOL> return encrypted_password == self . _password <EOL> def change_password ( self , clear_password ) : <EOL> """<STR_LIT>""" <EOL> self . _salt = self . _create_random_salt ( ) <EOL> self . _password = self . _encrypt_password ( self . _salt , clear_password ) <EOL> def set_email_to_confirm ( self , email_to_confirm ) : <EOL> if email_to_confirm : <EOL> self . email_to_confirm = email_to_confirm <EOL> def is_validated ( self ) : <EOL> return self . email_to_confirm is None <EOL> def confirm_email ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . email_to_confirm is None : <EOL> return <EOL> self . email = self . email_to_confirm <EOL> self . email_to_confirm = None <EOL> def add_board ( self , board , role = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> boards = set ( dbm . board for dbm in self . board_members ) <EOL> if board not in boards : <EOL> self . board_members . append ( DataBoardMember ( board = board ) ) <EOL> if role == "<STR_LIT>" and board not in self . managed_boards : <EOL> self . managed_boards . append ( board ) <EOL> def get_picture ( self ) : <EOL> return self . picture <EOL> @ classmethod <EOL> def get_confirmed_users ( cls ) : <EOL> return cls . query . filter ( cls . email is not None ) <EOL> @ staticmethod <EOL> def _create_random_salt ( length = <NUM_LIT:32> ) : <EOL> allowed_chars = string . ascii_letters + string . digits <EOL> return u'<STR_LIT>' . join ( random . choice ( allowed_chars ) for _ in range ( length ) ) <EOL> @ staticmethod <EOL> def _encrypt_password ( salt , password ) : <EOL> secret = "<STR_LIT>" <EOL> secret_salt = hashlib . sha512 ( secret + salt ) . hexdigest ( ) <EOL> utf8_password = password . encode ( '<STR_LIT:utf-8>' ) <EOL> return unicode ( hashlib . sha512 ( secret_salt + utf8_password ) . hexdigest ( ) ) <EOL> @ classmethod <EOL> def get_unconfirmed_users ( cls , before_date = None ) : <EOL> q = cls . query . filter ( cls . email is None ) <EOL> if before_date : <EOL> q = q . filter ( cls . registration_date < before_date ) <EOL> return q <EOL> @ classmethod <EOL> def get_by_username ( cls , username ) : <EOL> return cls . get_by ( username = username ) <EOL> @ classmethod <EOL> def get_by_email ( cls , email ) : <EOL> return cls . get_by ( email = email ) <EOL> @ classmethod <EOL> def search ( cls , value ) : <EOL> return cls . query . filter ( cls . fullname . ilike ( '<STR_LIT:%>' + value + '<STR_LIT:%>' ) | cls . email . ilike ( '<STR_LIT:%>' + value + '<STR_LIT:%>' ) ) <EOL> def best_friends ( self , exclude_list = ( ) , size = None ) : <EOL> from kansha . board . models import DataBoard <EOL> cls = self . __class__ <EOL> bm2 = aliased ( DataBoardMember ) <EOL> cnt = func . count ( DataBoardMember . board_id ) <EOL> query = database . session . query ( cls , cnt ) <EOL> query = query . join ( ( DataBoardMember , and_ ( DataBoardMember . user_source == cls . source , <EOL> DataBoardMember . user_username == cls . username ) ) ) <EOL> query = query . join ( <EOL> ( DataBoard , DataBoard . id == DataBoardMember . board_id ) ) <EOL> query = query . join ( ( bm2 , bm2 . board_id == DataBoard . id ) ) <EOL> query = query . filter ( bm2 . member == self ) <EOL> if exclude_list : <EOL> query = query . filter ( ~ cls . email . in_ ( exclude_list ) ) <EOL> query = query . group_by ( cls ) <EOL> query = query . order_by ( cnt . desc ( ) , cls . fullname ) <EOL> if size : <EOL> query = query . limit ( size ) <EOL> return [ res [ <NUM_LIT:0> ] for res in query ] </s>
