Beispiel #1
0
def test_initial(database):
    assert Document(slug='awerty').initial == 'a'
    assert Document(title='QWERTY').initial == 'q'
    assert Document(slug='awerty', title='QWERTY').initial == 'q'

    assert Document.get('a').initial == 'a'
    assert Document.get('b').initial == 'b'
Beispiel #2
0
def edit(slug):
    if request.method == 'GET':
        return render_template('edit.j2', page=Document.get(slug))

    current_revision = Document.get(slug=slug)

    if not request.form['revision'] == str(current_revision.id):
        return render_template('error.j2', error=_('This page has been '
            'modified while you were working on it.')), 400

    if not current_revision.id:
        current_revision.body = ''  # For byte delta computation

    edition_policy = param('edition_policy', '')

    if edition_policy == 'users':
        if not current_user.is_authenticated:
            return render_template('error.j2', error=_('Edition is limited to '
                'registered users only')), 403

    if edition_policy == 'admins':
        if not current_user.admin:
            return render_template('error.j2', error=_('Edition is limited to '
                'administrators only')), 403

    Metadata.deactivate(slug)
    Document.deactivate(slug)

    parsed = parse(request.form['body'])

    for key, values in parsed['meta'].items():
        for value in values:
            Metadata(slug=slug, key=key, value=value) \
                    .save()

    title = parsed['title'] or slug
    body = request.form['body']
    comment = request.form['comment'] or None

    bytes_delta = len(body) - len(current_revision.body)

    page = Document(slug=slug, title=title, body=body, comment=comment,
            bytes_delta=bytes_delta)

    if current_user.is_authenticated:
        page.user_id = current_user.id

    page.save()

    if not current_user.is_anonymous:
        Notification.add(slug, current_user, 'automatic')

    db.session.commit()

    Notification.send(page, current_user)

    flash(_('Thank you for your changes. Your attention to detail '
            'is appreciated.'))

    return redirect(url_for('read', slug=slug))
Beispiel #3
0
def search(path=None):
    query = request.args.get('query', '')

    filters = [item.split('=', 1) for item in path.split('/')] \
            if path else []

    ignores = [item[0] for item in filters]

    hits = Document.search(query, filters)
    facets = Document.facets(hits, ignores=ignores)

    return render_template('search.j2', query=query, path=path, hits=hits,
            facets=facets, total=Document.count())
Beispiel #4
0
def diff(slug, a, b):
    doc_a = Document.get(slug, int(a))
    doc_b = Document.get(slug, int(b))

    body_a = (doc_a.body or '').splitlines()
    body_b = (doc_b.body or '').splitlines()

    name_a = '%s v%s' % (slug, a)
    name_b = '%s v%s' % (slug, b)

    diff = unified_diff(body_a, body_b, name_a, name_b)

    return render_template('diff.j2', page=Document.get(slug), diff=diff)
Beispiel #5
0
def deactivate(slug):
    if request.method == 'GET':
        return render_template('deactivate.j2')

    if not current_user.admin:
        return render_template('error.j2', error=_('You are not allowed to '
            'deactivate this page')), 403

    Document.deactivate(slug)
    Metadata.deactivate(slug)

    db.session.commit()

    return redirect(url_for('read', slug=slug))
Beispiel #6
0
def test_search(database):
    docs = Document.search('')

    assert len(docs) == 3

    docs = Document.search('111')

    assert len(docs) == 2

    docs = Document.search('222')

    assert len(docs) == 1

    docs = Document.search('333')

    assert len(docs) == 0

    docs = Document.search('', filters=[('x', '1')])

    assert len(docs) == 2

    docs = Document.search('', filters=[('x', '1'), ('x', '2')])

    assert len(docs) == 1

    docs = Document.search('', filters=[('x', '1'), ('y', '2')])

    assert len(docs) == 1

    docs = Document.search('111', filters=[('x', '1')])

    assert len(docs) == 1
Beispiel #7
0
def recent_changes():
    page_arg = get_int_arg('page', 1)

    changes = Document.changes() \
            .paginate(page_arg, 100)

    return render_template('recent-changes.j2', changes=changes)
Beispiel #8
0
    def run(self, parent, blocks):
        block = blocks.pop(0)

        (directive, rest) = block.split(' ', 1)

        config = json.loads(rest)

        raw = config.get('raw', False)
        filters = config.get('filters')

        if filters:
            filters = filters.items()

        items = Document.search(filters=filters)

        if items:
            if raw:
                items = ['- [[' + item.slug + ']]' for item in items]
            else:
                items = ['- [' + item.title + '](' + item.slug + ')'
                            for item in items]
        else:
            items = ['- *No results found*']

        blocks.insert(0, '\n'.join(items))
Beispiel #9
0
def activate(slug):
    version = get_int_arg('version')

    if not version:
        return render_template('error.j2', error=_('Missing version '
            'parameter'))

    if request.method == 'GET':
        return render_template('activate.j2')

    if not current_user.admin:
        return render_template('error.j2', error=_('You are not allowed to '
            'activate this page')), 403

    page = Document.get(slug, version)

    parsed = parse(page.body)

    for key, values in parsed['meta'].items():
        for value in values:
            Metadata(slug=slug, key=key, value=value) \
                    .save()

    page.save()

    db.session.commit()

    return redirect(url_for('read', slug=slug))
