<commit_before>from . import db
from .models import Certificate, CertificateForm

from flask import Blueprint, jsonify, render_template, request, url_for
from logging import getLogger
from pwm import Domain

mod = Blueprint('views', __name__)
_logger = getLogger('pwm_server.views')

@mod.route('/')
def home():
    return render_template('main.html')


@mod.route('/domains')
def domain_search():
    domain_query = request.args.get('q')
    if not domain_query:
        return jsonify({
            'msg': 'You need to specify a query to search for',
        }), 400
    domains = db.session.query(Domain).filter(Domain.name.ilike('%%%s%%' % domain_query)).all()
    return jsonify({'domains': [
        {
        'salt': d.salt,
        'name': d.name,
        'charset': d.charset,
        'username': d.username,
        } for d in domains],
    })


@mod.route('/domains', methods=['POST'])
def new_domain():
    domain_name = request.form.get('name')
    domain_alphabet = request.form.get('alphabet')
    domain_key_length = request.form.get('length')
    if not all([domain_name, domain_alphabet, domain_key_length]):
        _logger.warning('Received invalid new domain form: name=%s, alphabet=%s, length=%s',
            domain_name, domain_alphabet, domain_key_length)
        return jsonify({
            'msg': 'name, alphabet and key_length *must* be specified!',
        }), 400
    domain_username = request.form.get('username')
    try:
        domain = Domain(name=domain_name, alphabet=domain_alphabet, key_length=domain_key_length,
            username=domain_username)
        db.session.add(domain)
        db.session.commit()
        return jsonify({
            'msg': 'New domain added succesfully',
            'domain': {
                'name': domain.name,
                'salt': domain.salt,
            }
        }), 201
    except:
        return jsonify({
            'msg': 'Did not validate',
        }), 400


@mod.route('/ca/csr', methods=['POST'])
def new_csr():
    form = CertificateForm()
    if form.validate_on_submit():
        cert = Certificate()
        form.populate_obj(cert)
        db.session.add(cert)
        db.session.commit()
        _logger.info('New certificate approved')
        return jsonify({
            'msg': 'CSR accepted.',
            'href': url_for('.get_certificate', certificate_id=cert.id),
        }), 202
    else:
        _logger.warning('Got invalid CSR from %s', request.access_route)
        return jsonify({
            'msg': 'Invalid CSR',<commit_msg>Fix logging of access_route on invalid CSR<commit_after>from . import db
from .models import Certificate, CertificateForm

from flask import Blueprint, jsonify, render_template, request, url_for
from logging import getLogger
from pwm import Domain

mod = Blueprint('views', __name__)
_logger = getLogger('pwm_server.views')

@mod.route('/')
def home():
    return render_template('main.html')


@mod.route('/domains')
def domain_search():
    domain_query = request.args.get('q')
    if not domain_query:
        return jsonify({
            'msg': 'You need to specify a query to search for',
        }), 400
    domains = db.session.query(Domain).filter(Domain.name.ilike('%%%s%%' % domain_query)).all()
    return jsonify({'domains': [
        {
        'salt': d.salt,
        'name': d.name,
        'charset': d.charset,
        'username': d.username,
        } for d in domains],
    })


@mod.route('/domains', methods=['POST'])
def new_domain():
    domain_name = request.form.get('name')
    domain_alphabet = request.form.get('alphabet')
    domain_key_length = request.form.get('length')
    if not all([domain_name, domain_alphabet, domain_key_length]):
        _logger.warning('Received invalid new domain form: name=%s, alphabet=%s, length=%s',
            domain_name, domain_alphabet, domain_key_length)
        return jsonify({
            'msg': 'name, alphabet and key_length *must* be specified!',
        }), 400
    domain_username = request.form.get('username')
    try:
        domain = Domain(name=domain_name, alphabet=domain_alphabet, key_length=domain_key_length,
            username=domain_username)
        db.session.add(domain)
        db.session.commit()
        return jsonify({
            'msg': 'New domain added succesfully',
            'domain': {
                'name': domain.name,
                'salt': domain.salt,
            }
        }), 201
    except:
        return jsonify({
            'msg': 'Did not validate',
        }), 400


@mod.route('/ca/csr', methods=['POST'])
def new_csr():
    form = CertificateForm()
    if form.validate_on_submit():
        cert = Certificate()
        form.populate_obj(cert)
        db.session.add(cert)
        db.session.commit()
        _logger.info('New certificate approved')
        return jsonify({
            'msg': 'CSR accepted.',
            'href': url_for('.get_certificate', certificate_id=cert.id),
        }), 202
    else:
        _logger.warning('Got invalid CSR from %s', ' -> '.join(request.access_route))
        return jsonify({
            'msg': 'Invalid CSR',
{'fraction_non_alphanumeric': 0.1362925303089558, 'fraction_numerical': 0.005866249511145874, 'mean_word_length': 2.5995777621393383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '7178469', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1486, 'n_words': 359}