Beispiel #1
0
def load_corps(csv_filepath: str = 'corp_nums/corps_to_load.csv'):
    """Load corps in given csv file from oracle into postgres."""
    global ROWCOUNT
    with open(csv_filepath, 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        with FLASK_APP.app_context():
            for row in reader:
                corp_num = row['CORP_NUM']
                print('loading: ', corp_num)
                added = False
                ROWCOUNT += 1
                try:
                    legal_type = Business.LegalTypes.COOP.value
                    if corp_num[:2] != Business.LegalTypes.COOP.value:
                        legal_type = Business.LegalTypes.BCOMP.value
                        corp_num = 'BC' + corp_num[-7:]
                    business = Business.find_by_identifier(corp_num)
                    if business:
                        added = True
                        print(
                            '-> business info already exists -- skipping corp load'
                        )
                    else:
                        try:
                            # get current company info
                            business_current_info = {}
                            for info_type in BUSINESS_MODEL_INFO_TYPES[
                                    legal_type]:
                                business_current_info[
                                    info_type] = get_oracle_info(
                                        corp_num=corp_num,
                                        legal_type=legal_type,
                                        info_type=info_type)
                                if business_current_info[info_type].get(
                                        'failed', False):
                                    raise Exception(
                                        f'could not load {info_type}')

                        except requests.exceptions.Timeout:
                            FAILED_CORPS.append(corp_num)
                            print(
                                'colin_api request timed out getting corporation details.'
                            )
                            continue

                        except Exception as err:
                            print(f'exception: {err}')
                            print(
                                f'skipping load for {corp_num}, exception occurred getting company info'
                            )
                            continue

                        uow = versioning_manager.unit_of_work(db.session)
                        transaction = uow.create_transaction(db.session)
                        try:
                            # add BC prefix to non coop identifiers
                            if legal_type != Business.LegalTypes.COOP.value:
                                business_current_info['business']['business']['identifier'] = 'BC' + \
                                    business_current_info['business']['business']['identifier']

                            # add company to postgres db
                            business = create_business(
                                business_current_info['business'])
                            add_business_offices(
                                business, business_current_info['office'])
                            add_business_directors(
                                business, business_current_info['parties'])
                            if legal_type == Business.LegalTypes.BCOMP.value:
                                add_business_shares(
                                    business,
                                    business_current_info['sharestructure'])
                                add_business_resolutions(
                                    business,
                                    business_current_info['resolutions'])
                                add_business_aliases(
                                    business, business_current_info['aliases'])
                            filing = Filing()
                            filing.filing_json = {
                                'filing': {
                                    'header': {
                                        'name': 'lear_epoch'
                                    },
                                    'business': business.json()
                                }
                            }
                            filing._filing_type = 'lear_epoch'
                            filing.source = Filing.Source.COLIN.value
                            filing.transaction_id = transaction.id
                            business.filings.append(filing)
                            business.save()
                            added = True
                            NEW_CORPS.append(corp_num)
                        except Exception as err:
                            print(err)
                            print(f'skipping {corp_num} missing info')
                            FAILED_CORPS.append(corp_num)

                    if added and history_needed(business=business):
                        load_historic_filings(corp_num=corp_num,
                                              business=business,
                                              legal_type=legal_type)
                    else:
                        print(
                            '-> historic filings not needed - skipping history load'
                        )
                except Exception as err:
                    print(err)
                    exit(-1)
Beispiel #2
0
                    try:
                        business = create_business(db, business_json)
                        add_business_addresses(business, offices_json)
                        add_business_directors(business, directors_json)
                        db.session.add(business)
                        filing = Filing()
                        # filing.filing_date = datetime.datetime.utcnow
                        filing.filing_json = {
                            'filing': {
                                'header': {
                                    'name': 'lear_epoch'
                                }
                            }
                        }
                        filing._filing_type = 'lear_epoch'
                        filing.transaction_id = transaction.id
                        db.session.add(filing)
                        db.session.commit()

                        # assign filing to business (now that business record has been comitted and id exists)
                        filing.business_id = business.id
                        db.session.add(filing)
                        db.session.commit()
                        added = True
                        NEW_COOPS.append(row["CORP_NUM"])
                    except Exception as err:
                        print(err)
                        print(f'skipping {row["CORP_NUM"]} missing info')
                        FAILED_COOPS.append(row['CORP_NUM'])
                else:
                    added = True