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 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 )