def wrapper(*args, **kwargs):
        language = kwargs.get("language", "en")

        path = kwargs.pop("database_path", None)
        if not path:
            path = CONTENT_DATABASE_PATH.format(
                channel=kwargs.get("channel", CHANNEL),
                language=language
            )

        db = SqliteDatabase(path, pragmas=settings.CONTENT_DB_SQLITE_PRAGMAS)

        kwargs["db"] = db

        db.connect()

        # This should contain all models in the database to make them available to the wrapped function

        with Using(db, [Item, AssessmentItem]):

            try:

                output = function(*args, **kwargs)

            except DoesNotExist:
                output = None

            except OperationalError:
                logging.error("No content database file found")
                raise
        db.close()

        return output
Example #2
0
class PWDatabase(object):

    __proxy = None

    @staticmethod
    def DBProxy():
        if not PWDatabase.__proxy:
            PWDatabase.__proxy = Proxy()
        return PWDatabase.__proxy

    _db = None

    def __init__(self, path):
        try:
            self._db = SqliteDatabase(path, check_same_thread=False)
            PWDatabase.DBProxy().initialize(self._db)
            self.startup()
        except Exception as e:
            logger.error(
                "database file does not exist, or incorrect permissions")

    def close(self):
        self._db.close()
        self._db = None

    def startup(self):
        self._db.connect()

    @property
    def DB(self):
        return self._db
Example #3
0
 def delete_artwork_in_db(self, name_of_artwork):
     database = SqliteDatabase(self.db)
     database.connect()
     delete = Artwork.delete().where(
         Artwork.name_of_artwork == name_of_artwork).execute()
     database.close()
     return delete
def add_customer(customer_id, name, lastname, home_address, phone_number,
                 email_address, status, credit_limit):
    """ This function will add a new customer to the customers.db database """
    logger.info('In add_customer().')

    database = SqliteDatabase('customers.db')
    try:
        database.connect()
        database.execute_sql('PRAGMA foreign_keys = ON;')
        with database.transaction():
            new_customer = Customer.create(customer_id=customer_id,
                                           name=name,
                                           lastname=lastname,
                                           home_address=home_address,
                                           phone_number=phone_number,
                                           email_address=email_address,
                                           status=status,
                                           credit_limit=credit_limit)

            new_customer.save()
            logger.info('Add new customer to Customer database successfully.')
    except Exception as e:
        logger.info(f'Error creating {name} customer record.')
        logger.info(e)

    finally:
        logger.info('database closes.')
        database.close()
Example #5
0
def loader():
    database = SqliteDatabase(dbname)

    names = 0
    donations = 1

    try:
        database.connect()
        database.execute_sql('PRAGMA foreign_keys = ON;')
        for entry in raw_input:
            with database.transaction():
                new_donor = Donor.create(name=entry[names])
                # new_donor.save()
                new_donation = Donation.create(gift_id=uuid.uuid4(),
                                               value=entry[donations],
                                               donated_by=entry[names],
                                               gift_num=1)
                # new_donation.save()
                logging.info('Database add successful')

    # except Exception as e:
    #     logging.info(e)

    finally:
        logging.info('database closes')
        database.close()
Example #6
0
    def wrapper(*args, **kwargs):
        language = kwargs.get("language", "en")

        path = kwargs.pop("database_path", None)
        if not path:
            path = CONTENT_DATABASE_PATH.format(channel=kwargs.get(
                "channel", CHANNEL),
                                                language=language)

        db = SqliteDatabase(path, pragmas=settings.CONTENT_DB_SQLITE_PRAGMAS)

        kwargs["db"] = db

        db.connect()

        # This should contain all models in the database to make them available to the wrapped function

        with Using(db, [Item, AssessmentItem]):

            try:

                output = function(*args, **kwargs)

            except DoesNotExist:
                output = None

            except OperationalError as e:
                logging.error(
                    "Content DB error: Perhaps content database file found? "
                    "Exception: {e}".format(e=str(e)))
                raise
        db.close()

        return output
Example #7
0
 def db_change_artwork_availability(self, name_of_artwork, availability):
     database = SqliteDatabase(self.db)
     database.connect()
     updated_artwork = Artwork.update(available=availability).where(
         Artwork.name_of_artwork == name_of_artwork).execute()
     database.close()
     return updated_artwork
 def empty(self, *args, **options):
     """
     Creates an empty content database for the Khan channel. This ensures
     that an empty content database exists in the default distribution and
     for tests.
     
     Especially useful for creating an *EMPTY TEMPLATE*
     
     retrievecontentpack empty en --template
     """
     lang = args[1]
     if not options.get('template', False):
         content_db_path = topic_settings.CONTENT_DATABASE_PATH.format(
             channel=topic_settings.CHANNEL,
             language=lang,
         )
     else:
         content_db_path = topic_settings.CONTENT_DATABASE_TEMPLATE_PATH.format(
             channel=topic_settings.CHANNEL,
             language=lang,
         )
     if os.path.exists(content_db_path):
         if options.get("force", False):
             os.unlink(content_db_path)
         else:
             raise CommandError(
                 "Content database already exists: {}".format(
                     content_db_path))
     db = SqliteDatabase(content_db_path)
     db.connect()
     db.create_table(Item, safe=True)
     db.create_table(AssessmentItem, safe=True)
     db.close()
     self.complete(
         _("Saved empty content database in {}.").format(content_db_path))
