Source

ryshcate / ryshcate / views / system.py

# -*- coding: utf-8 -*-
"""
ryshcate.views.system
=====================
This module contains views that perform system support functions like changing
the user's settings.

:copyright: 2010 Matthew "LeafStorm" Frazier
:license:   MIT/X11, see LICENSE for details
"""
from flask import (Module, Response, request, session, current_app, flash,
                   redirect, url_for, render_template)
from ryshcate.api import handler
from ryshcate.forms import SettingsForm
from ryshcate.locale import _
from ryshcate.utils import get_style_css

system = Module(__name__)

handler.connect(system, '/api')


@system.route('/settings', methods=['GET', 'POST'])
def settings():
    if request.method == 'POST' and request.form.get('clear'):
        session.clear()
        flash(_(u"Settings cleared"), 'success')
        return redirect(url_for('settings'))
    
    cfg = current_app.config
    locale = session.get('locale', cfg.get('BABEL_DEFAULT_LANGUAGE', 'en'))
    timezone = session.get('timezone', cfg.get('BABEL_DEFAULT_TIMEZONE',
                           'UTC'))
    style = session.get('style', cfg.get('DEFAULT_PYGMENTS_STYLE', 'default'))
    del cfg
    
    form = SettingsForm(request.form, locale=locale, style=style,
                        timezone=timezone)
    if request.method == 'POST' and form.validate():
        session['locale'] = form.locale.data
        session['timezone'] = form.timezone.data
        session['style'] = form.style.data
        session.permanent = True
        flash(_(u"Settings saved"), 'success')
    return render_template('settings.html', form=form)


@system.route('/api', methods=['GET'])
def api_info():
    api_url = url_for('system.api', _external=True)
    return render_template('api_info.html', url=api_url)