def create_social_group(social_group_size, round_number, date_of_visit):
    field_worker = random.choice(hdss['field_workers'])
    cursor = open_hds_connection.cursor()
    sub_village = query_db_one(cursor, "SELECT extId FROM locationhierarchy "
                                       "WHERE level_uuid = 'hierarchyLevelId5' ORDER BY RAND() LIMIT 1")['extId']
    #for now assume one location per social group
    location_index = len(hdss['social_groups']) + 1
    location_id = sub_village + str(location_index).zfill(6)
    coordinates = sample_coordinates()
    submission.submit_location_registration(sub_village, field_worker['ext_id'], location_id, 'Location name',
                                            'Ten cell leader', 'RUR', coordinates,
                                            create_end_time(date_of_visit), aggregate_url)
    visit_id = location_id + round_number.zfill(3)
    ind_id = ''
    sg_id = location_id + '00'
    #first create the social group head
    id_of_head = location_id + '1'.zfill(3)
    last_name = create_last_name()
    gender_of_head = sample_gender()
    submission.submit_baseline_individual(location_id, visit_id, field_worker['ext_id'], id_of_head, 'UNK', 'UNK',
                                          create_first_name(), create_first_name(), last_name, gender_of_head,
                                          str(create_date(sample_age(min_age_head_of_social_group), date_of_visit)),
                                          '0', str(date_of_visit), 'ORIGIN', 'REASON', 'MARITAL_CHANGE',
                                          create_end_time(date_of_visit), aggregate_url)
    #create a social group
    submission.submit_social_group_registration(sg_id, id_of_head, field_worker['ext_id'], last_name, "FAM",
                                                create_end_time(date_of_visit), aggregate_url)
    social_group = {'sg_id': sg_id, 'individuals': [], 'locations': []}
    social_group['locations'].append({'location_id': location_id, 'coordinates': coordinates})
    social_group['individuals'].append({'ind_id': id_of_head, 'gender': gender_of_head, 'last_seen': date_of_visit,
                                        'status': 'present'})
    #and make the head a member
    submission.submit_membership(id_of_head, sg_id, field_worker['ext_id'], '1', str(date_of_visit),
                                 create_end_time(date_of_visit), aggregate_url)
    for i in range(2, social_group_size):
        ind_id = location_id + str(i).zfill(3)
        gender = sample_gender()
        age = sample_age()
        submission.submit_baseline_individual(location_id, visit_id, field_worker['ext_id'], ind_id, 'UNK', 'UNK',
                                              create_first_name(), create_first_name(), create_last_name(), gender,
                                              str(create_date(age, date_of_visit)), '0',
                                              str(date_of_visit), 'ORIGIN', 'REASON', 'MARITAL_CHANGE',
                                              create_end_time(date_of_visit), aggregate_url)
        #create memberships here, 2-9 for relationship
        submission.submit_membership(ind_id, sg_id, field_worker['ext_id'], str(random.randint(2, 9)),
                                     str(date_of_visit), create_end_time(date_of_visit), aggregate_url)
        social_group['individuals'].append({'ind_id': ind_id, 'gender': gender, 'last_seen': date_of_visit,
                                            'status': 'present'})
    #then another loop for relationship, use code 2 for marriages.
    #submission.submit_relationship()
    #TODO: for now, just take individual 2 and marry it to the household head (if opposite sexes and old enough)
        if i == 2 and gender != gender_of_head and age > min_age_marriage:
            submission.submit_relationship(id_of_head, ind_id, field_worker['ext_id'], '2', str(date_of_visit),
                                           create_end_time(date_of_visit), aggregate_url)
    submission.submit_visit_registration(visit_id, field_worker['ext_id'], location_id, round_number,
                                         str(date_of_visit), ind_id, '1', '0', coordinates,
                                         create_end_time(date_of_visit), aggregate_url)
    hdss['social_groups'].append(social_group)
def visit_social_group(social_group, round_number, date_of_visit):
    field_worker = random.choice(hdss['field_workers'])
    #TODO: only one location per social group for now
    location_id = social_group['locations'][0]['location_id']
    visit_id = location_id + round_number.zfill(3)
    submission.submit_visit_registration(visit_id, field_worker['ext_id'], location_id, round_number,
                                         str(date_of_visit), social_group['individuals'][0]['ind_id'], '1', '0',
                                         social_group['locations'][0]['coordinates'],
                                         create_end_time(date_of_visit), aggregate_url)
    for individual in social_group['individuals']:
        #TODO: here decide for each individual if/which event occurred. For now just test some submissions.
        if individual['status'] == 'present' and random.random() < 0.5:
            submission.submit_death_registration(individual['ind_id'], field_worker['ext_id'], individual['gender'],
                                                 '1', 'VILLAGE', '1', visit_id, 'CAUSE_OF_DEATH', str(date_of_visit),
                                                 'OTHER', 'OTHERPLACE', create_end_time(date_of_visit), aggregate_url)
            individual['status'] == 'dead'
        if individual['status'] == 'present' and random.random() < 0.5:
            submission.submit_out_migration_registration(individual['ind_id'], field_worker['ext_id'], visit_id,
                                                         str(date_of_visit), 'DESTINATION', 'MARITAL_CHANGE', 'REC',
                                                         create_end_time(date_of_visit), aggregate_url)