Example #9
0
class Database(object):
    def __init__(self, database=None):

        self.database = database

        if self.database is None:
            self.load_database()

        self.Model = self.get_model_class()

    def load_database(self):
        self.database = SqliteDatabase(settings.SQLITE_FILEPATH)

    def get_model_class(self):
        class BaseModel(Model):
            class Meta:
                database = self.database

        return BaseModel

    def connect_db(self):
        self.database.connect()

    def close_db(self):
        if not self.database.is_closed():
            self.database.close()
Example #10
0
class PWDatabase:

    __proxy = None

    @staticmethod
    def DBProxy():
        if not PWDatabase.__proxy:
            PWDatabase.__proxy = Proxy()
        return PWDatabase.__proxy

    _db = None

    def __init__(self, path):
        try:
            self._db = SqliteDatabase(path, check_same_thread=False)
            PWDatabase.DBProxy().initialize(self._db)
            self.startup()
        except Exception as e:
            logger.error("database file does not exist, or incorrect permissions")

    def close(self):
        self._db.close()
        self._db = None

    def startup(self):
        self._db.connect()

    @property
    def DB(self):
        return self._db
    def test_writes_db_to_archive(self):
        with tempfile.NamedTemporaryFile() as zffobj:
            zf = zipfile.ZipFile(zffobj, "w")

            with tempfile.NamedTemporaryFile() as dbfobj:
                db = SqliteDatabase(dbfobj.name)
                db.connect()
                with Using(db, [Item]):
                    Item.create_table()
                    item = Item(id="test", title="test",
                                description="test", available=False,
                                slug="srug", kind=NodeType.video,
                                path="/test/test")
                    item.save()
                db.close()

                save_db(db, zf)

            zf.close()

            # reopen the db from the zip, see if our object was saved
            with tempfile.NamedTemporaryFile() as f:
                # we should only have one file in the zipfile, the db. Assume
                # that the first file is the db.
                zf = zipfile.ZipFile(zffobj.name)
                dbfobj = zf.open(zf.infolist()[0])
                f.write(dbfobj.read())
                f.seek(0)

                db = SqliteDatabase(f.name)

                with Using(db, [Item]):
                    Item.get(title="test")
Example #12
0
class DatabaseConnector:
    """Base class for establishing connection with database, creating and dropping tables"""
    def __init__(self, database_name='./task-manager.db'):
        self.database_name = database_name
        self.database = SqliteDatabase(database_name)
        self.connected = False
        database_proxy.initialize(self.database)
        self.create_tables()

    def create_tables(self):
        if not self.connected:
            self.database.connect()
            self.connected = True
        Category.create_table(True)
        Notification.create_table(True)
        Task.create_table(True)
        TaskPlan.create_table(True)
        UsersReadTasks.create_table(True)
        UsersWriteTasks.create_table(True)

    def drop_tables(self):
        self.database.drop_tables([
            Task, UsersReadTasks, UsersWriteTasks, Category, Notification,
            TaskPlan
        ])
        self.database.close()
        self.connected = False
class PurgeOldLogMessages(Thread):
    """Periodically remove old records from DB to keep size down."""
    def __init__(self):
        super().__init__()
        self.daemon = True
        self.db = SqliteDatabase(os.path.join(config.DB_FOLDER, 'log.db'),
                                 pragmas={'journal_mode': 'wal'})
        self.LogMessage = init_log_model(self.db)
        self.db.close()

        self.log_ttl = 259200  # 3 days
        self.check_interval = 1800  # 30 mins

    def run(self):
        while True:
            old_messages = self.LogMessage.select().where(
                (time() - self.LogMessage.created_at) > self.log_ttl)

            count = 0
            for message in old_messages:
                deleted = False
                count += 1
                # Wait for DB write lock from other threads.
                while not deleted:
                    try:
                        message.delete_instance()
                        deleted = True
                    except OperationalError:
                        sleep(1)

            if count != 0:
                logger.info('Deleted {} log messages'.format(count))
            self.db.close()
            sleep(self.check_interval)
def create_database(db):
    if not os.path.isfile(db):
        database = SqliteDatabase(db)
        database.connect()
        database.execute_sql('PRAGMA foreign_keys = ON;')

        database.create_tables([Customer])
        database.close()
Example #15
0
 def add_new_artist_to_db(self, name, email):
     database = SqliteDatabase(self.db)
     database.connect()
     # Add the new artist to the db
     new_artist = Artist(name=name, email=email)
     new_artist.save()
     database.close()
     return new_artist
