def keypage_edit(): query = (WikiPage.select(WikiPage.id, WikiPage.title).join( WikiKeypage, on=(WikiKeypage.wiki_page)).order_by(WikiKeypage.id)) wiki_keypages = query.execute() keypage_titles = [wiki_keypage.title for wiki_keypage in wiki_keypages] form = KeyPageEditForm(textArea='\n'.join(keypage_titles)) if form.validate_on_submit(): wiki_pages = list() new_titles = form.textArea.data.splitlines() for new_title in new_titles: wiki_page = (WikiPage.select( WikiPage.id, WikiPage.title).where(WikiPage.title == new_title).execute()) if wiki_page: wiki_pages.append((wiki_page[0], )) WikiKeypage.drop_table(safe=True) WikiKeypage.create_table(safe=True) (WikiKeypage.insert_many(wiki_pages, fields=[WikiKeypage.wiki_page]).execute()) return redirect(url_for('.home')) return render_template('wiki/keypage_edit.html', form=form)
def test_search_score(self, db): with db.atomic(): wiki_page = WikiPage.create( title='foo1', markdown='bar' ) WikiPageIndex.create( docid=wiki_page.id, title=wiki_page.title, ) wiki_page = WikiPage.create( title='bar', markdown='foo1 ' ) WikiPageIndex.create( docid=wiki_page.id, title=wiki_page.title, markdown=wiki_page.markdown ) query = WikiPageIndex.search_bm25( 'foo1', weights={'title': 3.0, 'markdown': 2.0}, with_score=True, score_alias='search_score', explicit_ordering=True ) # assert False assert isinstance(query.execute()[0], WikiPageIndex)
def rename(wiki_page_id): wiki_page = get_object_or_404(WikiPage.select(WikiPage.id, WikiPage.title), WikiPage.id == wiki_page_id) if wiki_page.title == 'Home': return redirect(url_for('.home')) form = RenameForm(new_title=wiki_page.title) if form.validate_on_submit(): new_title = form.new_title.data if wiki_page.title == new_title: flash('The page name is not changed.', 'warning') elif WikiPage.select().where(WikiPage.title == new_title).count() > 0: flash('The new page title has already been taken.', 'warning') else: with db.atomic(): old_markdown = '[[{}]]'.format(wiki_page.title) new_markdown = '[[{}]]'.format(new_title) old_html = render_wiki_page(wiki_page.id, wiki_page.title, tostring=True) new_html = render_wiki_page(wiki_page.id, new_title, tostring=True) # update the markdown of referencing wiki page query = (WikiPage.select( WikiPage.id, WikiPage.markdown, WikiPage.html).join( WikiReference, on=WikiReference.referencing).where( WikiReference.referenced == wiki_page)) wiki_referencing_pages = query.execute() for ref in wiki_referencing_pages: new_markdown_content = ref.markdown.replace( old_markdown, new_markdown) (WikiPageIndex.update(markdown=new_markdown_content).where( WikiPageIndex.docid == ref.id).execute()) (WikiPage.update( markdown=new_markdown_content, html=ref.html.replace( old_html, new_html)).where(WikiPage.id == ref.id).execute()) # update the diff of related wiki page versions query = (WikiPageVersion.select( WikiPageVersion.id, WikiPageVersion.diff).where( WikiPageVersion.diff.contains(old_markdown))) wiki_page_versions = query.execute() for pv in wiki_page_versions: (WikiPageVersion.update(diff=pv.diff.replace( old_markdown, new_markdown)).where( WikiPageVersion.id == pv.id).execute()) (WikiPage.update(title=new_title).where( WikiPage.id == wiki_page.id).execute()) return redirect(url_for('.page', wiki_page_id=wiki_page.id)) return render_template('wiki/rename.html', wiki_page=wiki_page, form=form)
def inject_wiki_group_data(): if request.endpoint in [ 'wiki.edit', 'wiki.upload', 'wiki.handle_upload', 'wiki.file' ]: return dict(wiki_group=g.wiki_group) search_form = SearchForm() query = (WikiPage.select(WikiPage.id, WikiPage.title).join( WikiKeypage, on=(WikiKeypage.wiki_page)).order_by(WikiKeypage.id)) wiki_keypages = query.execute() # TODO: enhancement - this might be a performance bottleneck in the future. query = (WikiPage.select(WikiPage.id, WikiPage.title, WikiPage.modified_on).order_by( WikiPage.modified_on.desc()).limit(5)) wiki_changes = query.execute() latest_change_time = convert_utc_to_local(wiki_changes[0].modified_on) now = convert_utc_to_local(datetime.utcnow()) if latest_change_time.date() == now.date(): latest_change_time = latest_change_time.strftime('[%H:%M]') else: latest_change_time = latest_change_time.strftime('[%b %d]') return dict(wiki_group=g.wiki_group, search_form=search_form, wiki_keypages=wiki_keypages, wiki_changes=wiki_changes, latest_change_time=latest_change_time, convert_utc_to_local=convert_utc_to_local)
def test_remove_with_in(self, db): with db.atomic(): wiki_pages_ref_ing = [] wiki_pages_ref_ed = [] for i in range(3): wiki_pages_ref_ing.append(WikiPage.create(title='ref-ing {0}'.format(i+1))) wiki_pages_ref_ed.append(WikiPage.create(title='ref-ed {0}'.format(i+1))) for i in range(3): for j in range(3): WikiReference.create( referencing = wiki_pages_ref_ing[i], referenced = wiki_pages_ref_ed[j] ) wiki_page = (WikiPage .select() .where(WikiPage.title=='ref-ing 1') .execute())[0] refs_to_del = list(WikiPage .select(WikiPage.id) .join(WikiReference, on=WikiReference.referenced) .where(WikiReference.referencing==wiki_page) .execute()) (WikiReference .delete() .where(WikiReference.referenced.in_(refs_to_del)) .execute()) assert WikiReference.select().where(WikiReference.referencing==wiki_page).count() == 0
def inject_wiki_group_data(): if g.wiki_group not in current_app.active_wiki_groups: return dict() if request.path.startswith('/{0}/edit/'.format(g.wiki_group)) \ or request.path.startswith('/{0}/upload/'.format(g.wiki_group)): return dict(wiki_group=g.wiki_group) search_form = SearchForm() query = (WikiPage.select(WikiPage.id, WikiPage.title).join( WikiKeypage, on=(WikiKeypage.wiki_page)).order_by(WikiKeypage.id)) wiki_keypages = query.execute() # TODO: enhancement - this might be a performance bottleneck in the future. query = (WikiPage.select(WikiPage.id, WikiPage.title, WikiPage.modified_on).order_by( WikiPage.modified_on.desc()).limit(5)) wiki_changes = query.execute() latest_change_time = convert_utc_to_mdt(wiki_changes[0].modified_on) now = convert_utc_to_mdt(datetime.utcnow()) if latest_change_time.date() == now.date(): latest_change_time = latest_change_time.strftime('[%H:%M]') else: latest_change_time = latest_change_time.strftime('[%b %d]') return dict(wiki_group=g.wiki_group, search_form=search_form, wiki_keypages=wiki_keypages, wiki_changes=wiki_changes, latest_change_time=latest_change_time, convert_utc_to_mdt=convert_utc_to_mdt)
def test_atomic_update_for_string_addition(self, db): wiki_page = WikiPage.create(title='foo', markdown='bar') (WikiPage.update(markdown=WikiPage.markdown + ' spam').where(WikiPage.id == wiki_page.id).execute()) wiki_page = WikiPage.get_by_id(wiki_page.id) assert wiki_page.markdown == 'bar spam'
def reference(wiki_page_id): wiki_page = get_object_or_404(WikiPage.select(WikiPage.id, WikiPage.title), WikiPage.id == wiki_page_id) wiki_referencing_pages = (WikiPage.select( WikiPage.id, WikiPage.title).join( WikiReference, on=WikiReference.referencing).where( WikiReference.referenced == wiki_page).execute()) return render_template('wiki/reference.html', wiki_page=wiki_page, wiki_referencing_pages=wiki_referencing_pages)
def test_atomic_update_for_string_addition_when_initially_empth(self, db): wiki_page = WikiPage.create(title='foo') wiki_page = WikiPage.get_by_id(1) (WikiPage.update(markdown=WikiPage.markdown + 'spam').where(WikiPage.id == wiki_page.id).execute()) wiki_page = WikiPage.get_by_id(wiki_page.id) assert wiki_page.markdown == 'spam'
def test_unique_constraint_on_multiple_columns(self, db): with db.atomic(): wiki_page_ref_ing = WikiPage.create(title='ref-ing') wiki_page_ref_ed = WikiPage.create(title='ref-ed') WikiReference.create(referencing=wiki_page_ref_ing, referenced=wiki_page_ref_ed) with pytest.raises(IntegrityError): WikiReference.create(referencing=wiki_page_ref_ing, referenced=wiki_page_ref_ed)
def handle_upload(): form = request.form wiki_page_id = int(form.get('wiki_page_id', None)) upload_from_upload_page = form.get('upload_page', None) file_markdown, file_html = '', '' with db.atomic(): for i, file in enumerate(request.files.getlist('wiki_file')): # create a WikiFile in database and retrieve id wiki_file = WikiFile.create(name=file.filename, mime_type=file.mimetype) # save uploaded file with WikiFile.id as filename file.save(os.path.join(DB_PATH, g.wiki_group, str(wiki_file.id))) wiki_file.size = file.tell() wiki_file.save() if 'image' in wiki_file.mime_type: file_type = 'image' else: file_type = 'file' file_markdown += '\n\n[{}:{}]'.format(file_type, wiki_file.id) file_html += '<p>{}</p>'.format( render_wiki_file(wiki_file.id, wiki_file.name, file_type, tostring=True)) if upload_from_upload_page: wiki_page = get_object_or_404( WikiPage.select(WikiPage.id, WikiPage.markdown, WikiPage.current_version, WikiPage.modified_on), WikiPage.id == wiki_page_id) diff = make_patch(xstr(wiki_page.markdown), xstr(wiki_page.markdown) + file_markdown) WikiPageVersion.create(wiki_page=wiki_page, diff=diff, version=wiki_page.current_version, modified_on=wiki_page.modified_on) (WikiPageIndex.update( markdown=wiki_page.markdown + file_markdown).where( WikiPageIndex.docid == wiki_page.id).execute()) (WikiPage.update(markdown=WikiPage.markdown + file_markdown, html=WikiPage.html + file_html, current_version=WikiPage.current_version + 1, modified_on=datetime.utcnow()).where( WikiPage.id == wiki_page.id).execute()) return '' return file_markdown
def test_versions(self, db): with db.atomic(): wiki_page = WikiPage.create(title='foo', ) for i in range(10): WikiPageVersion.create(wiki_page=wiki_page, diff=str(i + 1), version=i + 1, modified_on=time.time()) wiki_page = WikiPage.select().where(WikiPage.id == 1)[0] assert [v.diff for v in wiki_page.versions ] == [str(i + 1) for i in range(10)]
def test_search_markdown(self, db): with db.atomic(): for i in range(3): WikiPage.create(title='title {0}'.format(i), markdown='foo {0}'.format(i+1)) WikiPage.create(title='title {0}'.format(i+3), markdown='bar {0}'.format(i+1)) WikiPageIndex.rebuild() WikiPageIndex.optimize() wiki_pages_foo = WikiPageIndex.search('foo') assert [wp.markdown for wp in wiki_pages_foo] == ['foo 1', 'foo 2', 'foo 3'] wiki_pages_bar = WikiPageIndex.search('bar') assert [wp.markdown for wp in wiki_pages_bar] == ['bar 1', 'bar 2', 'bar 3']
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 test_search_after_update(self, db): with db.atomic(): wiki_page = WikiPage.create(title='foo') WikiPageIndex.create(rowid=wiki_page.id, title=wiki_page.title) wiki_pages_bar = WikiPageIndex.search('bar') assert len(wiki_pages_bar) == 0 WikiPageIndex.update(title='bar').where( WikiPageIndex.rowid == 1).execute() WikiPage.update(title='bar').where(WikiPage.id == 1).execute() wiki_pages_bar = WikiPageIndex.search('bar') assert wiki_pages_bar[0].title == 'bar'
def edit(wiki_page_id): wiki_page = get_object_or_404( WikiPage.select(WikiPage.id, WikiPage.title, WikiPage.markdown, WikiPage.current_version, WikiPage.modified_on), WikiPage.id == wiki_page_id) form = WikiEditForm() upload_form = UploadForm() if form.validate_on_submit(): if form.current_version.data == wiki_page.current_version: g.wiki_page = wiki_page g.wiki_refs = list( WikiPage.select(WikiPage.id).join( WikiReference, on=WikiReference.referenced).where( WikiReference.referencing == wiki_page).execute()) diff = make_patch(wiki_page.markdown, form.textArea.data) if diff: with db.atomic(): toc, html = markdown(form.textArea.data) WikiPageVersion.create(wiki_page=wiki_page, diff=diff, version=wiki_page.current_version, modified_on=wiki_page.modified_on) (WikiPageIndex.update(markdown=form.textArea.data).where( WikiPageIndex.docid == wiki_page.id).execute()) (WikiPage.update( markdown=form.textArea.data, html=html, toc=toc, current_version=WikiPage.current_version, modified_on=datetime.utcnow()).where( WikiPage.id == wiki_page.id).execute()) # remove unused WikiReference (WikiReference.delete().where( WikiReference.referenced.in_(g.wiki_refs)).execute()) return redirect(url_for('.page', wiki_page_id=wiki_page.id)) else: flash('Other changes have been made to this ' 'wiki page since you started editing it.') return render_template('wiki/edit.html', wiki_page=wiki_page, form=form, upload_form=upload_form)
def test_non_searchable_field(self, db): with db.atomic(): wiki_page = WikiPage.create( title='foo', markdown='bar', html='spam' ) WikiPageIndex.create( docid=wiki_page.id, title=wiki_page.title, markdown=wiki_page.markdown ) query = (WikiPage .select(WikiPage, WikiPageIndex.bm25(3.0, 2.0)) .join( WikiPageIndex, on=(WikiPage.id == WikiPageIndex.docid)) .where(WikiPageIndex.match('foo bar')) .order_by(WikiPageIndex.bm25(3.0, 2.0))) wiki_page = query.execute()[0] count = query.count() assert wiki_page.title == 'foo' assert wiki_page.markdown == 'bar' assert wiki_page.html == 'spam' assert count == 1
def test_timezone(self, db): wiki_page = WikiPage.create(title='foo') mdt = timezone(timedelta(hours=-7), 'MDT') time_utc = wiki_page.modified_on.replace(tzinfo=timezone.utc) time_mdt = time_utc.astimezone(mdt).replace(tzinfo=timezone.utc) assert time_mdt - time_utc == timedelta(hours=-7)
def changes(): query = (WikiPage.select(WikiPage.id, WikiPage.title, WikiPage.modified_on).order_by( WikiPage.modified_on.desc()).limit(50)) wiki_more_changes = query.execute() return render_template('wiki/changes.html', wiki_more_changes=wiki_more_changes)
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 group_admin(): wiki_page_num = WikiPage.select().count() wiki_file_num = WikiFile.select().count() return render_template( 'wiki/group_admin.html', wiki_page_num=wiki_page_num, wiki_file_num=wiki_file_num )
def test_search_when_fts_model_is_out_of_sync(self, db): with db.atomic(): wiki_page = WikiPage.create(title='foo') WikiPageIndex.create(rowid=wiki_page.id, title='bar') wiki_pages_bar = WikiPageIndex.search('bar') assert isinstance(wiki_pages_bar[0], WikiPageIndex) assert wiki_pages_bar[0].title == 'foo'
def handleMatch(self, m): wiki_page_title = m.group(2) try: wiki_page = (WikiPage.select( WikiPage.id).where(WikiPage.title == wiki_page_title).get()) except WikiPage.DoesNotExist: wiki_page = WikiPage.create(title=wiki_page_title) wiki_page_index = WikiPageIndex.create(docid=wiki_page.id, title=wiki_page_title) (WikiReference.insert( referencing=g.wiki_page, referenced=wiki_page).on_conflict_ignore().execute()) try: g.wiki_refs.remove(wiki_page) except ValueError: pass return render_wiki_page(wiki_page.id, wiki_page_title)
def all_pages(): current_page_number = request.args.get('page', default=1, type=int) number_per_page = 100 query = (WikiPage.select( WikiPage.id, WikiPage.title, WikiPage.modified_on).order_by( WikiPage.id).paginate(current_page_number, paginate_by=number_per_page)) kwargs = dict() get_pagination_kwargs(kwargs, query, current_page_number, number_per_page) return render_template('wiki/all_pages.html', **kwargs)
def test_many_to_many_relationship_of_same_model(self, db): with db.atomic(): wiki_pages_ref_ing = [] wiki_pages_ref_ed = [] for i in range(3): wiki_pages_ref_ing.append( WikiPage.create(title='ref-ing {0}'.format(i + 1))) wiki_pages_ref_ed.append( WikiPage.create(title='ref-ed {0}'.format(i + 1))) for i in range(3): for j in range(3): WikiReference.create(referencing=wiki_pages_ref_ing[i], referenced=wiki_pages_ref_ed[j]) wiki_pages = (WikiPage.select().join( WikiReference, on=WikiReference.referencing).where( WikiReference.referenced == wiki_pages_ref_ed[0]).execute()) assert isinstance(wiki_pages[0], WikiPage) assert [wp.title for wp in wiki_pages ] == ['ref-ing 1', 'ref-ing 2', 'ref-ing 3']
def wiki_page(self, title): wiki_page_title = title try: wiki_page = (WikiPage.select( WikiPage.id).where(WikiPage.title == wiki_page_title).get()) except WikiPage.DoesNotExist: wiki_page = WikiPage.create(title=wiki_page_title) wiki_page_index = WikiPageIndex.create(rowid=wiki_page.id, title=wiki_page_title) (WikiReference.insert( referencing=g.wiki_page, referenced=wiki_page).on_conflict_ignore().execute()) try: g.wiki_refs.remove(wiki_page) # AttributeError: g.wiki_refs not exist # ValueError: wiki_page not in g.wiki_refs except (AttributeError, ValueError): pass return render_wiki_page(wiki_page.id, wiki_page_title)
def pdf(wiki_page_id): wiki_page = get_object_or_404( WikiPage.select( WikiPage.id, WikiPage.title, WikiPage.html, WikiPage.modified_on), WikiPage.id==wiki_page_id ) return render_template( 'wiki/pdf.html', wiki_page=wiki_page, )
def handle_upload(): form = request.form wiki_page_id = int(form.get('wiki_page_id', None)) upload_from_upload_page = form.get('upload_page', None) file_markdown, file_html = '', '' with db.atomic(): for i, file in enumerate(request.files.getlist('wiki_file')): # create a WikiFile in database and retrieve id wiki_file = WikiFile.create( name=file.filename, mime_type=file.mimetype ) # save uploaded file with WikiFile.id as filename file.save(os.path.join( current_app.config['DB_PATH'], g.wiki_group, str(wiki_file.id) )) wiki_file.size = file.tell() wiki_file.save() if 'image' in wiki_file.mime_type: file_type = 'image' else: file_type = 'file' file_markdown += '\n\n[{}:{}]'.format(file_type, wiki_file.id) file_html += '<p>{}</p>'.format(render_wiki_file( wiki_file.id, wiki_file.name, file_type )) if upload_from_upload_page: wiki_page = get_object_or_404( WikiPage.select( WikiPage.id, WikiPage.markdown, WikiPage.current_version, WikiPage.modified_on), WikiPage.id==wiki_page_id ) diff = make_patch(xstr(wiki_page.markdown), xstr(wiki_page.markdown)+file_markdown) wiki_page.update_db_after_upload(diff, file_markdown, file_html) return '' return file_markdown
def test_no_index_rebuild_optimize(self, db): with db.atomic(): for i in range(3): wiki_page = WikiPage.create(title='foo {0}'.format(i + 1), markdown='bar {0}'.format(i + 1)) WikiPageIndex.create(rowid=wiki_page.id, title=wiki_page.title, markdown=wiki_page.markdown) wiki_pages_foo = WikiPageIndex.search('foo') assert [wp.title for wp in wiki_pages_foo] == ['foo 1', 'foo 2', 'foo 3'] wiki_pages_bar = WikiPageIndex.search('bar') assert [wp.markdown for wp in wiki_pages_bar] == ['bar 1', 'bar 2', 'bar 3']
def test_column_contains(self, db): with db.atomic(): wiki_page = WikiPage.create(title='foo', ) for i in range(10): WikiPageVersion.create(wiki_page=wiki_page, diff='bar' + str(i + 1), version=i + 1, modified_on=time.time()) query = (WikiPageVersion.select().where( WikiPageVersion.diff.contains('bar'))) wiki_page_versions = query.execute() assert [ wiki_page_version.diff for wiki_page_version in wiki_page_versions ] == ['bar' + str(i + 1) for i in range(10)]