Esempio n. 1
0
def user_csv_to_sql(session):
    with create_app().app_context():
        file_path = None
        try:
            file_path = get_csv_path_from_table_name(User)
            current_app.logger.info('Opening file : {}.'.format(file_path))
            data = load_data_from_csv(file_path)

            for attr in data:
                record = User(
                    **{
                        'username': attr[0].decode('utf-8'),
                        'email': attr[1].decode('utf-8'),
                        'password': attr[2].decode('utf-8'),
                        'longitude': float(attr[3]),
                        'latitude': float(attr[4])
                    })
                session.add(record)
            current_app.logger.info(
                'Rows inserted successfully in table {}.'.format(
                    table_name(User)))

        except (IndexError, ValueError) as err:
            raise FileFormatException(file_path=file_path, err=err)
        except OSError as err:
            if '.csv not found' in err.args[0]:
                raise FileNotFoundException(file_path=file_path, err=err)
Esempio n. 2
0
 def read_all_without_filters_dao(self, session):
     with create_app().app_context():
         try:
             objects = session.query(self.__table).all()
             if not objects:
                 err = EmptyTableException(table=self.__table)
                 current_app.logger.warning(err.serialize)
                 raise err
         except EmptyTableException as err:
             raise err
         return objects
Esempio n. 3
0
import unittest

from flask import current_app
from flask_script import Manager

from cafe import create_app, db
from cafe.database.database import get_session
from cafe.exceptions.file.file_exception import FileException
from cafe.utils.csv_utils import user_csv_to_sql

app = create_app()
manager = Manager(app)


@manager.command
def recreate_database():
    current_app.logger.info("Recreates database...")
    db.drop_all()
    db.create_all()
    populate_database()
    db.session.commit()


@manager.command
def populate_database():
    """Populates the database with csv files into stuart/csv/*.csv"""
    current_app.logger.info("Populating the database.")
    session = get_session()
    try:
        user_csv_to_sql(session)
        session.commit()