def add_user(firstname='', lastname='', username='', email='', is_admin=False, is_active=True): p = Profile(firstname=firstname, lastname=lastname) p.username = username or p.set_username() p.avatar = p.set_avatar(email) user = User(email=email, password='******', is_admin=is_admin, is_active=is_active, profile=p) user.save() return user
def register_user(): request_data = request.get_json() if not request_data: return bad_request("No input data provided") try: data = AuthSchema().load(request_data) except ValidationError as err: return error_response(422, err.messages) email = data.get('email') password = data.get('password') firstname = data.get('firstname').title() lastname = data.get('lastname').title() try: # check for existing user user = User.query.filter(User.email == email).first() if user: return bad_request('Sorry. That user already exists.') # add new user to db profile = Profile(firstname=firstname, lastname=lastname) profile.username = profile.set_username() profile.avatar = profile.set_avatar(email) user = User(email=email, password=password, profile=profile) user.save() response = jsonify({ 'token': user.encode_auth_token(user.id).decode(), }) response.status_code = 201 response.headers['Location'] = url_for('auth.get_user', id=user.id) return response # handle errors except (exc.IntegrityError, ValueError): db.session.rollback() return error_response(500, 'Something went wrong, please try again.')
def register_user(): post_data = request.get_json() if not post_data: return bad_request("No input data provided") try: data = AuthSchema(partial=True).load(post_data) except ValidationError as err: return error_response(422, err.messages) name = data.get('name') username = data.get('username') email = data.get('email') password = data.get('password') # check for existing user user = User.query.filter(User.email == email).first() if user: return bad_request('That user already exists.') profile = Profile() profile.name = name profile.username = username profile.avatar = profile.set_avatar(email) user = User(password=password) user.email = email user.profile = profile try: user.save() except (exc.IntegrityError, ValueError): db.session.rollback() return server_error('Something went wrong, please try again.') response = jsonify({'token': user.encode_auth_token()}) response.status_code = 201 response.headers['Location'] = url_for('users.get_user', id=user.id) return response