Ejemplo n.º 1
0
def import_data(filespec):
    db = Database('instance/database.sqlite').connect()
    category = Category(db)
    uom = Uom(db)
    item = Item(db)
    trx = Transaction(db)

    with open(filespec, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            print('Name: {}, Category: {}'.format(row['item_name'],
                                                  row['category_name']))
            #import pdb;pdb.set_trace()
            #create a Category if not exists
            cats = category.select_one(
                where='name = "{}"'.format(row['category_name']))
            if not cats:
                cats = category.new()
                cats.name = row["category_name"]
                category.save(cats)
            cat_id = cats.id
            #create UOM if not exists
            uom_name = row['uom'].strip()
            if uom_name == '':
                uom_name = "Ea."

            uoms = uom.select_one(where='name = "{}"'.format(uom_name))
            if not uoms:
                uoms = uom.new()
                uoms.name = uom_name
                uom.save(uoms)

            items = item.select_one(
                where='name = "{}"'.format(row['item_name'], ))
            if not items:
                items = item.new()
                items.name = row['item_name']
                items.uom = uom_name
                items.cat_id = cat_id
                item.save(items)

            # Create a transaction record
            trxs = trx.new()
            trx.update(trxs, row)
            trxs.created = datetime.strptime(row['created'], '%Y-%m-%d')
            trxs.item_id = items.id
            trx.save(trxs)

        try:
            db.commit()
        except Exception as e:
            db.rollback()
            print(str(e))
Ejemplo n.º 2
0
def get_db(filespec=None):
    """Return a connection to the database.
    If the db path does not exist, create it and initialize the db"""

    if not filespec:
        filespec = shotglass.get_site_config()['DATABASE_PATH']

    # This is probobly a good place to change the
    # filespec if you want to use a different database
    # for the current request.

    # test the path, if not found, create it
    initialize = shotglass.make_db_path(filespec)

    g.db = Database(filespec).connect()
    if initialize:
        initalize_all_tables(g.db)

    return g.db
Ejemplo n.º 3
0
def get_db(filespec=None):
    """Return a connection to the database.

    If the db path does not exist, create it and initialize the db"""

    if not filespec:
        filespec = shotglass.get_site_config()['DATABASE_PATH']

    # This is probobly a good place to change the
    # filespec if you want to use a different database
    # for the current request.

    # test the path, if not found, try to create it
    if shotglass.make_db_path(filespec):
        g.db = Database(filespec).connect()
        initalize_all_tables(g.db)

        return g.db
    else:
        # was unable to create a path to the database
        raise IOError(
            "Unable to create path to () in app.get_db".format(filespec))
Ejemplo n.º 4
0
#print(sys.path)
sys.path.append('')  ##get import to look in the working dir.
import os
import pytest
#from app import app
from shotglass2.takeabeltof.sqlite_backup import SqliteBackup
import time

# create an empty sqlite file for testing
from shotglass2.takeabeltof.database import Database
script_root = os.path.dirname(os.path.realpath(__file__))
backup_dir_name = 'backup_test'
backup_dir = os.path.join(script_root, backup_dir_name)
backup_file_name = 'test_db.sqlite'
file_name = os.path.join(script_root, backup_file_name)
conn = Database(file_name).connect()


def test_backup():
    bac = SqliteBackup(file_name,
                       frequency=0,
                       backup_dir=backup_dir,
                       force=True)
    bac.backup()

    assert bac.result_code == 0
    assert "Success" in bac.result
    assert bac.fatal_error == False
    assert os.path.isdir(backup_dir)
    file_list = os.listdir(backup_dir)
    assert len(file_list) > 0