def fillDBRandomly(seed, workgroups): global default_pwd, fakenames if seed: random.seed(seed) now = datetime.datetime.now() mi = month_info(now.date()) # read in the fakenames names = {} for l in file(fakenames, 'r'): fname, lname = l.strip().split() names[fname] = lname namelist = sorted(list(names.keys())) # 20% of the people are applicants members = [] for l in namelist[int(len(namelist) * 0.2):]: m = Applicant(fname=l, lname=names[l]) m.email = "*****@*****.**" % (l) m.household_size = random.randint(1, 5) m.telnr = "06" + unicode(random.randint(10000000, 100000000)) DBSession.add(m) # randomly select a number of workgroups m can be a member of DBSession.flush() # and the rest are members for l in namelist[:int(len(namelist) * 0.8)]: prefix = random.choice([u"de", u"van", u"voor", u"van den", u"te"]) m = Member(fname=l, prefix=prefix, lname=names[l]) m.mem_email = "*****@*****.**" % (l) m.mem_enc_pwd = md5crypt(default_pwd, default_pwd) m.mem_mobile = "06" + unicode(random.randint(10000000, 100000000)) m.household_size = random.randint(1, 5) m.mem_membership_paid = True if random.random() < 0.01: m.mem_membership_paid = False m.mem_active = True if random.random() < 0.05: m.mem_active = False m.mem_street = random.choice(namelist) + "street" m.mem_house = random.randint(1, 200) m.mem_postcode = "1000AB" m.city = "Amsterdam" DBSession.add(m) # randomly select a number of workgroups m can be a member of for wg in random.sample(workgroups, random.randint(1, len(workgroups))): wg.members.append(m) # randomly select if m is a leader of the workgroup: only 10 % of all members are leaders if random.random() < 0.1: wg.leaders.append(m) members.append(m) DBSession.flush() months = [(mi.prev_month, mi.prev_year), (now.month, now.year), (mi.next_month, mi.next_year)] # next step: create 50 new shifts in each wg with random state/description for wg in workgroups: for i in range(50): task = random.choice([ "clean", "buy", "feed", "fill", "write", "do", "play", "sleep" ]) month = random.choice(months) s = Shift(wg.id, task, month[1], month[0], day=random.randint(1, 30)) s.state = random.choice(shift_states) if not s.state == 'open': s.member = random.choice(wg.members) DBSession.add(s) DBSession.flush()
def fillDBRandomly(seed, workgroups, existing_members): random.seed(seed) now = datetime.datetime.now() mi = month_info(now.date()) # read in the fakenames names = {} for l in open(fakenamesname, 'r').readlines(): fname, lname = l.strip().split() names[fname] = lname namelist = sorted(list(names.keys())) # 20% of the people are applicants members = existing_members for l in namelist[int(len(namelist) * 0.2):]: m = Applicant(fname=l, lname=names[l]) m.email = "*****@*****.**" % (l) m.household_size = random.randint(1, 5) m.telnr = "06" + str(random.randint(10000000, 100000000)) DBSession.add(m) # randomly select a number of workgroups m can be a member of DBSession.flush() # and the rest are members for l in namelist[:int(len(namelist) * 0.8)]: prefix = random.choice([u"de", u"van", u"voor", u"van den", u"te"]) m = Member(fname=l, prefix=prefix, lname=names[l]) m.mem_email = "*****@*****.**" % (l) m.mem_enc_pwd = md5_crypt.hash(default_pwd) m.mem_mobile = "06" + str(random.randint(10000000, 100000000)) m.mem_household_size = random.randint(1, 5) m.mem_membership_paid = True if random.random() < 0.01: m.mem_membership_paid = False m.mem_active = True if random.random() < 0.05: m.mem_active = False m.mem_street = random.choice(namelist) + "street" m.mem_house = random.randint(1, 200) m.mem_postcode = "1000AB" m.mem_city = "Amsterdam" DBSession.add(m) # randomly select a number of workgroups m can be a member of for wg in random.sample(workgroups, random.randint(1, len(workgroups))): wg.members.append(m) # randomly select if m is a leader of the workgroup: only 10 % of all members are leaders if random.random() < 0.1: wg.leaders.append(m) members.append(m) DBSession.flush() months = [(mi.prev_month, mi.prev_year), (now.month, now.year), (mi.next_month, mi.next_year)] # next step: create 50 new shifts in each wg with random state/description for wg in workgroups: for i in range(50): task = random.choice([ "clean", "buy", "feed", "fill", "write", "do", "play", "sleep" ]) month = random.choice(months) s = Shift(wg.id, task, month[1], month[0], day=random.randint(1, 30)) s.state = random.choice(shift_states) if not s.state == 'open': s.member = random.choice(wg.members) DBSession.add(s) DBSession.flush() # Finally: create 20 transactions per member ttype = DBSession.query(TransactionType).filter( TransactionType.name == 'Order Charge').first() orders = DBSession.query(Order).all() for m in members: for i in range(20): month = random.choice(months) t = Transaction(ttype_id=ttype.id, amount=random.random() * 150, date=datetime.datetime(month[1], month[0], random.randint(1, 28)), mem_id=m.mem_id) t.ttype = ttype t.member = m if ttype.pos_neg == 'neg': t.amount *= -1 if ttype.name == 'Order Charge': o = random.choice(orders) t.ord_no = o.id t.order = o t.validate() DBSession.add(t) DBSession.flush()