sphinx-demo-webapp-pencils-down / sphinxdemo / views /

Full commit
# -*- coding: utf-8 -*-

    Views for the Sphinx Web Support demo.

    :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.

from os import path
from flask import Module, render_template, request, g, session, flash, \
     redirect, url_for, abort, jsonify
from sphinx.websupport import WebSupport
from sphinx.websupport.errors import *
from sphinxdemo import app
demo = Module(__name__)

support = WebSupport(datadir=path.join(app.config['BUILD_DIR'], 'data'),

def docs():
    return redirect('/docs/contents')

def doc(docname):
    username = if g.user else ''
    moderator = g.user.moderator if g.user else False
    document = support.get_document(docname, username, moderator)
    return render_template('doc.html', document=document)

def search():
    document = support.get_search_results(request.args.get('q', ''))
    return render_template('doc.html', document=document)

def get_comments():
    username = if g.user else None
    moderator = g.user.moderator if g.user else False
    node_id = request.args.get('node', '')
    data = support.get_data(node_id, username, moderator=moderator)
    return jsonify(**data)

@demo.route('/docs/add_comment', methods=['POST'])
def add_comment():
    parent_id = request.form.get('parent', '')
    node_id = request.form.get('node', '')
    text = request.form.get('text', '')
    proposal = request.form.get('proposal', '')
    username = if g.user is not None else 'Anonymous'
    comment = support.add_comment(text, node_id=node_id, parent_id=parent_id,
                                  username=username, proposal=proposal)
    return jsonify(comment=comment)

@demo.route('/docs/accept_comment', methods=['POST'])
def accept_comment():
    moderator = g.user.moderator if g.user else False
    comment_id = request.form.get('id')
    support.accept_comment(comment_id, moderator=moderator)
    return 'OK'

@demo.route('/docs/reject_comment', methods=['POST'])
def reject_comment():
    moderator = g.user.moderator if g.user else False
    comment_id = request.form.get('id')
    support.reject_comment(comment_id, moderator=g.user.moderator)
    return 'OK'

@demo.route('/docs/delete_comment', methods=['POST'])
def delete_comment():
    moderator = g.user.moderator if g.user else False
    username = if g.user else ''
    comment_id = request.form.get('id')
        support.delete_comment(comment_id, username=username,
    except UserNotAuthorizedError:
    return 'OK'

@demo.route('/docs/process_vote', methods=['POST'])
def process_vote():
    if g.user is None:
    comment_id = request.form.get('comment_id')
    value = request.form.get('value')
    if value is None or comment_id is None:
    support.process_vote(comment_id,, value)
    return "success"