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 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 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 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 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( textArea=wiki_page.markdown, current_version=wiki_page.current_version, ) if form.validate_on_submit(): if form.current_version.data == wiki_page.current_version: g.wiki_refs = list( WikiPage .select(WikiPage.id) .join(WikiReference, on=WikiReference.referenced) .where(WikiReference.referencing==wiki_page) .execute() ) md = form.textArea.data diff = make_patch(wiki_page.markdown, md) if diff: with db.atomic(): toc, html = markdown(wiki_page, md) wiki_page.update_db(diff, md, html, toc) 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 )
def replace_file(): form = request.form file = request.files['wiki_file'] wiki_file_id = form.get('wiki_file_id', None) wiki_file_markdown = '[file:{0}]'.format(wiki_file_id) # save uploaded file with WikiFile.id as filename file.save(os.path.join( current_app.config['DB_PATH'], g.wiki_group, wiki_file_id )) wiki_file = WikiFile.get_or_none(WikiFile.id==int(wiki_file_id)) with db.atomic(): (WikiFile .update( name=file.filename, mime_type=file.mimetype, size=file.tell()) .where(WikiFile.id==int(wiki_file_id)) .execute()) if wiki_file and wiki_file.name != file.filename: query = (WikiPage .select(WikiPage.id, WikiPage.markdown) .where(WikiPage.markdown.contains(wiki_file_markdown))) for wiki_page in query.execute(): toc, html = markdown(wiki_page, wiki_page.markdown) (WikiPage .update(toc=toc, html=html) .where(WikiPage.id==wiki_page.id) .execute()) return ''
def history(wiki_page_id): wiki_page = get_object_or_404( WikiPage.select(), WikiPage.id==wiki_page_id ) if wiki_page.current_version == 1: return redirect(url_for('.page', wiki_page_id=wiki_page_id)) form = HistoryRecoverForm() if form.validate_on_submit(): if form.version.data >= wiki_page.current_version: flash('Please enter an old version number.') else: query = (WikiPageVersion .select() .where( WikiPageVersion.wiki_page==wiki_page, WikiPageVersion.version>=form.version.data) .order_by(WikiPageVersion.id.desc())) wiki_page_versions = query.execute() old_to_current_patches = [pv.diff for pv in wiki_page_versions] recovered_content = apply_patches(wiki_page.markdown, old_to_current_patches, revert=True) 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, recovered_content) if diff: with db.atomic(): toc, html = markdown(wiki_page, recovered_content) wiki_page.update_db(diff, recovered_content, html, toc) return redirect(url_for('.page', wiki_page_id=wiki_page.id)) old_ver_num = request.args.get('version', default=wiki_page.current_version-1, type=int) new_ver_num = old_ver_num + 1 if new_ver_num > wiki_page.current_version: return redirect(url_for( '.history', wiki_page_id=wiki_page_id, version=wiki_page.current_version-1 )) query = (WikiPageVersion .select() .where( WikiPageVersion.wiki_page==wiki_page, WikiPageVersion.version>=old_ver_num) .order_by(WikiPageVersion.id.desc())) wiki_page_versions = query.execute() old_to_current_patches = [pv.diff for pv in wiki_page_versions] new_markdown = apply_patches(wiki_page.markdown, old_to_current_patches[:-1], revert=True) old_markdown = apply_patches(new_markdown, [old_to_current_patches[-1]], revert=True) diff = difflib.HtmlDiff() diff_table = diff.make_table(old_markdown.splitlines(), new_markdown.splitlines()) diff_table = diff_table.replace(' ', ' ').replace(' nowrap="nowrap"', '') kwargs = dict() get_pagination_kwargs(kwargs, old_ver_num, wiki_page.current_version-1) # manually set toc to empty string to prevent toc from being generated in html wiki_page.toc = '' return render_template( 'wiki/history.html', wiki_page=wiki_page, form=form, wiki_page_versions=wiki_page_versions, old_ver_num=old_ver_num, new_ver_num=new_ver_num, diff_table=diff_table, **kwargs )