def setUp(self, **extra_config): """Set up the testing client and database connection. self.api will be a flask TestClient to make requests self.db will be a MongoDB database """ super().setUp() # In 3.2, assertItemsEqual was replaced by assertCountEqual # Make assertItemsEqual work in tests for py3 as well if sys.version_info >= (3, 2): self.assertItemsEqual = self.assertCountEqual # create eve app and test client config = {} config.update(self.test_config) config.update(extra_config) self.app = bootstrap.create_app(**config) self.app.response_class = TestResponse self.app.test_client_class = TestClient self.app.test_mails = [] self.api = self.app.test_client() # Create a separate mongo connection and db reference for tests self.connection = MongoClient(host=self.app.config['MONGO_HOST'], port=self.app.config['MONGO_PORT']) self.db = self.connection[self.app.config['MONGO_DBNAME']] self.db.authenticate(name=self.app.config['MONGO_USERNAME'], password=self.app.config['MONGO_PASSWORD'], source=self.app.config['MONGO_DBNAME'])
def cron(config, continuous): """Run scheduled tasks. Use --continuous to keep running and execute tasks periodically. """ app = create_app(config_file=config) if not continuous: run_cron(app) else: interval = app.config['CRON_INTERVAL'] echo('Running scheduled tasks periodically (every %i seconds).' % interval.total_seconds()) while True: checkpoint = dt.utcnow() run_cron(app) execution_time = dt.utcnow() - checkpoint echo('Tasks executed, total execution time: %.3f seconds.' % execution_time.total_seconds()) if execution_time > interval: echo('Warning: Execution time exceeds interval length.') sleep((interval - execution_time).total_seconds())
def recreate_mailing_lists(config): """(Re-)create mailing lists for all groups. 1. Delete all mailing list files. 2. Create new mailing list files. For every group, we call the update_group function for this """ app = create_app(config_file=config) directory = app.config.get('MAILING_LIST_DIR') prefix = app.config['MAILING_LIST_FILE_PREFIX'] if not directory: echo('No directory for mailing lists specified in config.') return # Delete existing files if isdir(directory): for filename in listdir(directory): if filename.startswith(prefix): remove(join(directory, filename)) # Create new files with app.app_context(): groups = app.data.driver.db['groups'].find({}) for g in groups: updated_group(g, g) # Use group as update and original
def run(config, mode): """Run production/development server. Two modes of operation are available: - dev: Run a development server - prod: Run a production server (requires the `bjoern` module) """ app = create_app(config_file=config, DEBUG=True, TESTING=True) if mode == 'dev': app.run(threaded=True) elif mode == 'prod': if bjoern: echo('Starting bjoern on port 8080...') bjoern.run(create_app(), '0.0.0.0', 8080) else: raise ClickException('The production server requires `bjoern`, ' 'try installing it with ' '`pip install bjoern`.')
def run(config, mode): """Run production/development server. Two modes of operation are available: - dev: Run a development server - prod: Run a production server (requires the `bjoern` module) """ if mode == 'dev': app = create_app(config_file=config, ENV='development', DEBUG=True, TESTING=True) app.run(threaded=True) elif mode == 'prod': if bjoern: echo('Starting bjoern on port 8080...') bjoern.run(create_app(config_file=config), '0.0.0.0', 8080) else: raise ClickException('The production server requires `bjoern`, ' 'try installing it with ' '`pip install bjoern`.')
def ldap_sync(config, sync_all, nethz): """Synchronize users with eth ldap. Examples: amivapi ldap_sync --all amivapi ldap_sync adietmue bconrad blumh """ app = create_app(config_file=config) if not app.config['ldap_connector']: echo("LDAP is not enabled, can't proceed!") else: with app.test_request_context(): if sync_all: res = ldap.sync_all() echo("Synchronized %i users." % len(res)) else: for user in nethz: if ldap.sync_one(user) is not None: echo("Successfully synchronized '%s'." % user) else: echo("Could not synchronize '%s'." % user)
def run(config): """Start amivapi development server.""" app = create_app(config_file=config, DEBUG=True, TESTING=True) app.run(threaded=True)
def cron(config): """Run scheduled tasks.""" app = create_app(config_file=config) with app.app_context(): run_scheduled_tasks()