def seed_groups(): """ Seed the database with a few groups. """ try: groups = [ 'partner', 'junior partner', 'senior associate', 'associate', 'paralegal' ] users = User.query.limit(24).all() perms = Permission.query.all() for group in groups: grp = Group(name=group) grp.members.extend(random.sample( users, k=random.randrange(len(users)))) grp.permissions.extend(random.sample( perms, k=random.randrange(len(perms)))) grp.save() print(f'Added users to {len(groups)} groups...') except Exception as error: print(f'Error: {error}')
def add_group(): request_data = request.get_json() if not request_data: return bad_request('No input data provided.') try: data = GroupSchema().load(request_data) name = data.get('name') description = data.get('description') # check for existing group name group = Group.find_by_name(name=name) if group: return bad_request('Group already exist.') group = Group(name=name, description=description) group.save() response = jsonify(GroupSchema().dump(group)) response.status_code = 201 response.headers['Location'] = url_for('admin.get_group', id=group.id) return response # handle errors except ValidationError as err: return error_response(422, err.messages) except (exc.IntegrityError, ValueError): db.session.rollback() return server_error('Something went wrong, please try again.')
def add_group(name, description='', members=[], permissions=[]): group = Group(name=name, description=description) group.add_members(members) group.add_permissions(permissions) group.save() return group