Example #16
0
 def search_db_for_artwork_by_artist(self, name):
     database = SqliteDatabase(self.db)
     database.connect()
     # Locate all artwork with the corresponding name
     artwork_list = Artwork.select().where(Artwork.artist == name)
     # Display all artwork
     database.close()
     return artwork_list
Example #17
0
def report():
    database = SqliteDatabase('mailroom.db')
    database.connect()
    print('Last Name____Title__Total Donations__Number of Donations')
    donor_table = database.execute_sql('select * from donor;')
    for donor in donor_table:
        print(donor)
    database.close()
Example #18
0
def initsys():
    if not os.path.exists(PHOTO_PATH):
        os.mkdir(PHOTO_PATH)

    if not os.path.exists(DB_PATH):
        db = SqliteDatabase(os.path.join(APP_PATH, 'fanhao.db'))
        db.create_table(models.fanhao)
        db.close()
Example #19
0
class ValveDriverTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        fakesleep.monkey_patch()
        SetTestMode()

    @classmethod
    def tearDownClass(cls):
        fakesleep.monkey_restore()

    def setUp(self):
        self.test_db = SqliteDatabase(':memory:')
        self.test_db.bind(MODELS)
        self.test_db.connect()
        self.test_db.create_tables(MODELS)

    def tearDown(self):
        self.test_db.drop_tables(MODELS)
        self.test_db.close()

    def test_valve_driver(self):
        valve_output_1 = Output.create(number=2)
        valve_1 = Valve.create(number=1,
                               name='valve 1',
                               delay=30,
                               output=valve_output_1)

        SetUpTestInjections(output_controller=mock.Mock(OutputController))
        driver_1 = ValveDriver(valve_1)

        self.assertEqual(valve_1.id, driver_1.id)
        self.assertEqual(0, driver_1.percentage)
        self.assertEqual(0, driver_1._desired_percentage)
        self.assertFalse(driver_1.is_open)
        self.assertFalse(driver_1.in_transition)

        driver_1.set(50)
        self.assertEqual(50, driver_1._desired_percentage)
        driver_1.close()
        self.assertEqual(0, driver_1._desired_percentage)
        driver_1.open()
        self.assertEqual(100, driver_1._desired_percentage)
        self.assertTrue(driver_1.will_open)
        driver_1.steer_output()
        driver_1._output_controller.set_output_status.assert_called_once()
        self.assertFalse(driver_1.will_open)
        self.assertEqual(100, driver_1.percentage)
        self.assertFalse(driver_1.is_open)
        self.assertTrue(driver_1.in_transition)

        time.sleep(20)
        self.assertFalse(driver_1.is_open)
        self.assertTrue(driver_1.in_transition)

        time.sleep(15)
        self.assertTrue(driver_1.is_open)
        self.assertFalse(driver_1.in_transition)
 def test_db_closure(*args, **kwargs):
     test_db = SqliteDatabase(":memory:")
     with test_db.bind_ctx(dbs):
         test_db.create_tables(dbs)
         try:
             func(*args, **kwargs)
         finally:
             test_db.drop_tables(dbs)
             test_db.close()
Example #21
0
class PumpDriverTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        SetTestMode()

    def setUp(self):
        self.test_db = SqliteDatabase(':memory:')
        self.test_db.bind(MODELS)
        self.test_db.connect()
        self.test_db.create_tables(MODELS)

    def tearDown(self):
        self.test_db.drop_tables(MODELS)
        self.test_db.close()

    def test_pump_driver(self):
        output = Output.create(number=0)
        pump = Pump.create(number=1,
                           name='pump',
                           output=output)

        SetUpTestInjections(output_controller=mock.Mock(OutputController))
        driver = PumpDriver(pump)
        self.assertIsNone(driver.state)
        self.assertFalse(driver.error)
        self.assertEqual(pump.id, driver.id)

        driver.turn_on()
        self.assertTrue(driver.state)
        self.assertFalse(driver.error)

        driver.turn_off()
        self.assertFalse(driver.state)
        self.assertFalse(driver.error)

        driver._output_controller.set_output_status.side_effect = RuntimeError()
        with self.assertRaises(RuntimeError):
            driver.turn_on()
        self.assertFalse(driver.state)
        self.assertTrue(driver.error)

        driver._output_controller.set_output_status.side_effect = None
        driver.turn_on()
        self.assertTrue(driver.state)
        self.assertFalse(driver.error)

        driver._output_controller.set_output_status.side_effect = RuntimeError()
        with self.assertRaises(RuntimeError):
            driver.turn_off()
        self.assertTrue(driver.state)
        self.assertTrue(driver.error)

        driver._output_controller.set_output_status.side_effect = None
        driver.turn_off()
        self.assertFalse(driver.state)
        self.assertFalse(driver.error)
