Beispiel #1
0
    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)
Beispiel #2
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
Beispiel #3
0
    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)
Beispiel #4
0
    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']
Beispiel #5
0
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)
Beispiel #6
0
    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
Beispiel #7
0
    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)
Beispiel #8
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 #9
0
    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'
Beispiel #10
0
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)
Beispiel #11
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 #12
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)]
Beispiel #13
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']
Beispiel #14
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 #15
0
    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']
Beispiel #16
0
    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)]
Beispiel #17
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)
Beispiel #18
0
    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)