<s> import datetime <EOL> from decimal import Decimal as D <EOL> import random <EOL> import string <EOL> import sys <EOL> from faker import Factory <EOL> from psycopg2 import IntegrityError <EOL> from liberapay . billing . exchanges import record_exchange_result , _record_transfer_result <EOL> from liberapay . constants import MAX_TIP , MIN_TIP <EOL> from liberapay . models . exchange_route import ExchangeRoute <EOL> from liberapay . models . participant import Participant <EOL> from liberapay . models import community <EOL> faker = Factory . create ( ) <EOL> def _fake_thing ( db , tablename , ** kw ) : <EOL> cols , vals = zip ( * kw . items ( ) ) <EOL> cols = '<STR_LIT:U+002CU+0020>' . join ( cols ) <EOL> placeholders = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT:%s>' ] * len ( vals ) ) <EOL> return db . one ( """<STR_LIT>""" . format ( tablename , cols , placeholders ) , vals ) <EOL> def fake_text_id ( size = <NUM_LIT:6> , chars = string . ascii_lowercase + string . digits ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( random . choice ( chars ) for x in range ( size ) ) <EOL> def fake_sentence ( start = <NUM_LIT:1> , stop = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> return faker . sentence ( random . randrange ( start , stop ) ) <EOL> def fake_participant ( db , kind = None , is_admin = False ) : <EOL> """<STR_LIT>""" <EOL> username = faker . first_name ( ) + fake_text_id ( <NUM_LIT:3> ) <EOL> kind = kind or random . choice ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> is_a_person = kind in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> _fake_thing ( db <EOL> , "<STR_LIT>" <EOL> , username = username <EOL> , password = None if not is_a_person else '<STR_LIT:x>' <EOL> , email = username + '<STR_LIT>' <EOL> , is_admin = is_admin <EOL> , balance = <NUM_LIT:0> <EOL> , hide_giving = is_a_person and ( random . randrange ( <NUM_LIT:5> ) == <NUM_LIT:0> ) <EOL> , hide_receiving = is_a_person and ( random . randrange ( <NUM_LIT:5> ) == <NUM_LIT:0> ) <EOL> , status = '<STR_LIT>' <EOL> , join_time = faker . date_time_this_year ( ) <EOL> , kind = kind <EOL> , mangopay_user_id = username <EOL> , mangopay_wallet_id = '<STR_LIT>' <EOL> ) <EOL> except IntegrityError : <EOL> return fake_participant ( db , is_admin ) <EOL> return Participant . from_username ( username ) <EOL> def fake_community ( db , creator ) : <EOL> """<STR_LIT>""" <EOL> name = community . normalize ( faker . city ( ) ) <EOL> c = creator . create_community ( name ) <EOL> creator . update_community_status ( '<STR_LIT>' , True , c . id ) <EOL> return c <EOL> def fake_tip_amount ( ) : <EOL> amount = ( D ( random . random ( ) ) * ( MAX_TIP - MIN_TIP ) + MIN_TIP ) <EOL> decimal_amount = D ( amount ) . quantize ( D ( '<STR_LIT>' ) ) <EOL> while decimal_amount == D ( '<STR_LIT>' ) : <EOL> decimal_amount = fake_tip_amount ( ) <EOL> return decimal_amount <EOL> def fake_tip ( db , tipper , tippee ) : <EOL> """<STR_LIT>""" <EOL> return _fake_thing ( <EOL> db , <EOL> "<STR_LIT>" , <EOL> ctime = faker . date_time_this_year ( ) , <EOL> mtime = faker . date_time_this_month ( ) , <EOL> tipper = tipper . id , <EOL> tippee = tippee . id , <EOL> amount = fake_tip_amount ( ) , <EOL> ) <EOL> def fake_elsewhere ( db , participant , platform ) : <EOL> """<STR_LIT>""" <EOL> _fake_thing ( <EOL> db , <EOL> "<STR_LIT>" , <EOL> platform = platform , <EOL> user_id = fake_text_id ( ) , <EOL> user_name = participant . id , <EOL> participant = participant . id , <EOL> extra_info = None , <EOL> ) <EOL> def fake_transfer ( db , tipper , tippee , amount , timestamp ) : <EOL> status = '<STR_LIT>' <EOL> t = _fake_thing ( <EOL> db , <EOL> "<STR_LIT>" , <EOL> timestamp = timestamp , <EOL> tipper = tipper . id , <EOL> tippee = tippee . id , <EOL> amount = amount , <EOL> context = '<STR_LIT>' , <EOL> status = status , <EOL> ) <EOL> _record_transfer_result ( db , t . id , status ) <EOL> return t <EOL> def fake_exchange ( db , participant , amount , fee , vat , timestamp ) : <EOL> route = ExchangeRoute . from_network ( participant , '<STR_LIT>' ) <EOL> if not route : <EOL> route = _fake_thing ( <EOL> db , <EOL> "<STR_LIT>" , <EOL> participant = participant . id , <EOL> network = '<STR_LIT>' , <EOL> address = '<STR_LIT>' , <EOL> error = '<STR_LIT>' , <EOL> one_off = False , <EOL> ) <EOL> e = _fake_thing ( <EOL> db , <EOL> "<STR_LIT>" , <EOL> timestamp = timestamp , <EOL> participant = participant . id , <EOL> amount = amount , <EOL> fee = fee , <EOL> vat = vat , <EOL> status = '<STR_LIT>' , <EOL> route = route . id , <EOL> ) <EOL> record_exchange_result ( db , e . id , '<STR_LIT>' , '<STR_LIT>' , participant ) <EOL> return e <EOL> def populate_db ( website , num_participants = <NUM_LIT:100> , num_tips = <NUM_LIT:200> , num_teams = <NUM_LIT:5> , num_transfers = <NUM_LIT> , num_communities = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> db = website . db <EOL> print ( "<STR_LIT>" ) <EOL> participants = [ ] <EOL> for i in range ( num_participants ) : <EOL> participants . append ( fake_participant ( db ) ) <EOL> print ( "<STR_LIT>" ) <EOL> teams = [ ] <EOL> for i in range ( num_teams ) : <EOL> team = fake_participant ( db , kind = "<STR_LIT>" ) <EOL> members = random . sample ( participants , random . randint ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> for p in members : <EOL> team . add_member ( p ) <EOL> teams . append ( team ) <EOL> participants . extend ( teams ) <EOL> print ( "<STR_LIT>" ) <EOL> platforms = [ p . name for p in website . platforms ] <EOL> for p in participants : <EOL> num_elsewheres = random . randint ( <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> for platform_name in random . sample ( platforms , num_elsewheres ) : <EOL> fake_elsewhere ( db , p , platform_name ) <EOL> print ( "<STR_LIT>" ) <EOL> for i in range ( num_communities ) : <EOL> creator = random . sample ( participants , <NUM_LIT:1> ) <EOL> community = fake_community ( db , creator [ <NUM_LIT:0> ] ) <EOL> members = random . sample ( participants , random . randint ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> for p in members : <EOL> p . update_community_status ( '<STR_LIT>' , True , community . id ) <EOL> print ( "<STR_LIT>" ) <EOL> tips = [ ] <EOL> for i in range ( num_tips ) : <EOL> tipper , tippee = random . sample ( participants , <NUM_LIT:2> ) <EOL> tips . append ( fake_tip ( db , tipper , tippee ) ) <EOL> transfers = [ ] <EOL> for i in range ( num_transfers ) : <EOL> tipper , tippee = random . sample ( participants , <NUM_LIT:2> ) <EOL> while tipper . kind in ( '<STR_LIT>' , '<STR_LIT>' ) or tippee . kind in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> tipper , tippee = random . sample ( participants , <NUM_LIT:2> ) <EOL> sys . stdout . write ( "<STR_LIT>" % ( i + <NUM_LIT:1> , num_transfers ) ) <EOL> sys . stdout . flush ( ) <EOL> amount = fake_tip_amount ( ) <EOL> ts = faker . date_time_this_year ( ) <EOL> fake_exchange ( db , tipper , amount , <NUM_LIT:0> , <NUM_LIT:0> , ( ts - datetime . timedelta ( days = <NUM_LIT:1> ) ) ) <EOL> transfers . append ( fake_transfer ( db , tipper , tippee , amount , ts ) ) <EOL> print ( "<STR_LIT>" ) <EOL> min_date = min ( min ( x . ctime for x in tips ) , min ( x . timestamp for x in transfers ) ) <EOL> max_date = max ( max ( x . ctime for x in tips ) , max ( x . timestamp for x in transfers ) ) <EOL> payday_counter = <NUM_LIT:1> <EOL> date = min_date <EOL> paydays_total = ( max_date - min_date ) . days / <NUM_LIT:7> + <NUM_LIT:1> <EOL> while date < max_date : <EOL> sys . stdout . write ( "<STR_LIT>" % ( payday_counter , paydays_total ) ) <EOL> sys . stdout . flush ( ) <EOL> payday_counter += <NUM_LIT:1> <EOL> end_date = date + datetime . timedelta ( days = <NUM_LIT:7> ) <EOL> week_tips = [ x for x in tips if date < x . ctime < end_date ] <EOL> week_transfers = [ x for x in transfers if date < x . timestamp < end_date ] <EOL> week_participants = [ x for x in participants if x . join_time < end_date ] <EOL> actives = set ( ) <EOL> tippers = set ( ) <EOL> for xfers in week_tips , week_transfers : <EOL> actives . update ( x . tipper for x in xfers ) <EOL> actives . update ( x . tippee for x in xfers ) <EOL> tippers . update ( x . tipper for x in xfers ) <EOL> payday = { <EOL> '<STR_LIT>' : date , <EOL> '<STR_LIT>' : end_date , <EOL> '<STR_LIT>' : len ( week_tips ) , <EOL> '<STR_LIT>' : len ( week_transfers ) , <EOL> '<STR_LIT>' : len ( week_participants ) , <EOL> '<STR_LIT>' : len ( tippers ) , <EOL> '<STR_LIT>' : len ( actives ) , <EOL> '<STR_LIT>' : sum ( x . amount for x in week_transfers ) <EOL> } <EOL> _fake_thing ( db , "<STR_LIT>" , ** payday ) <EOL> date = end_date <EOL> print ( "<STR_LIT>" ) <EOL> def main ( ) : <EOL> from liberapay . main import website <EOL> populate_db ( website ) <EOL> website . db . self_check ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