Example #22
0
class TestManage(unittest.TestCase):
    def setUp(self):
        self.db = SqliteDatabase('peewee.db')
        self.db.connect()

    def tearDown(self):
        self.db.close()

    def testCreate(self):
        self.db.create_tables(ALL_MODELS, safe=True)
Example #23
0
 def create_data_base(self):
     database = SqliteDatabase(self.db)
     # Create the database connection to a db file
     database.connect()
     tables = database.get_tables()
     # If there are no tables in the db then add this base data
     if len(tables) == 0:
         database.create_tables([Artist, Artwork])
         #add_test_data()
     database.close()
def init_db():
    """Initialize the database and create tables."""
    from app.models.customer import Customer
    from app.models.event import Event
    from app.models.ticket import Ticket
    print("Initializing database...")
    db = SqliteDatabase(Config.get('SQLITE_DATABASE'))
    db.connect()
    db.create_tables([Customer, Event, Ticket], safe=True)
    db.close()
Example #25
0
 def add_new_artwork_to_db(self, name, name_of_artwork, price, available):
     database = SqliteDatabase(self.db)
     database.connect()
     new_artwork = Artwork(artist=name,
                           name_of_artwork=name_of_artwork,
                           price=price,
                           available=available)
     new_artwork.save()
     database.close()
     return new_artwork
Example #26
0
def db_in_memory():
    """Binds all models to in-memory SQLite and creates all tables`"""
    db = SqliteDatabase(':memory:')
    db.bind(ALL_MODELS)
    db.connect()
    db.create_tables(ALL_MODELS)

    yield db

    db.drop_tables(ALL_MODELS)
    db.close()
Example #27
0
def exportSQLite32Excel(dbname, tblname, file):
    try:
        db = SqliteDatabase(dbname)
        sql =  "select * from {0}".format(tblname,)
        result = db.execute_sql(sql)
        utils.writeExcel(result, file, 6, 2)
        utils.writeCreateTime(file, 2, 4)
    except Exception as e:
        logging.debug(e)
    finally:
        db.close() 
 def wrapped_test_function(*a, **k):
     db = SqliteDatabase(":memory:",
                         pragmas={'foreign_keys': 1},
                         autoconnect=False)
     db.bind(models.REGISTRY, bind_refs=True, bind_backrefs=True)
     db.connect()
     db.create_tables(models.REGISTRY)
     try:
         return func(*a, **k)
     finally:
         db.drop_tables(models.REGISTRY)
         db.close()
class TestBase(TestCase):
    def setUp(self):
        self.db = SqliteDatabase(":memory:",
                                 pragmas={'foreign_keys': 1},
                                 autoconnect=False)
        self.db.bind(models.REGISTRY, bind_refs=False, bind_backrefs=False)
        self.db.connect()
        self.db.create_tables(models.REGISTRY)

    def tearDown(self):
        #[cls.delete().where(True).execute() for cls in models.REGISTRY]
        self.db.drop_tables(models.REGISTRY)
        self.db.close()
Example #30
0
def initialDates(dbname, startDate, endDate):
    try:
        db = SqliteDatabase(dbname)
        days = (endDate - startDate).days
        db.execute_sql("drop table if exists t_date_temp")
        db.execute_sql("create table t_date_temp (mydate text, week text, weekday text)")
        db.execute_sql("insert into t_date_temp values( ?, ?, ? )", (startDate.strftime('%Y-%m-%d'),startDate.isoweekday(),''))
        for i in range(days):
            db.execute_sql("insert into t_date_temp  select date(max(mydate), '+1 day'),  strftime('%w', date(max(mydate), '+1 day')), '' from t_date_temp")
    except Exception as e:
        logging.debug(e)
    finally:
        db.close()
class IndexServerUnitTests(unittest.TestCase):
    """Index server unit and integration tests."""

    # pylint: disable=invalid-name
    def setUp(self):
        """Setup the database with in memory sqlite."""
        self._db = SqliteDatabase('file:cachedb?mode=memory&cache=shared')
        for model in [UniqueIndex]:
            model.bind(self._db, bind_refs=False, bind_backrefs=False)
        self._db.connect()
        self._db.create_tables([UniqueIndex])

    def tearDown(self):
        """Tear down the database."""
        self._db.drop_tables([UniqueIndex])
        self._db.close()
        self._db = None
    # pylint: enable=invalid-name

    def test_index_update(self):
        """Test return and update of unique index."""
        test_object = UniqueIndex.create(idid='file', index=892)
        self.assertEqual(test_object.idid, 'file')

        index, index_range = update_index(10, 'file')
        self.assertEqual(index, 892)
        self.assertEqual(index_range, 10)

        index, index_range = update_index(10, 'file')
        self.assertEqual(index, 902)
        self.assertEqual(index_range, 10)

        index, index_range = update_index(10, 'new')
        self.assertEqual(index, 1)
        self.assertEqual(index_range, 10)

        index, index_range = update_index(10, 'new')
        self.assertEqual(index, 11)
        self.assertEqual(index_range, 10)

        index, index_range = update_index(None, 'new')
        self.assertEqual(index, -1)
        self.assertEqual(index_range, -1)

        index, index_range = update_index(2, None)
        self.assertEqual(index, -1)
        self.assertEqual(index_range, -1)

        index, index_range = update_index(-5, 'new')
        self.assertEqual(index, -1)
        self.assertEqual(index_range, -1)
