def db(): """A test database for the tests.""" _db.pick(':memory:') _db.create_tables(MODELS) yield _db _db.drop_tables(MODELS) _db.close()
def register_database(app): admin_db_exists = os.path.exists( os.path.join(app.config['DB_PATH'], app.config['ADMIN_DB'])) db.pick(app.config['ADMIN_DB']) if not admin_db_exists: db.create_tables([WikiGroup]) query = WikiGroup.select().where(WikiGroup.active == True) app.active_wiki_groups = [ wiki_group.db_name for wiki_group in query.execute() ] db.close()
def super_admin(): """Manage wiki groups.""" active_wiki_groups = get_active_wiki_groups() inactive_wiki_groups = get_inactive_wiki_groups() all_wiki_groups = active_wiki_groups + inactive_wiki_groups form = AddWikiGroupForm() # Create a new wiki group with its own database and static file directory if form.validate_on_submit(): new_wiki_group_name = form.wiki_group_name.data if new_wiki_group_name in all_wiki_groups: flash('Wiki Group already exists. Please remove it and try again.', 'warning') if os.path.exists( os.path.join(current_app.config['DB_PATH'], new_wiki_group_name)): flash( 'Upload directory already exists. Please remove it and try again.', 'warning') # make the folder for uploaded files os.mkdir( os.path.join(current_app.config['DB_PATH'], new_wiki_group_name)) db.pick( f'{new_wiki_group_name}{current_app.config["ACTIVE_DB_SUFFIX"]}') db.create_tables([ WikiPage, WikiPageIndex, WikiPageVersion, WikiReference, WikiFile, WikiKeypage ]) # Create wiki group home page, and the id is 1. WikiPage.create(title='Home') db.close() flash('New wiki group added', 'info') return redirect(url_for('.super_admin')) else: flash_errors(form) # merge all wiki groups into one list and mark active/inactive all_wiki_groups_sorted = [] for wiki_group in active_wiki_groups: all_wiki_groups_sorted.append((wiki_group, True)) for wiki_group in inactive_wiki_groups: all_wiki_groups_sorted.append((wiki_group, False)) all_wiki_groups_sorted.sort() return render_template('admin/super_admin.html', form=form, all_wiki_groups_sorted=all_wiki_groups_sorted)
def super_admin(): """Manage wiki groups.""" all_wiki_groups = WikiGroup.select().execute() form = AddWikiGroupForm() # Create a new wiki group with its own database and static file directory if form.validate_on_submit(): new_wiki_group_name = form.wiki_group_name.data new_db_name = new_wiki_group_name.replace(' ', '') # Save the name of the new wiki group in database `_admin` # Remove whitespaces in the wiki group name. # Then use it to name the database which is about to be initialized. try: new_wiki_group = WikiGroup.create(name=new_wiki_group_name, db_name=new_db_name, active=True) os.mkdir(os.path.join(DB_PATH, new_wiki_group.db_name)) query = WikiGroup.select().where(WikiGroup.active == True) current_app.active_wiki_groups = [ wiki_group.db_name for wiki_group in query.execute() ] db.close() db.pick(new_wiki_group.db_filename()) db.create_tables([ WikiPage, WikiPageIndex, WikiPageVersion, WikiReference, WikiFile, WikiKeypage ]) # Create wiki group home page, and the id is 1. WikiPage.create(title='Home') flash('New wiki group added', 'info') return redirect(url_for('.super_admin')) except IntegrityError: flash('Wiki Group already exists', 'warning') except FileExistsError: flash('Upload directory already exists.', 'warning') else: flash_errors(form) return render_template('admin/super_admin.html', form=form, all_wiki_groups=all_wiki_groups)
def open_database_connection(): db.pick('{0}.db'.format(g.wiki_group))
def open_database_connection(): db.pick(f'{g.wiki_group}{current_app.config["ACTIVE_DB_SUFFIX"]}')
def open_database_connection(): db.pick(ADMIN_DB)