Beispiel #1
0
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)
Beispiel #2
0
    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'
Beispiel #3
0
    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'
Beispiel #4
0
    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'
Beispiel #5
0
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
Beispiel #6
0
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)