Example #32
0
def bundle_language_pack(dest, nodes, frontend_catalog, backend_catalog,
                         metadata, assessment_items, assessment_files,
                         subtitles, html_exercise_path):

    # make sure dest's parent directories exist
    pathlib.Path(dest).parent.mkdir(parents=True, exist_ok=True)

    with zipfile.ZipFile(dest,
                         "w") as zf, tempfile.NamedTemporaryFile() as dbf:
        db = SqliteDatabase(dbf.name)
        db.connect()

        nodes = convert_dicts_to_models(nodes)
        nodes = mark_exercises_as_available(nodes)
        nodes = list(save_models(nodes, db))  # we have to make sure to force
        # the evaluation of each
        # save_models call, in order to
        # avoid nesting them.
        nodes = list(populate_parent_foreign_keys(nodes))
        list(save_models(nodes, db))

        nodes = recurse_availability_up_tree(nodes, db)
        list(save_models(nodes, db))

        assessment_items = convert_dicts_to_assessment_items(assessment_items)
        list(save_assessment_items(assessment_items, db))

        db.close()
        dbf.flush()

        save_catalog(frontend_catalog, zf, "frontend.mo")
        save_catalog(backend_catalog, zf, "backend.mo")
        # save_subtitles(subtitle_path, zf)

        try:  # sometimes we have no html exercises
            save_html_exercises(html_exercise_path, zf)
        except FileNotFoundError:
            logging.warning("No html exercises found; skipping.")

        save_db(db, zf)

        save_metadata(zf, metadata)

        for file_path in assessment_files:
            save_assessment_file(file_path, zf)
        write_assessment_version(metadata, zf)

        for subtitle_path in subtitles:
            save_subtitle(subtitle_path, zf)

    return dest
Example #33
0
class BaseTestCase(unittest.TestCase):
    """Base class for testing DB."""
    def setUp(self):
        """Set up database."""
        super(BaseTestCase, self).setUp()
        self.conn = SqliteDatabase(":memory:")
        self.conn.bind(MODELS, bind_refs=False, bind_backrefs=False)
        self.conn.connect()
        self.conn.create_tables(MODELS)

    def tearDown(self):
        """Cleanup database."""
        self.conn.drop_tables(MODELS)
        self.conn.close()
Example #34
0
def qapp(tmpdir_factory):
    # DB is required to init QApplication. New DB used for every test.
    tmp_db = tmpdir_factory.mktemp('Vorta').join('settings.sqlite')
    mock_db = SqliteDatabase(str(tmp_db))
    vorta.store.connection.init_db(mock_db)

    from vorta.application import VortaApp
    VortaApp.set_borg_details_action = MagicMock()  # Can't use pytest-mock in session scope
    VortaApp.scheduler = MagicMock()

    qapp = VortaApp([])  # Only init QApplication once to avoid segfaults while testing.

    yield qapp
    mock_db.close()
    qapp.quit()
def bundle_language_pack(dest, nodes, frontend_catalog, backend_catalog, metadata, assessment_items, assessment_files, subtitles, html_exercise_path):

    # make sure dest's parent directories exist
    pathlib.Path(dest).parent.mkdir(parents=True, exist_ok=True)

    with zipfile.ZipFile(dest, "w") as zf, tempfile.NamedTemporaryFile() as dbf:
        db = SqliteDatabase(dbf.name)
        db.connect()

        nodes = convert_dicts_to_models(nodes)
        nodes = mark_exercises_as_available(nodes)
        nodes = list(save_models(nodes, db)) # we have to make sure to force
                                             # the evaluation of each
                                             # save_models call, in order to
                                             # avoid nesting them.
        nodes = list(populate_parent_foreign_keys(nodes))
        list(save_models(nodes, db))

        nodes = recurse_availability_up_tree(nodes, db)
        list(save_models(nodes, db))

        assessment_items = convert_dicts_to_assessment_items(assessment_items)
        list(save_assessment_items(assessment_items, db))

        db.close()
        dbf.flush()

        save_catalog(frontend_catalog, zf, "frontend.mo")
        save_catalog(backend_catalog, zf, "backend.mo")
        # save_subtitles(subtitle_path, zf)

        try:                    # sometimes we have no html exercises
            save_html_exercises(html_exercise_path, zf)
        except FileNotFoundError:
            logging.warning("No html exercises found; skipping.")

        save_db(db, zf)

        save_metadata(zf, metadata)

        for file_path in assessment_files:
            save_assessment_file(file_path, zf)
        write_assessment_version(metadata, zf)

        for subtitle_path in subtitles:
            save_subtitle(subtitle_path, zf)

    return dest
 def empty(self, *args, **options):
     """
     Creates an empty content database for the Khan channel. This ensures
     that an empty content database exists in the default distribution and
     for tests.
     
     Especially useful for creating an *EMPTY TEMPLATE*
     
     retrievecontentpack empty en --template
     """
     lang = args[1]
     if not options.get('template', False):
         content_db_path = topic_settings.CONTENT_DATABASE_PATH.format(
             channel=topic_settings.CHANNEL,
             language=lang,
         )
     else:
         content_db_path = topic_settings.CONTENT_DATABASE_TEMPLATE_PATH.format(
             channel=topic_settings.CHANNEL,
             language=lang,
         )
     if os.path.exists(content_db_path):
         if options.get("force", False):
             os.unlink(content_db_path)
         else:
             raise CommandError(
                 "Content database already exists: {}".format(
                     content_db_path
                 )
             )
     db = SqliteDatabase(
         content_db_path
     )
     db.connect()
     db.create_table(Item, safe=True)
     db.create_table(AssessmentItem, safe=True)
     db.close()
     self.complete(
         _("Saved empty content database in {}.").format(
             content_db_path
         )
     )
