<s> from __future__ import unicode_literals <EOL> import logging <EOL> import balanced <EOL> import wac <EOL> from sqlalchemy . orm import relationship , backref <EOL> from random import randint <EOL> from sqlalchemy . orm . exc import NoResultFound <EOL> from werkzeug . security import generate_password_hash , check_password_hash <EOL> from rentmybike . db import Session <EOL> from rentmybike . db . tables import users <EOL> from rentmybike . models import Base <EOL> logger = logging . getLogger ( __name__ ) <EOL> class User ( Base ) : <EOL> __table__ = users <EOL> def __init__ ( self , password = None , ** kwargs ) : <EOL> if password : <EOL> self . password_hash = generate_password_hash ( password ) <EOL> self . has_password = True <EOL> super ( User , self ) . __init__ ( ** kwargs ) <EOL> Session . add ( self ) <EOL> def check_password ( self , password ) : <EOL> return check_password_hash ( self . password_hash , password ) <EOL> def set_password ( self , password ) : <EOL> self . password_hash = generate_password_hash ( password ) <EOL> @ staticmethod <EOL> def login ( email , password ) : <EOL> try : <EOL> user = User . query . filter ( User . email == email ) . one ( ) <EOL> except NoResultFound : <EOL> pass <EOL> else : <EOL> if not user . has_password : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if user . check_password ( password ) : <EOL> return user <EOL> return None <EOL> @ property <EOL> def is_authenticated ( self ) : <EOL> return True if self . has_password else False <EOL> @ property <EOL> def balanced_customer ( self ) : <EOL> if self . account_href : <EOL> return balanced . Customer . fetch ( self . account_href ) <EOL> @ staticmethod <EOL> def create_guest_user ( email , name = None , password = None ) : <EOL> try : <EOL> user = User . query . filter ( <EOL> User . email == email ) . one ( ) <EOL> except NoResultFound : <EOL> Session . flush ( ) <EOL> user = User ( email = email , name = name , <EOL> password = password ) <EOL> Session . add ( user ) <EOL> try : <EOL> Session . flush ( ) <EOL> except : <EOL> Session . rollback ( ) <EOL> Session . commit ( ) <EOL> Session . add ( user ) <EOL> Session . commit ( ) <EOL> return user <EOL> def create_balanced_customer ( self , card_href = None , <EOL> merchant_data = None ) : <EOL> if self . account_href : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if card_href : <EOL> account = self . _create_balanced_buyer ( card_href ) <EOL> else : <EOL> account = self . _create_balanced_merchant ( merchant_data ) <EOL> self . associate_balanced_customer ( account . href ) <EOL> if merchant_data : <EOL> self . add_merchant ( merchant_data ) <EOL> return account <EOL> def _create_balanced_buyer ( self , card_href ) : <EOL> marketplace = balanced . Marketplace . my_marketplace <EOL> account = balanced . Customer ( email = self . email , <EOL> name = self . name , source = card_href ) <EOL> account . save ( ) <EOL> return account <EOL> def _create_balanced_merchant ( self , merchant_data ) : <EOL> marketplace = balanced . Marketplace . my_marketplace <EOL> try : <EOL> account = balanced . Customer . query . filter ( email = self . email ) . one ( ) <EOL> except wac . NoResultFound as ex : <EOL> account = balanced . Customer ( email = self . email , name = self . name , <EOL> merchant = merchant_data ) . save ( ) <EOL> return account <EOL> def lookup_balanced_customer ( self ) : <EOL> if self . account_href : <EOL> return <EOL> try : <EOL> account = balanced . Customer . query . filter ( email = self . email ) . one ( ) <EOL> except wac . NoResultFound : <EOL> pass <EOL> else : <EOL> self . account_href = account . href <EOL> def associate_balanced_customer ( self , account_href = None ) : <EOL> """<STR_LIT>""" <EOL> if account_href : <EOL> balanced_email = balanced . Customer . fetch ( <EOL> account_href ) . email <EOL> else : <EOL> balanced_email = balanced . Customer . filter ( <EOL> email = self . email ) . one ( ) <EOL> if balanced_email != self . email : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if self . account_href and self . account_href != account_href : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . account_href = account_href <EOL> def add_card ( self , card_href ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> account = balanced . Customer . query . filter ( email = self . email ) . one ( ) <EOL> except wac . NoResultFound : <EOL> account = balanced . Customer ( <EOL> email = self . email , card_href = card_href , <EOL> name = self . name ) . save ( ) <EOL> else : <EOL> card = balanced . Card . fetch ( card_href ) <EOL> card . associate_to_customer ( account . href ) <EOL> return account <EOL> def add_merchant ( self , merchant_data ) : <EOL> address_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> customer_resource = self . balanced_customer <EOL> for field in merchant_data : <EOL> if field in address_fields : <EOL> customer_resource . address [ field ] = merchant_data [ field ] <EOL> else : <EOL> setattr ( customer_resource , field , merchant_data [ field ] ) <EOL> customer_resource . save ( ) <EOL> @ classmethod <EOL> def fetch_one_at_random ( cls ) : <EOL> user_query = User . query . filter ( ) <EOL> selector = randint ( <NUM_LIT:0> , ( user_query . count ( ) - <NUM_LIT:1> ) ) <EOL> owner = user_query [ selector ] <EOL> return owner </s>
