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 menu():
    if not current_user.admin:
        error = _('You are not allowed to access this page')
        return render_template('error.j2', error=error), 403

    if request.method == 'GET':
        return render_template('admin-menu.j2')

    item = {
        'type': request.form.get('type', 'link'),
    }

    if item['type'] == 'link':
        item['title'] = request.form.get('title', 'Title')
        item['url'] = request.form.get('url', 'URL')

    menu = param('menu', g.DEFAULT_MENU, 'json') or {}
    menu.setdefault('items', []) \
        .append(item)

    Parameter.set('menu', json.dumps(menu))

    db.session.commit()

    Parameter.clear_cache()

    flash(_('Your changes have been saved'))

    return redirect(url_for('menu'))
Exemplo n.º 3
0
def send_email(context, subject, body, params):
    if not with_mail():
        return

    recipients = [context['user']['email']]

    params.update({
        'scheme': context['scheme'],
        'host': context['host'],
        'service': param('title', 'Diamond wiki')
    })

    with app.app_context():
        g.locale = context['locale']

        refresh()

        subject = _(subject)
        body = _(body.strip(), **params)

        message = Message(subject, recipients, body)

        try:
            mail.send(message)
        except:
            app.logger.error(traceback.format_exc().strip())
Exemplo n.º 4
0
def menu_remove(i):
    menu = param('menu', g.DEFAULT_MENU, 'json') or {}
    menu.setdefault('items', [])

    menu['items'].pop(i)

    Parameter.set('menu', json.dumps(menu))

    db.session.commit()

    Parameter.clear_cache()

    flash(_('Your changes have been saved'))

    return redirect(url_for('menu'))
Exemplo n.º 5
0
def menu_move_down(i):
    menu = param('menu', g.DEFAULT_MENU, 'json') or {}
    menu.setdefault('items', [])

    item = menu['items'].pop(i)

    i = 0 if i >= len(menu['items']) else i + 1

    menu['items'].insert(i, item)

    Parameter.set('menu', json.dumps(menu))

    db.session.commit()

    Parameter.clear_cache()

    flash(_('Your changes have been saved'))

    return redirect(url_for('menu'))
Exemplo n.º 6
0
def test_all(database):
    Parameter.clear_cache()

    assert Parameter.cache is None
    assert Parameter.timestamp is None

    assert len(Parameter.get_all()) == 0

    assert Parameter.get('xxx') == ''
    assert Parameter.get('xxx', 42) == 42

    assert len(Parameter.get_all()) == 0

    Parameter.set('xxx', 666)

    db.session.commit()

    assert len(Parameter.get_all()) == 1

    assert Parameter.cache is not None
    assert Parameter.timestamp is not None

    assert Parameter.get('xxx') != ''
    assert Parameter.get('xxx', 42) == '666'

    assert Parameter.get('xxx', 42, int) == 666

    assert Parameter.cache is not None
    assert Parameter.timestamp is not None

    Parameter.set('xxx', 42)

    db.session.commit()

    assert len(Parameter.get_all()) == 1

    assert param('xxx', 666, cast=int) == 42
Exemplo n.º 7
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