Example #37
0
def bundle_language_pack(dest, nodes, frontend_catalog, backend_catalog, metadata, assessment_items, assessment_files, subtitles):
    with zipfile.ZipFile(dest, "w") as zf, tempfile.NamedTemporaryFile() as dbf:
        db = SqliteDatabase(dbf.name)
        db.connect()

        nodes = convert_dicts_to_models(nodes)
        nodes = mark_exercises_as_available(nodes)
        nodes = list(save_models(nodes, db)) # we have to make sure to force
                                             # the evaluation of each
                                             # save_models call, in order to
                                             # avoid nesting them.
        nodes = list(populate_parent_foreign_keys(nodes))
        list(save_models(nodes, db))

        nodes = recurse_availability_up_tree(nodes, db)
        list(save_models(nodes, db))

        assessment_items = convert_dicts_to_assessment_items(assessment_items)
        list(save_assessment_items(assessment_items, db))

        db.close()
        dbf.flush()

        save_catalog(frontend_catalog, zf, "frontend.mo")
        save_catalog(backend_catalog, zf, "backend.mo")
        # save_subtitles(subtitle_path, zf)

        save_db(db, zf)

        save_metadata(zf, metadata)

        for file_path in assessment_files:
            save_assessment_file(file_path, zf)

        for subtitle_path in subtitles:
            save_subtitle(subtitle_path, zf)

    return dest
Example #38
0
class TestModel(unittest.TestCase):
    def setUp(self):
        self.db = SqliteDatabase('peewee.db')
        self.db.connect()

        self.db.create_tables([Device, Group, User, UserToGroup, Publication], safe=True)

        Device.delete().execute()
        Group.delete().execute()
        User.delete().execute()
        UserToGroup.delete().execute()
        Publication.delete().execute()

        self.user0 = User.create_user(name='user0name', username='******', password='******')
        self.user0.create_device(name='device0name', resource='device0resource', type='device0type', dev_id='device0id', reg_id='device0regid')

        self.user1 = User.create_user(name='user1name', username='******', password='******')
        self.user1.create_device(name='device1name', resource='device1resource', type='device1type', dev_id='device1id')

        self.group0 = Group.create(name='group0name', description='group0description', owner=self.user0)
        self.group0.add_user(user=self.user0)
        self.group0.add_user(user=self.user1)

        self.group1 = Group.create(name='group1name', description='group1description', owner=self.user0)
        self.group1.add_user(user=self.user0)

        self.group2 = Group.create(name='group2name', description='group2description', owner=self.user1)
        self.group2.add_user(user=self.user1)

        self.pub0 = Publication.create(user=self.user0, topic='pub0topic', description='pub0description', publish_group=self.group1, subscribe_group=self.group0)

    def tearDown(self):
        self.db.close()
        self.db = None

    def test_group_get(self):
        os = Group.select()
        self.assertEqual(len(os), 3)

        o = Group.select().where(Group.name == 'group0name').get()
        self.assertEqual(o, self.group0)

        o = Group.select().where(Group.id == self.group0.id).get()
        self.assertEqual(o, self.group0)
        self.assertTrue(o.owner_id)

    def test_group_memberships(self):
        self.assertTrue(self.group0.is_member(self.user0))
        self.assertTrue(self.group0.is_member(self.user1))

        self.assertTrue(self.group1.is_member(self.user0))
        self.assertFalse(self.group1.is_member(self.user1))

        self.assertFalse(self.group2.is_member(self.user0))
        self.assertTrue(self.group2.is_member(self.user1))

    def test_get_reg_ids_by_user_id(self):
        user_id = self.user0.id

        devices = Device.select(Device, User).join(User).where(User.id == user_id)
        self.assertEqual(len(devices), 1)

        reg_ids = [d.reg_id for d in devices]
        self.assertEqual(reg_ids, ['device0regid'])
