def seed_users(num_of_users): """ Seed the database with users. :param num_of_users: Number of users to seed the database with, default is 50. """ print('Collecting users...') try: data = requests.get( 'https://randomuser.me/api/?' f'results={num_of_users}' '&inc=name,email,login,picture,dob,location,nat' ).json() perms = Permission.query.all() print('Saving to database...') for user in data.get('results'): u = User(password='******') u.username = user.get('login')['username'] u.email = user.get('email') u.created_on = random_timestamp( datetime.datetime(2018, 7, 1), datetime.datetime(2019, 2, 28)) u.profile = Profile() u.profile.name = user.get('name')['first'] + \ ' ' + user.get('name')['last'] u.profile.avatar = user.get('picture')['thumbnail'] u.profile.dob = user.get('dob')['date'] u.profile.bio = f"Hi, I am {user.get('name')['first']} \ from {user.get('location')['city']} \ {user.get('location')['state']}, {user.get('nat')}. \ I am a {user.get('dob')['age']} yrs old, who likes \ to tell jokes." u.profile.created_on = random_timestamp( datetime.datetime(2019, 1, 1), datetime.datetime(2019, 6, 30)) u.permissions.extend(random.sample( perms, k=random.randrange(len(perms)))) u.save() print('Setting up followers/following...') users = User.query.all() for user in users: following = random.sample(users, k=random.randrange(len(users))) user.followed.extend(following) user.save() print(f'Users table seeded with {num_of_users} users...') except exc.IntegrityError as error: db.session.rollback() print(f'Error: {error}')
def add_user(): request_data = request.get_json() if not request_data: return bad_request("No input data provided") try: data = AuthSchema().load(request_data) email = data.get('email') password = data.get('password') name = data.get('name') username = data.get('username') # check for existing user user = User.query.filter((User.email == email) | (User.username == username)).first() if user is not None: return bad_request('That user already exists.') # add new user to db profile = Profile(name=name) user = User(password=password) user.email = email user.username = username user.is_active = data.get('is_active') or False user.is_admin = data.get('is_admin') or False user.profile = profile user.save() response = jsonify(UserSchema().dump(user)) response.status_code = 201 response.headers['Location'] = url_for('admin.get_user', id=user.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_user(name, email, username='', avatar='', bio='', is_admin=False, is_active=True, permissions=[]): profile = Profile(name=name, bio=bio) profile.avatar = avatar or profile.set_avatar(email) user = User(password='******') user.email = email user.username = username user.is_admin = is_admin user.is_active = is_active user.add_permissions(permissions) user.profile = profile user.save() return user