コード例 #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)
コード例 #2
0
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)
コード例 #3
0
ファイル: decorators.py プロジェクト: GNHua/pwlite
    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)
コード例 #4
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)
コード例 #5
0
    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
コード例 #6
0
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)
コード例 #7
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)
コード例 #8
0
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)
コード例 #9
0
ファイル: views.py プロジェクト: GNHua/pwlite
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
    )
コード例 #10
0
    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)]
コード例 #11
0
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)
コード例 #12
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
コード例 #13
0
ファイル: views.py プロジェクト: GNHua/pwlite
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,
    )
コード例 #14
0
ファイル: views.py プロジェクト: GNHua/pwlite
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
コード例 #15
0
    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)
コード例 #16
0
def search():
    keyword = request.args.get('keyword')
    start_date = request.args.get('start')
    end_date = request.args.get('end')
    current_page_number = request.args.get('page', default=1, type=int)
    number_per_page = 100
    kwargs = dict()
    form = SearchForm(search=keyword, start_date=start_date, end_date=end_date)

    if keyword and not keyword.isspace():
        filter = [WikiPageIndex.match(keyword)]
        if start_date:
            temp = datetime.strptime(start_date, '%m/%d/%Y')
            filter.append(
                WikiPage.modified_on > convert_utc_to_mdt(temp, reverse=True))
        if end_date:
            temp = datetime.strptime(end_date, '%m/%d/%Y') + timedelta(days=1)
            filter.append(
                WikiPage.modified_on < convert_utc_to_mdt(temp, reverse=True))

        query = (WikiPage.select(
            WikiPage.id, WikiPage.title, WikiPage.modified_on).join(
                WikiPageIndex, on=(WikiPage.id == WikiPageIndex.docid)).where(
                    *filter).order_by(WikiPageIndex.rank(2.0, 1.0),
                                      WikiPage.modified_on.desc()).paginate(
                                          current_page_number,
                                          paginate_by=100))
        # TODO: add title-only search
        # query = query.where(WikiPage.title.contains(search_keyword))

        get_pagination_kwargs(kwargs, query, current_page_number,
                              number_per_page)

    if form.validate_on_submit():
        return redirect(
            url_for('.search',
                    keyword=form.search.data,
                    start=form.start_date.data,
                    end=form.end_date.data))

    return render_template('wiki/search.html', form=form, **kwargs)
コード例 #17
0
ファイル: views.py プロジェクト: GNHua/pwlite
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
    )
コード例 #18
0
    def test_non_searchable_field(self, db):
        with db.atomic():
            wiki_page = WikiPage.create(title='foo',
                                        markdown='bar',
                                        html='spam')
            WikiPageIndex.create(rowid=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.rowid)).where(
                WikiPageIndex.match(
                    WikiPageIndex.clean_query('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
コード例 #19
0
ファイル: markdown.py プロジェクト: GNHua/pwlite
    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)
コード例 #20
0
    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']
コード例 #21
0
ファイル: views.py プロジェクト: GNHua/pwlite
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('&nbsp;', ' ').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
    )