Example #39
0
class TestBase(unittest.TestCase):
    def populate_database(self):
        self.db = SqliteDatabase('peewee.db')
        self.db.connect()

        self.db.create_tables(model.ALL_MODELS, safe=True)

        for m in model.ALL_MODELS:
            m.delete().execute()

        self.db.close()

        # Config
        release = model.Config.create(app_api_key=APP_API_KEY, messaging_api_key=MESSAGING_API_KEY)

        admin_user = model.User.create_user(name='Administrator', description='Administrator', email='*****@*****.**', username=TEST_USER, password=TEST_PASSWORD)

        # Groups
        admin = model.Group.create(name=TEST_USER, owner=admin_user)
        user = model.Group.create(name='user', owner=admin_user)
        guest = model.Group.create(name='guest', owner=admin_user)

        admin.add_user(admin_user)

        # Users
        model.Device.create(user=admin, name='d2', resource='work', type='computer', dev_id='a')

        chloe = model.User.create_user(name='Chloe', username='******', password=TEST_PASSWORD)
        d = chloe.create_device(name='d2', resource='work', type='computer', dev_id='a')
        chloe.create_device(name='d0', resource='home', type='phone', dev_id='b')
        chloe.create_device(name='d3', resource='home', type='laptop', dev_id='c')
        chloe.create_device(name='d1', resource='work', type='phone', dev_id='d')
        model.UserToGroup.create(user=chloe, group=guest)

        sunshine = model.User.create_user(name='Sunshine', username='******', password=TEST_PASSWORD)
        sunshine.create_device(name='d5', resource='work', type='phone', dev_id='e')
        model.UserToGroup.create(user=sunshine, group=user)
        model.UserToGroup.create(user=sunshine, group=guest)
        p = model.Publication.create(user=sunshine, topic='Life and Times of Sunshine', description='', publish_group=guest, subscribe_group=guest)
        model.Message.create(user=sunshine, to_publication=p, subject='First post!')
        model.Message.create(user=sunshine, to_publication=p, subject='Eating breakfast')
        model.Message.create(user=sunshine, to_publication=p, subject='Time for a nap')

        guinness = model.User.create_user(name='Guinness', username='******', password=TEST_PASSWORD)
        guinness.create_device(name='d7', resource='work', type='phone', dev_id='g')
        model.UserToGroup.create(user=guinness, group=guest)

        felix = model.User.create_user(name='Felix', username='******', password=TEST_PASSWORD)
        felix.create_device(name='d6', resource='work', type='phone', dev_id='f')
        model.UserToGroup.create(user=felix, group=guest)
        model.Subscription.create(user=felix, publication=p)
        model.Message.create(user=felix, to_publication=p, subject='boring...')
        model.Message.create(user=felix, to_user=sunshine, subject='hi sunshine')
        model.Message.create(user=felix, to_device=d, subject='hi chloe')
        model.Message.create(user=felix, to_user=chloe, subject='hi chloe again')

        ducky = model.User.create_user(name='Ducky', username='******', password=TEST_PASSWORD)
        ducky.create_device(name='d8', resource='work', type='phone', dev_id='h')
        model.UserToGroup.create(user=ducky, group=admin)
        model.UserToGroup.create(user=ducky, group=user)
        model.UserToGroup.create(user=ducky, group=guest)

    def setUp(self):
        self.app = app.test_client()
        self.populate_database()

    def request(self, method, url, auth=None, json_data=None, **kwargs):
        headers = kwargs.get('headers', {})

        if auth:
            # Add the auth header if credentials are specified.
            headers['Authorization'] = 'Basic %s' % b64encode(bytes(auth[0] + ':' + auth[1], 'utf-8')).decode('ascii')

        if json:
            headers['Content-Type'] = 'application/json'
            kwargs['data'] = json.dumps(obj=json_data)
            #print(kwargs['data'])

        kwargs['headers'] = headers
        #print(kwargs['headers'])

        return self.app.open(url, method=method, **kwargs)
