def create_database(argv): set_app(config.ProductiveConfig) app.app_context().push() db.create_all() db.session.commit() rank1 = Rank(name='Member', debt_limit=-2000) rank2 = Rank(name='Alumni', debt_limit=-2000) rank3 = Rank(name='Contender', debt_limit=0) rank4 = Rank(name='Inactive', debt_limit=0, active=False) try: for r in (rank1, rank2, rank3, rank4): db.session.add(r) db.session.commit() except IntegrityError: os.remove(config.ProductiveConfig.DATABASE_PATH) sys.exit('ERROR: Could not create database!') # Handle the user. try: opts, _ = getopt.getopt(argv, 'f:l:p:') for opt, arg in opts: if opt == '-f': firstname = arg elif opt == '-l': lastname = arg elif opt == '-p': password = arg except getopt.GetoptError: firstname, lastname, password = input_user() user = { 'firstname': firstname, 'lastname': lastname, 'password': password, 'password_repeat': password } try: insert_user(user) except exc.PasswordTooShort: os.remove(config.ProductiveConfig.DATABASE_PATH) sys.exit(('ERROR: Password to short. Needs at least {} characters.' + ' Aborting setup.').format( config.BaseConfig.MINIMUM_PASSWORD_LENGTH)) # Get the User user = User.query.filter_by(id=1).first() # Add User as Admin (is_admin, admin_id) user.set_admin(True, 1) # Verify the user (admin_id, rank_id) user.verify(1, 1) db.session.commit()
def setUp(self): # Create tables db.create_all() db.session.commit() # Create test client self.client = app.test_client() self.bcrypt = bcrypt # Insert default data self.insert_default_users() self.insert_first_admin() self.verify_all_users_except_last() self.insert_default_ranks() self.insert_default_tags() self.insert_default_products()
'that you renamed or copied the sample configuration ' 'configuration.example.py and adapted it to your needs.') import argparse from shopdb.api import app, db, set_app from dev import insert_dev_data from flask_cors import CORS parser = argparse.ArgumentParser(description='Starting script shop.db') parser.add_argument('--mode', choices=['development', 'local']) args = parser.parse_args() if args.mode == 'development': print('Starting shop-db in developing mode') set_app(config.DevelopmentConfig) app.app_context().push() db.create_all() insert_dev_data(db) elif args.mode == 'local': print('Starting shop-db on the local database') set_app(config.ProductiveConfig) CORS(app, expose_headers='*') app.app_context().push() else: parser.print_help() sys.exit(f'{args.mode}: invalid operating mode') app.run(host=app.config['HOST'], port=app.config['PORT'])