Esempio n. 1
0
def update_from_csv(db, csv):
    '''Update table using values from CSV. Slower than 'insert_csv' but raises
    no error if primary key already exists (just updates values).'''
    table = pd.read_csv(csv)
    pks = create_pks(table)
    counter = ProgressCounter(len(table))
    modified_counter = 0
    added_counter = 0

    for row_i, row in table.iterrows():
        code = pks.iloc[row_i]
        row_model = db.session.query(Execucao).filter_by(code=code).first()
        new_row = prepare_row(code, row)
        date = datetime.datetime.strptime(new_row['data']['datafinal'],
                                          '%Y-%m-%d')
        if row_model:
            modified = {}

            # Check if state was modified
            if row_model.state != new_row['state'].decode('utf8'):
                modified['state'] = (row_model.state, new_row['state'])
                row_model.state = new_row['state']

            # Check if a field in data was modified
            for key, new_value in new_row['data'].items():
                old_value = row_model.data.get(key)

                # Avoids confusion caused by new_value not been unicode
                if type(new_value) is str:
                    new_value = new_value.decode('utf8')
                    new_row['data'][key] = new_value

                # Avoids data that changed from 0 to None
                if (old_value or new_value) and (old_value != new_value):
                    modified[key] = (old_value, new_value)

            # Avoids registering row as modified if only datafinal changend
            if len(modified) == 1 and 'datafinal' in modified:
                modified = {}

            if modified:
                db.session.add(
                    History(event='modified',
                            code=code,
                            date=date,
                            data=modified))
                modified_counter += 1

            # Updates DB data even if only 'datafinal' changed
            row_model.data = new_row['data']
        else:
            db.session.add(
                History(event='created', code=code, date=date, data=new_row))
            db.session.add(Execucao(**new_row))
            added_counter += 1
        counter.update()
    counter.end()
    db.session.commit()
    print('Added/Modified/Total: %s/%s/%s' %
          (added_counter, modified_counter, len(table)))
def update_from_csv(db, csv):
    '''Update table using values from CSV. Slower than 'insert_csv' but raises
    no error if primary key already exists (just updates values).'''
    table = pd.read_csv(csv)
    pks = create_pks(table)
    counter = ProgressCounter(len(table))
    for row_i, row in table.iterrows():
        code = pks.iloc[row_i]
        row_model = db.session.query(Execucao).filter_by(code=code).first()
        new_row = prepare_row(code, row)
        if row_model:
            for key, new_value in new_row.iteritems():
                setattr(row_model, key, new_value)
                # old_value = getattr(row_model, key)
                # if old_value != new_value:
                #     print(key, old_value, new_value)
                #     setattr(row_model, key, new_value)
        else:
            db.session.add(Execucao(**new_row))
        counter.update()
    counter.end()
    db.session.commit()