Exemplo n.º 1
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))
Exemplo n.º 2
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'
Exemplo n.º 3
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
Exemplo n.º 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)
Exemplo n.º 5
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))
Exemplo n.º 6
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)
Exemplo n.º 7
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
Exemplo n.º 8
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())
Exemplo n.º 9
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
Exemplo n.º 10
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()
Exemplo n.º 11
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
Exemplo n.º 12
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() == []
Exemplo n.º 13
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'
    }
Exemplo n.º 14
0
 def page(self):
     return Document.get(self.slug)