<commit_before># -*- coding: utf-8 -*-
"""
    website.vpn.dial.views
    ~~~~~~~~~~~~~~~~~~~~~~

    vpn sts views:
        /vpn/dial/settings
        /vpn/dial/add
        /vpn/dial/<int:id>
        /vpn/dial/<int:id>/settings
"""

from flask import Blueprint, render_template
from flask import url_for, redirect
from flask import flash

from flask.ext.login import login_required

from website.vpn.dial.services import get_accounts, account_update, account_del
from website.vpn.dial.services import VpnServer, settings_update
from website.vpn.dial.forms import AddForm, SettingsForm, ConsoleForm
from website.vpn.dial.models import Account, Settings


dial = Blueprint('dial', __name__, url_prefix='/vpn/dial',
                 template_folder='templates',
                 static_folder='static')


@dial.route('/')
@login_required
def index():
    accounts = get_accounts(status=True)
    if not accounts:
        flash(u'目前没有任何VPN 配置，如有需要请添加。', 'info')
    return render_template('dial/index.html', accounts=accounts)


@dial.route('/add', methods=['GET', 'POST'])
@login_required
def add():
    settings = Settings.query.get(1)
    if not settings:
        flash(u'提示：请先进行「设置」再添加VPN 账号。', 'alert')
        return redirect(url_for('dial.settings'))
    form = AddForm()
    if form.validate_on_submit():
        if not Account.query.filter_by(name=form.name.data).first():
            if account_update(form):
                message = u'添加VPN 拨号账号成功！'
                flash(message, 'success')
                return redirect(url_for('dial.index'))
        else:
            message = u'该账号已经存在：%s' % form.name.data
            flash(message, 'alert')
    return render_template('dial/add.html', form=form)


@dial.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
    form = SettingsForm()
    settings = Settings.query.get(1)
    if form.validate_on_submit():
        if settings_update(form):
            flash(u'修改配置成功！注：修改「虚拟IP 地址池」之后，需手工调整相应的SNAT 设置！', 'success')
            return redirect(url_for('dial.settings'))
    if settings:
        form.subnet.data = settings.subnet
        form.c2c.data = settings.c2c
        form.duplicate.data = settings.duplicate
    return render_template('dial/settings.html', settings=settings, form=form)


@dial.route('/<int:id>/settings', methods=['GET', 'POST'])
@login_required
def id_settings(id):
    form = AddForm()
    account = get_accounts(id)
    if form.validate_on_submit():
        if form.delete.data:
            if account_del(id):
                message = u'删除账号%s ：成功！' % account[0]['name']
                flash(message, 'success')
                return redirect(url_for('dial.index'))
        if form.save.data:
            if account_update(form, id):
                flash(u'修改账号配置成功！', 'success')
                return redirect(url_for('dial.id_settings', id=id))
    return render_template('dial/view.html', account=account[0], form=form)


@dial.route('/console', methods=['GET', 'POST'])
@login_required
def console():
    form = ConsoleForm()
    vpn = VpnServer()<commit_msg>Fix dial vpn new settings default value('no').<commit_after># -*- coding: utf-8 -*-
"""
    website.vpn.dial.views
    ~~~~~~~~~~~~~~~~~~~~~~

    vpn sts views:
        /vpn/dial/settings
        /vpn/dial/add
        /vpn/dial/<int:id>
        /vpn/dial/<int:id>/settings
"""

from flask import Blueprint, render_template
from flask import url_for, redirect
from flask import flash

from flask.ext.login import login_required

from website.vpn.dial.services import get_accounts, account_update, account_del
from website.vpn.dial.services import VpnServer, settings_update
from website.vpn.dial.forms import AddForm, SettingsForm, ConsoleForm
from website.vpn.dial.models import Account, Settings


dial = Blueprint('dial', __name__, url_prefix='/vpn/dial',
                 template_folder='templates',
                 static_folder='static')


@dial.route('/')
@login_required
def index():
    accounts = get_accounts(status=True)
    if not accounts:
        flash(u'目前没有任何VPN 配置，如有需要请添加。', 'info')
    return render_template('dial/index.html', accounts=accounts)


@dial.route('/add', methods=['GET', 'POST'])
@login_required
def add():
    settings = Settings.query.get(1)
    if not settings:
        flash(u'提示：请先进行「设置」再添加VPN 账号。', 'alert')
        return redirect(url_for('dial.settings'))
    form = AddForm()
    if form.validate_on_submit():
        if not Account.query.filter_by(name=form.name.data).first():
            if account_update(form):
                message = u'添加VPN 拨号账号成功！'
                flash(message, 'success')
                return redirect(url_for('dial.index'))
        else:
            message = u'该账号已经存在：%s' % form.name.data
            flash(message, 'alert')
    return render_template('dial/add.html', form=form)


@dial.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
    form = SettingsForm()
    settings = Settings.query.get(1)
    if form.validate_on_submit():
        if settings_update(form):
            flash(u'修改配置成功！注：修改「虚拟IP 地址池」之后，需手工调整相应的SNAT 设置！', 'success')
            return redirect(url_for('dial.settings'))
    if settings:
        form.subnet.data = settings.subnet
        form.c2c.data = settings.c2c or 'no'
        form.duplicate.data = settings.duplicate or 'no'
    return render_template('dial/settings.html', settings=settings, form=form)


@dial.route('/<int:id>/settings', methods=['GET', 'POST'])
@login_required
def id_settings(id):
    form = AddForm()
    account = get_accounts(id)
    if form.validate_on_submit():
        if form.delete.data:
            if account_del(id):
                message = u'删除账号%s ：成功！' % account[0]['name']
                flash(message, 'success')
                return redirect(url_for('dial.index'))
        if form.save.data:
            if account_update(form, id):
                flash(u'修改账号配置成功！', 'success')
                return redirect(url_for('dial.id_settings', id=id))
    return render_template('dial/view.html', account=account[0], form=form)


@dial.route('/console', methods=['GET', 'POST'])
@login_required
def console():
    form = ConsoleForm()
    vpn = VpnServer()
{'fraction_non_alphanumeric': 0.14487859784531276, 'fraction_numerical': 0.0022511657822801095, 'mean_word_length': 2.9922978177150195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7026974', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2058, 'n_words': 395}