<commit_before>from flask import Flask, render_template, request, redirect, abort, flash, session, url_for
from flask_script import Manager
import settings
from importlib import reload
from shared import *
from database import loadSession, Gateways, Address, InboundMapping, OutboundRoutes, Subscribers, dSIPFusionPBXDB, dSIPLCR
import os
import subprocess
import json

app = Flask(__name__, static_folder="./static", static_url_path="/static")
app.debug = settings.DEBUG
# app.add_template_filter()
db = loadSession()


@app.route('/')
def index():
    if not session.get('logged_in'):
        return render_template('index.html', version=settings.VERSION)
    else:
        action = request.args.get('action')
        return render_template('dashboard.html', show_add_onload=action, version=settings.VERSION)


@app.route('/login', methods=['POST'])
def login():
    if request.form['password'] == settings.PASSWORD and request.form['username'] == settings.USERNAME:
        session['logged_in'] = True
        session['username'] = request.form['username']
    else:
        flash('wrong username or password!')
        return redirect(url_for('index'))
    return index()


@app.route('/logout')
def logout():
    session['logged_in'] = False
    return index()


@app.route('/carriers')
def displayCarriers(db_err=0):
    if session.get('logged_in'):
        try:
            res = db.query(Gateways).filter(Gateways.type == settings.FLT_CARRIER).all()
            return render_template('carriers.html', rows=res)
        except:
            db.rollback()
            if db_err <= 3:
                # Try again
                db_err = db_err + 1
                print(db_err)
                return displayCarriers(db_err);
            else:
                return render_template('error.html', type="db")
    else:
        return index()


@app.route('/carriers', methods=['POST'])
def addUpdateCarriers():
    gwid = request.form['gwid']
    name = request.form['name']
    ip_addr = request.form['ip_addr']
    strip = request.form['strip']
    prefix = request.form['prefix']
    # Adding
    if len(gwid) <= 0:
        print(name)
        Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_CARRIER)
        Addr = Address(name, ip_addr, 32, settings.FLT_CARRIER)

        try:
            db.add(Gateway)
            db.add(Addr)
            db.commit()
            return displayCarriers()
        except:
            db.rollback()
            return render_template('error.html', type="db")
    # Updating
    else:
        db.query(Gateways).filter(Gateways.gwid == gwid).update(
            {'description': "name:" + name, 'address': ip_addr, 'strip': strip, 'pri_prefix': prefix})
        # TODO: You will end up with multiple Address records -will fix
        Addr = Address(name, ip_addr, 32, settings.FLT_CARRIER)
        try:
            db.add(Addr)
            db.commit()
            return displayCarriers()
        except:
            db.rollback()
            return render_template('error.html', type="db")


@app.route('/carrierdelete', methods=['POST'])
def deleteCarriers():
    gwid = request.form['gwid']
    name = request.form['name']
    d = db.query(Gateways).filter(Gateways.gwid == gwid)
    d.delete(synchronize_session=False)
    a = db.query(Address).filter(Address.tag == 'name:' + name)
    a.delete(synchronize_session=False)
    return displayCarriers()<commit_msg>Fixed the csv file so that each carrier contains a name: in the tags/notes column.  This is used to manage the Gateways Fixes #41<commit_after>from flask import Flask, render_template, request, redirect, abort, flash, session, url_for
from flask_script import Manager
import settings
from importlib import reload
from shared import *
from database import loadSession, Gateways, Address, InboundMapping, OutboundRoutes, Subscribers, dSIPFusionPBXDB, dSIPLCR
import os
import subprocess
import json

app = Flask(__name__, static_folder="./static", static_url_path="/static")
app.debug = settings.DEBUG
# app.add_template_filter()
db = loadSession()


@app.route('/')
def index():
    if not session.get('logged_in'):
        return render_template('index.html', version=settings.VERSION)
    else:
        action = request.args.get('action')
        return render_template('dashboard.html', show_add_onload=action, version=settings.VERSION)


@app.route('/login', methods=['POST'])
def login():
    if request.form['password'] == settings.PASSWORD and request.form['username'] == settings.USERNAME:
        session['logged_in'] = True
        session['username'] = request.form['username']
    else:
        flash('wrong username or password!')
        return redirect(url_for('index'))
    return index()


@app.route('/logout')
def logout():
    session['logged_in'] = False
    return index()


@app.route('/carriers')
def displayCarriers(db_err=0):
    if session.get('logged_in'):
        try:
            res = db.query(Gateways).filter(Gateways.type == settings.FLT_CARRIER).all()
            return render_template('carriers.html', rows=res)
        except:
            db.rollback()
            if db_err <= 3:
                # Try again
                db_err = db_err + 1
                print(db_err)
                return displayCarriers(db_err);
            else:
                return render_template('error.html', type="db")
    else:
        return index()


@app.route('/carriers', methods=['POST'])
def addUpdateCarriers():
    gwid = request.form['gwid']
    name = request.form['name']
    ip_addr = request.form['ip_addr']
    strip = request.form['strip']
    prefix = request.form['prefix']
    # Adding
    if len(gwid) <= 0:
        print(name)
        Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_CARRIER)
        Addr = Address(name, ip_addr, 32, settings.FLT_CARRIER)

        try:
            db.add(Gateway)
            db.add(Addr)
            db.commit()
            return displayCarriers()
        except:
            db.rollback()
            return render_template('error.html', type="db")
    # Updating
    else:
        db.query(Gateways).filter(Gateways.gwid == gwid).update(
            {'description': "name:" + name, 'address': ip_addr, 'strip': strip, 'pri_prefix': prefix})
        # TODO: You will end up with multiple Address records -will fix
        Addr = Address(name, ip_addr, 32, settings.FLT_CARRIER)
        try:
            db.add(Addr)
            db.commit()
            return displayCarriers()
        except:
            db.rollback()
            return render_template('error.html', type="db")


@app.route('/carrierdelete', methods=['POST'])
def deleteCarriers():
    gwid = request.form['gwid']
    name = request.form['name']
    d = db.query(Gateways).filter(Gateways.gwid == gwid)
    d.delete(synchronize_session=False)
    a = db.query(Address).filter(Address.tag == 'name:' + name)
    a.delete(synchronize_session=False)
    db.commit()
    return displayCarriers()
{'fraction_non_alphanumeric': 0.13083978217254227, 'fraction_numerical': 0.0025795356835769563, 'mean_word_length': 2.8367234744365035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4777900', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2061, 'n_words': 477}