Example #1
0
File: db.py Project: bmnz90/cozy
def init_db():
    global db
    global update
    global data_dir

    tmp_db = None
    
    if update:
        update_db()
    else:
        tmp_db = SqliteDatabase(os.path.join(data_dir, "cozy.db"))
        if PeeweeVersion[0] == '2':
            with tmp_db.connection_context():
                tmp_db.create_tables([Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache], True)
        else:
            with tmp_db.connection_context():
                tmp_db.create_tables([Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache])

    # this is necessary to ensure that the tables have indeed been created
    if tmp_db:
        while not tmp_db.table_exists("settings"):
            time.sleep(0.01)

    try:
        db.connect()
    except Exception as e:
        log.error("Could not connect to database. ")
        log.error(e)


    if PeeweeVersion[0] == '3':
        db.bind([Book, Track, Settings, ArtworkCache, StorageBlackList, OfflineCache, Storage], bind_refs=False, bind_backrefs=False)

    if (Settings.select().count() == 0):
        Settings.create(path="", last_played_book=None)
Example #2
0
def init_db():
    tmp_db = None

    _connect_db(_db)

    if Settings.table_exists():
        update_db()
    else:
        tmp_db = SqliteDatabase(os.path.join(get_data_dir(), "cozy.db"))
        if PeeweeVersion[0] == '2':
            tmp_db.create_tables([
                Track, Book, Settings, ArtworkCache, Storage, StorageBlackList,
                OfflineCache
            ], True)
        else:
            with tmp_db.connection_context():
                tmp_db.create_tables([
                    Track, Book, Settings, ArtworkCache, Storage,
                    StorageBlackList, OfflineCache
                ])

    # this is necessary to ensure that the tables have indeed been created
    if tmp_db:
        if PeeweeVersion[0] == '2':
            while not Settings.table_exists():
                time.sleep(0.01)
        else:
            while not tmp_db.table_exists("settings"):
                time.sleep(0.01)

    _connect_db(_db)

    if PeeweeVersion[0] == '3':
        _db.bind([
            Book, Track, Settings, ArtworkCache, StorageBlackList,
            OfflineCache, Storage
        ],
                 bind_refs=False,
                 bind_backrefs=False)

    if (Settings.select().count() == 0):
        Settings.create(path="", last_played_book=None)
Example #3
0
    class Meta:
        table_name = 'users'


class JokesUsers(BaseModel):
    user_id = ForeignKeyField(Users)
    joke_id = ForeignKeyField(Jokes)

    class Meta:
        table_name = 'jokes_users'


TABLES = [Jokes, JokesUsers, Users]

with db.connection_context():
    db.create_tables(TABLES, safe=True)
    db.commit()

app = Flask(__name__)

first_options_list = "animal, career, celebrity, dev, explicit, fashion, food, history, money, movie, music, political, religion, science, sport, travel"


@app.before_request
def _db_connect():
    db.connect()


@app.teardown_request
def _db_close(_):
Example #4
0
        ['user', 'Student', '*****@*****.**', student_role],
    ]

    for entity in entities:
        user = dict(zip(fields, entity))
        password = generate_password()
        User.create(**user, password=password)
        print(f"User: {user['username']}, Password: {password}")


def create_basic_roles():
    for role in RoleOptions:
        Role.create(name=role.value)


with database.connection_context():
    admin = Admin(
        webapp,
        name='LMS',
        template_mode='bootstrap3',
        index_view=MyAdminIndexView(),
    )

    ALL_MODELS = (User, Exercise, CommentText, Solution, Role, Comment)
    for m in ALL_MODELS:
        admin.add_view(AdminModelView(m))

    database.create_tables(ALL_MODELS, safe=True)

    if Role.select().count() == 0:
        create_basic_roles()