def init(): # create the database db.configure_mappers() db.create_all() # create the super user u = User(name=settings.SUPER_USER, email=settings.SUPER_USER_EMAIL, password=settings.SUPER_USER_PASSWORD, admin=True, super_user=True) # optionally add super user apikey if getattr(settings, 'SUPER_USER_APIKEY', None): u.apikey = settings.SUPER_USER_APIKEY db_session.add(u) # load sql extensions + functions for sql in load_sql(): db_session.execute(sql) # load built-in sous-chefs for sc in load_sous_chefs(): sc = sous_chef_schema.validate(sc) s = SousChef(**sc) db_session.add(s) # commit db_session.commit()
def org_create_user(user, org_id_slug): if not user.admin: raise AuthError('You must be an admin to create a user for an Org.') # get the form. req_data = request_data() email = req_data.get('email') password = req_data.get('password') name = req_data.get('name') admin = req_data.get('admin', False) if not all([email, password, name]): raise RequestError( 'An email, password, and name are required to create a User.') # fetch org org = fetch_by_id_or_field(Org, 'slug', org_id_slug) # if it still doesn't exist, raise an error. if not org: raise NotFoundError('This Org does not exist.') # localize localize(org) # ensure the active user can edit this Org if user.id not in org.user_ids: raise ForbiddenError("You are not allowed to access this Org.") if User.query.filter_by(email=email).first(): raise RequestError( 'A User with email "{}" already exists'.format(email)) if not mail.validate(email): raise RequestError('{} is an invalid email address.'.format(email)) new_org_user = User(email=email, password=password, name=name, admin=admin) org.users.append(new_org_user) db.session.commit() return jsonify(new_org_user)
def org(name=settings.SUPER_USER_ORG, timezone=settings.SUPER_USER_ORG_TIMEZONE, email=settings.SUPER_USER_EMAIL): # create the org and super user org = Org.query.filter_by(name=name).first() if not org: log.info('Creating org: "{}"'.format(name)) org = Org(name=name, timezone=timezone) else: log.warning('Updating org: "{}"'.format(name)) org.timezone = timezone org.name = name org.slug = slug(unicode(name)) # create the super user and add to the org. u = User.query.filter_by(email=email).first() if not u: log.info('Creating super user: "******"'.format(email)) u = User(name=settings.SUPER_USER, email=settings.SUPER_USER_EMAIL, password=settings.SUPER_USER_PASSWORD, admin=True, super_user=True) u.apikey = settings.SUPER_USER_APIKEY else: log.warning('Updating super user: "******"'.format(email)) u.apikey = settings.SUPER_USER_APIKEY u.email = settings.SUPER_USER_EMAIL u.password = settings.SUPER_USER_PASSWORD u.admin = True u.super_user = True org.users.append(u) db.session.add(org) db.session.commit() tags(org) sous_chefs(org) recipes(org) return org
def org( name=settings.SUPER_USER_ORG, timezone=settings.SUPER_USER_ORG_TIMEZONE, email=settings.SUPER_USER_EMAIL): # create the org and super user org = Org.query.filter_by(name=name).first() if not org: log.info('Creating org: "{}"'.format(name)) org = Org(name=name, timezone=timezone) else: log.warning('Updating org: "{}"'.format(name)) org.timezone = timezone org.name = name org.slug = slug(unicode(name)) # create the super user and add to the org. u = User.query.filter_by(email=email).first() if not u: log.info('Creating super user: "******"'.format(email)) u = User(name=settings.SUPER_USER, email=settings.SUPER_USER_EMAIL, password=settings.SUPER_USER_PASSWORD, admin=True, super_user=True) u.apikey = settings.SUPER_USER_APIKEY else: log.warning('Updating super user: "******"'.format(email)) u.apikey = settings.SUPER_USER_APIKEY u.email = settings.SUPER_USER_EMAIL u.password = settings.SUPER_USER_PASSWORD u.admin = True u.super_user = True org.users.append(u) db.session.add(org) db.session.commit() tags(org) sous_chefs(org) recipes(org) return org
def org_add_user(user, org_id_slug, user_email): if not user.admin: raise AuthError( 'You must be an admin to add a user to an Org.') # fetch org org = fetch_by_id_or_field(Org, 'slug', org_id_slug) if not org: raise NotFoundError( 'This Org does not exist.') # ensure the active user can edit this Org if user.id not in org.user_ids: raise ForbiddenError( 'You are not allowed to edit this Org.') # localize localize(org) # get this new user by id / email new_org_user = fetch_by_id_or_field(User, 'email', user_email) # get the form. req_data = request_data() email = req_data.get('email') name = req_data.get('name') admin = req_data.get('admin', False) password = req_data.get('password') if email and not mail.validate(email): raise RequestError( '{} is an invalid email address.' .format(email)) # insert if not new_org_user: if not all([email, password, name]): raise RequestError( 'An email, password, and name are required to create a User.') new_org_user = User( email=email, password=password, name=name, admin=admin) org.users.append(new_org_user) db.session.add(org) # ensure the active user can edit this Org elif new_org_user.id not in org.user_ids: raise ForbiddenError( "You are not allowed to access this Org.") # update if name: new_org_user.name = name if email: new_org_user.email = email if admin: new_org_user.admin = admin if password: new_org_user.set_password(password) new_org_user.admin = admin db.session.add(new_org_user) db.session.commit() return jsonify(new_org_user)
def org_add_user(user, org_id, user_email): if not user.admin: raise AuthError('You must be an admin to add a user to an Org.') # fetch org org = fetch_by_id_or_field(Org, 'slug', org_id) if not org: raise NotFoundError('Org {} does not exist.'.format(org_id)) # ensure the active user can edit this Org if user.id not in org.user_ids: raise ForbiddenError('You are not allowed to edit this Org.') # localize localize(org) # get this new user by id / email new_org_user = fetch_by_id_or_field(User, 'email', user_email) # get the form. req_data = request_data() email = req_data.get('email') name = req_data.get('name') admin = req_data.get('admin', False) password = req_data.get('password') if email and not mail.validate(email): raise RequestError('{} is an invalid email address.'.format(email)) # insert if not new_org_user: if not all([email, password, name]): raise RequestError( 'An email, password, and name are required to create a User.') new_org_user = User(email=email, password=password, name=name, admin=admin) org.users.append(new_org_user) db.session.add(org) # ensure the active user can edit this Org elif new_org_user.id not in org.user_ids: raise ForbiddenError("You are not allowed to access this Org.") # update if name: new_org_user.name = name if email: new_org_user.email = email if admin: new_org_user.admin = admin if password: new_org_user.set_password(password) new_org_user.admin = admin db.session.add(new_org_user) db.session.commit() return jsonify(new_org_user)
def run(opts, **kwargs): # create the database try: with app.app_context(): echo('Creating database "{}"'.format(settings.SQLALCHEMY_DATABASE_URI), no_color=opts.no_color) db.configure_mappers() db.create_all() # create the super user u = User.query.filter_by(email=settings.SUPER_USER_EMAIL).first() if not u: echo('Creating super user "{}"'.format(settings.SUPER_USER_EMAIL), no_color=opts.no_color) u = User(name=settings.SUPER_USER, email=settings.SUPER_USER_EMAIL, password=settings.SUPER_USER_PASSWORD, admin=True, super_user=True) # optionally add super user apikey if getattr(settings, 'SUPER_USER_APIKEY', None): u.apikey = settings.SUPER_USER_APIKEY else: echo('Updating super user "{}"'.format(settings.SUPER_USER_EMAIL), no_color=opts.no_color) u.name=settings.SUPER_USER, u.email=settings.SUPER_USER_EMAIL, u.password=settings.SUPER_USER_PASSWORD, u.admin=True super_user=True db.session.add(u) echo('(Re)Loading SQL Extensions', no_color=opts.no_color) # load sql extensions + functions for sql in load_sql(): db.session.execute(sql) # load built-in sous-chefs for sc in load_sous_chefs(): sc = sous_chef_schema.validate(sc) sc_obj = db.session.query(SousChef).filter_by(slug=sc['slug']).first() if not sc_obj: echo('Importing Sous Chef "{}"'.format(sc['slug']), no_color=opts.no_color) sc_obj = SousChef(**sc) else: echo('Updating Sous Chef "{}"'.format(sc['slug']), no_color=opts.no_color) sc = sous_chef_schema.update(sc_obj.to_dict(), sc) # udpate for name, value in sc.items(): setattr(sc_obj, name, value) db.session.add(sc_obj) # commit db.session.commit() db.session.close() except Exception as e: db.session.rollback() db.session.close() raise e