Example #40
0
class Pomito(object):
    """Controls the application lifetime.

    Responsibilities:
        - Read and initialize the configuration
        - Choose the run mode
        - Handover execution to UI plugin
    """

    def __init__(self, config=None, database=None, message_dispatcher=None):
        """Create a Pomito object.

        Arguments:
            config   Configuration  Path to the configuration file
            database peewee.SqliteDatabase database to use for tasks etc.
            message_dispatcher MessageDispatcher message dispatcher instance
        """
        from pomito import pomodoro

        self._config = config
        self._database = database
        self._message_dispatcher = message_dispatcher
        self._threads = {}
        self._hooks = []

        if self._message_dispatcher is None:
            self._message_dispatcher = MessageDispatcher()
        if self._config is None:
            self._config_file = os.path.join(CONFIG_DIR, "config.ini")
            self._config = Configuration(self._config_file)
        self._config.load()

        # Pomodoro service instance. Order of initializations are important
        self.pomodoro_service = pomodoro.Pomodoro(self)

        # Default plugins
        pomito.plugins.initialize(self.pomodoro_service)
        self.ui_plugin = pomito.plugins.get_plugin(self._config.ui_plugin)
        self.task_plugin = pomito.plugins.get_plugin(self._config.task_plugin)

        # Add the plugins to threads list
        self._threads['task_plugin'] = threading.Thread(target=self.task_plugin)

        # Default hooks
        from pomito.hooks import activity
        self._hooks.append(activity.ActivityHook(self.pomodoro_service))
        return

    def initialize(self):
        """Initialize configuration, database and starts worker threads."""
        os.makedirs(DATA_DIR, exist_ok=True)

        database_path = os.path.join(DATA_DIR, "pomito.db")
        if self._database is None:
            self._database = SqliteDatabase(None)
            self._database.init(database_path)
        self._database.connect()

        # Initialize the plugins
        self.ui_plugin.initialize()
        self.task_plugin.initialize()

        # Initialize the hooks
        for hook in self._hooks:
            hook.initialize()
        return

    def run(self):
        """Start the application."""
        if not self._validate_state():
            logger.critical("Pomito.Run: Invalid state. Exiting.")
            return
        self.initialize()
        self._message_dispatcher.start()
        self.ui_plugin.run()
        self.exit()

    def exit(self):
        """Clean up and save any configuration data. Prepare for exiting the application."""
        if self._message_dispatcher.is_alive():
            self._message_dispatcher.stop()
            self._message_dispatcher.join()
        for hook in self._hooks:
            hook.close()
        if self._database is not None:
            self._database.close()

    def get_db(self):
        """Get the database object.

        Returns:
            database peewee.SqliteDatabase object

        """
        return self._database

    def get_configuration(self):
        return self._config

    def queue_signal(self, message):
        self._message_dispatcher.queue_message(message)

    def _validate_state(self):
        """Validates configuration, plugins."""
        import pomito.plugins

        _retval = True

        if not issubclass(type(self.ui_plugin), pomito.plugins.ui.UIPlugin):
            logger.error("Invalid UIPlugin object = {0}".format(self.ui_plugin))
            _retval = False

        if not issubclass(type(self.task_plugin), pomito.plugins.task.TaskPlugin):
            logger.error("Invalid TaskPlugin object = {0}".format(self.task_plugin))
            _retval = False

        return _retval
Example #41
0
import sqlite3
from peewee import Model, SqliteDatabase, DateTimeField, ForeignKeyField, FloatField, CharField
from datetime import datetime


DB = SqliteDatabase('finance-manager.db')
DB.connect()


class User(Model):
    username = CharField(primary_key=True)


    class Meta:
        database = DB


class Amount(Model):
    user = ForeignKeyField(User, backref='amount')
    amount = FloatField()
    date_time = DateTimeField()


    class Meta:
        database = DB

DB.create_tables([User, Amount])
DB.close()
Example #42
0
app.config.from_object('settings')

database = SqliteDatabase(app.config['DATABASE'], threadlocals=True)

from mhvdb2.admin import admin  # noqa
app.register_blueprint(admin, url_prefix='/admin')


@app.before_request
def before_request():
    g.db = database
    g.db.connect()


@app.after_request
def after_request(response):
    g.db.close()
    return response

import mhvdb2.routes   # noqa

from mhvdb2.models import Entity, User  # noqa

database.connect()
if not Entity.table_exists():
    Entity.create_table()
if not User.table_exists():
    User.create_table()
database.close()
Example #43
0
        database = db


def get_user(user_id):
    try:
        user = User.get(User.id == user_id)
    except User.DoesNotExist:
        user = User.get(User.id == 0)
    return user

if __name__ == "__main__":
    if User.table_exists():
        print("Database Already Exists")
        sys.exit()

    db.connect()
    db.create_tables([User])

    User.insert_many([
        {"id": 0, "name": "Invlid User", "langpair": "!!|!!"},
        {"id": 120755813, "name": "Boris", "langpair": "ru|en"},
        {"id": 93649293, "name": "Elizaveta", "langpair": "ru|en"},
        {"id": 77815902, "name": "James", "langpair": "en|ru"},
        {"id": 68382468, "name": "Jill", "langpair": "en|ru"},
        {"id": 82080280, "name": "Kyle", "langpair": "en|ru", "admin": True},
        {"id": 117778855, "name": "Mickey", "langpair": "en|ru"},
        {"id": 97384423, "name": "Nataly", "langpair": "__|__"},
        {"id": 96351689, "name": "Transbotor", "langpair": "!!|!!"},
    ]).execute()
    db.close()