Beispiel #10
0
def history(slug):
    page_arg = get_int_arg('page', 1)

    page = Document.get(slug)
    history = page.history() \
            .paginate(page_arg, 100)

    return render_template('history.j2', page=page, history=history)
Beispiel #11
0
def recent_changes_atom():
    page_arg = get_int_arg('page', 1)

    changes = Document.changes() \
            .paginate(page_arg, 100)

    return render_template('recent-changes_atom.j2', changes=changes,
            now=datetime.datetime.utcnow())
Beispiel #12
0
def history_atom(slug):
    page_arg = get_int_arg('page', 1)

    page = Document.get(slug)
    history = page.history() \
            .paginate(page_arg, 100)

    return render_template('history_atom.j2', page=page, history=history,
            now=datetime.datetime.utcnow())
Beispiel #13
0
def test_history(database):
    history = Document.get('a') \
            .history() \
            .all()

    assert len(history) == 2

    history = Document.get('b') \
            .history() \
            .all()

    assert len(history) == 1

    history = Document.get('c') \
            .history() \
            .all()

    assert len(history) == 1
Beispiel #14
0
def test_get(database):
    assert Document.get('a').id is not None
    assert Document.get('b').id is not None
    assert Document.get('c').id is not None
    assert Document.get('d').id is None

    assert '222' in Document.get('a').body
    assert '111' in Document.get('b').body
    assert '111' in Document.get('c').body
    assert 'Describe' in Document.get('d').body

    doc = Document.get('a', '1')

    assert '111' in doc.body
    assert not doc.active
Beispiel #15
0
def load_fixtures():
    '''Load fixtures into the database.'''
    cwd = getcwd()
    dir = path.join(cwd, FIXTURES_DIR)

    for filename in listdir(dir):
        file = path.join(dir, filename)

        if not path.isfile(file) or not file.endswith('.md'):
            continue

        print(' * Loading %s' % filename)

        slug = filename[:-3]

        page = Document.get(slug=slug)

        if page.id:
            print(' * Document exists, skipping')
            continue

        body = codecs.open(file, 'r', 'utf-8') \
                .read()

        parsed = parse(body)

        title = parsed['title'] or slug

        Metadata.deactivate(slug)
        Document.deactivate(slug)

        for key, values in parsed['meta'].items():
            for value in values:
                Metadata(slug=slug, key=key, value=value) \
                        .save()

        Document(slug=slug, title=title, body=body) \
                        .save()

        db.session.commit()
Beispiel #16
0
def test_timestamp(database):
    assert Document().ymd is None
    assert Document().hm is None
    assert Document().ymd_hm is None

    assert Document.get('a').ymd is not None
    assert Document.get('a').hm is not None
    assert Document.get('a').ymd_hm is not None
Beispiel #17
0
def test_facets(database):
    docs = Document.search('')
    facets = Document.facets(docs)

    assert 'x' in facets
    assert 'y' in facets

    docs = Document.search('', filters=[('x', '3')])
    facets = Document.facets(docs, ignores=['x'])

    assert not facets

    docs = Document.search('', filters=[('x', '1')])
    facets = Document.facets(docs, ignores=['x'])

    assert 'y' in facets
Beispiel #18
0
def read(slug=None):
    version = get_int_arg('version')
    page = Document.get(slug or param('frontpage', 'front-page'),
            version=version)

    if version:
        if not page:
            return render_template('error.j2', error=_('This version does not '
                'exist')), 404

        flash(_('You are viewing version %(version)s of this page',
            version=version))

        return render_template('read.j2', page=page)

    parsed = parse(page.body)

    if 'page' in parsed['redirect']:
        page_name = page.title or page.slug
        flash(_('Redirected from %(page_name)s', page_name=page_name))

        return redirect(url_for('read', slug=parsed['redirect']['page']))

    return render_template('read.j2', page=page), 200 if page.id else 404
Beispiel #19
0
def database():
    db.drop_all()
    db.create_all()

    Document(slug='a', title='A', body='111').save()

    db.session.commit()

    Document.deactivate('a')

    db.session.commit()

    Document(slug='a', title='A', body='222').save()
    Document(slug='b', title='B', body='111').save()
    Document(slug='c', title='C', body='111').save()

    Metadata(slug='a', key='x', value='1').save()
    Metadata(slug='a', key='y', value='2').save()
    Metadata(slug='b', key='x', value='1').save()
    Metadata(slug='b', key='x', value='2').save()

    db.session.commit()
Beispiel #20
0
def test_titles(database):
    titles = Document.titles().all()

    assert len(titles) == 3
Beispiel #21
0
def read_md(slug):
    page = Document.get(slug)

    return page.body, 200 if page.id else 404, {
        'Content-Type': 'text/markdown; charset=utf-8'
    }
Beispiel #22
0
def test_count(database):
    assert Document.count() == 3
Beispiel #23
0
def test_meta(database):
    assert Document.get('a').meta() != []
    assert Document.get('b').meta() != []
    assert Document.get('c').meta() == []
    assert Document.get('d').meta() == []
Beispiel #24
0
def title_index():
    return render_template('title-index.j2', titles=Document.titles())
Beispiel #25
0
 def page(self):
     return Document.get(self.slug)
Beispiel #26
0
def test_load_fixtures():
    db.create_all()

    load_fixtures()

    assert Document.count() == 3
Beispiel #27
0
def test_changes(database):
    changes = Document.changes().all()

    assert len(changes) == 4