Ejemplo n.º 1
0
def list_hosts():
    click.echo("List of hosts:")
    with app.app_context():
        results = Ping.query
        if results.count() == 0:
            click.echo("** Empty **")
        else:
            for ping in results:
                click.echo(" - %s" % ping.hostname)
Ejemplo n.º 2
0
def test_app():
    '''Uses ``app`` imported from flask_app to create a testable Flask
    application.

    :yield: Flask application with a context, ready for testing
    '''
#    global app                         # Uses global variable "app"
    app.config['TESTING'] = True
    test_app = app.test_client() 
    ctx = app.app_context()
    ctx.push()
    yield test_app 
    ctx.pop()
Ejemplo n.º 3
0
def create_order(ctx, hostname, config):
    click.echo("Creating order for hostname %s ..." % hostname)

    if config not in os.listdir(AGENT_CONFIGURATION_DIR):
        click.echo("The configuration file did not exist.")
        ctx.exit(-1)

    with app.app_context():
        if not Ping.query.filter_by(hostname=hostname).first():
            click.echo("The hostname %s was not referenced in DB." % hostname)
            ctx.exit(-1)

        results = Order.query.filter_by(hostname=hostname)
        order = results.first()

        if order is not None:
            if order.active:
                click.echo("An order for %s was already exist and was activated." % hostname)
            else:
                order.active = True
                order.config_filename = config
                DB.session.add(order)
                try:
                    DB.session.commit()
                    click.echo("An order for %s was already exist and has been updated." % hostname)
                except:
                    click.echo("Failed to update order for %s." % hostname)
                    ctx.exit(-1)

        else:
            order = Order(hostname=hostname, active=True, config_filename=config)
            DB.session.add(order)

            try:
                DB.session.commit()
                click.echo("A new order has been created for the hostname %s." % hostname)
            except:
                click.echo(
                    "Unable to create a new order for the hostname %s with configuration file %s." % (
                        hostname,
                        config
                    )
                )
                ctx.exit(-1)
Ejemplo n.º 4
0
def update_queues(arg):
    """Update queue related stuff."""
    with app.app_context():
        finished_entries()
Ejemplo n.º 5
0
import sys
sys.path.append('..')

from server.app import app, db
from server.models.Crop import Crop
from server.models.Record import Record

with app.app_context():
    db.create_all()
def insert_units(units_filename):
    with open(units_filename) as f:
        for unit_line in f:
            code, name = map(str.strip, unit_line.split(',', 1))
            unit = Unit(code,name) 
            db.session.add(unit)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Insert initial data from spreadsheets')

    parser.add_argument('db_uri', help='Database URI to insert to (e.g. sqlite:///test.db)')
    parser.add_argument('data_dir', help='Directory with initial data')
    args = parser.parse_args()

    app.config['SQLALCHEMY_DATABASE_URI'] = args.db_uri 

    with app.app_context():
        db.drop_all()
        db.create_all()

        units_filename = os.path.join(args.data_dir, 'units.txt')
        insert_units(units_filename)
        db.session.commit()

        spreadsheets = glob.glob(os.path.join(args.data_dir, '*.xlsx')) 
        for workbook in spreadsheets:
            print('Processing ' + workbook)
            process_workbook(workbook)
            db.session.commit()

Ejemplo n.º 7
0
 def __init__(self, *mixed, **kwargs):
     super(User, self).__init__(*mixed, **kwargs)
     with app.app_context():
         self.password = hash_password(kwargs['password'])