def test_delete_user(self): """ensure that user is successfully deleted""" user1 = User(username=rnd_dummy_data(), email=rnd_dummy_data(), password='******') user2 = User(username=rnd_dummy_data(), email=rnd_dummy_data(), password='******') user3 = User(username=rnd_dummy_data(), email=rnd_dummy_data(), password='******') db.session.add(user1) db.session.add(user2) db.session.add(user3) db.session.commit() length_before = len(User.query.all()) del_user = User.query.all()[0] db.session.delete(del_user) db.session.commit() length_after = len(User.query.all()) self.assertEqual(length_after, length_before - 1)
def post(self, args): user = User.user_exists(args['username'], args['email']) if user: response = { "message": "User with that username or email already exists" } return make_response(jsonify(response), 409) user = User(username=args['username'], email=args['email'], password=args['password']) message = user.create_user() response = {"message": message} return make_response(jsonify(response), 201)
def reset_password(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_reset_token(token) if not (user): flash('The Token Expired or token is invalid', 'warning') return redirect(url_for('users.reset_password_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your password has been successfully updated!You can login now.', 'success') return redirect(url_for('users.login')) form.email.data = user.email return render_template('reset_password.htm', title='Reset Password', form=form)
def test_form_valid( self, user: User, request_factory: RequestFactory ): form_data = {"name": "John Doe"} request = request_factory.post( reverse("users:update"), form_data ) request.user = user session_middleware = SessionMiddleware() session_middleware.process_request(request) msg_middleware = MessageMiddleware() msg_middleware.process_request(request) response = UserUpdateView.as_view()(request) user.refresh_from_db() assert response.status_code == 302 assert user.name == form_data["name"]
def get(self): users = User.get_users() if users: users_schema = UserSchema(many=True) users_serailizer = users_schema.dump(users) response = {"users": users_serailizer} else: response = {"message": "No users created yet"} return make_response(jsonify(response), 200)
def admin(): if not current_user.username == 'admin': abort(403) all_users = User.all_users() return render_template('admin/admin.html', all_users=all_users, all_followers_of_user=all_followers_of_user, all_posts_by_author=Post.all_posts_by_author)
def get(self, user_id): user = User.get_user(user_id) if user: user_schema = UserSchema() user_serializer = user_schema.dump(user) response = {"user": user_serializer} return make_response(jsonify(response), 200) else: response = {"message": "User not found"} return make_response(jsonify(response), 404)
def registration_data(form): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=str((form.username.data).capitalize()), email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() return user
def test_add_users(self): """ensure that user is successfully created""" length_before = len(User.query.all()) user1 = User(username=rnd_dummy_data(), email=rnd_dummy_data(), password='******') user2 = User(username=rnd_dummy_data(), email=rnd_dummy_data(), password='******') user3 = User(username=rnd_dummy_data(), email=rnd_dummy_data(), password='******') db.session.add(user1) db.session.add(user2) db.session.add(user3) db.session.commit() length_after = len(User.query.all()) self.assertEqual(length_after, length_before + 3)
def user(username): # user wall view function post_host = User.query.filter_by(username=username).first() # wall host all_users = User.all_users() # list of all FlaskBlog users d = {} user_ids = [] # list of all user IDs for user in all_users: user_ids.append(user.id) for user_id in user_ids: d[user_id] = all_followers_of_user( user_id) # a dictionary {user_id: [list of followers of user_id]} if request.method == 'POST': new_post_post_author_id = post_host.id new_post_post_title = request.form['post_title'] new_post_post_content = request.form['post_content'] new_post_date_added = datetime.datetime.utcnow() new_post = Post(post_author_id=new_post_post_author_id, post_author_username=post_host.username, post_title=new_post_post_title, post_content=new_post_post_content, post_date_added=new_post_date_added) db.session.add(new_post) db.session.commit() return redirect(url_for( 'user', username=username)) # redirect to the user wall page all_posts = Post.all_posts_by_author( post_host.id) # all posts of the wall host user all_followers = all_followers_of_user( post_host.id) # all followers of the wall host user return render_template('user.html', post_host=post_host, all_posts=all_posts, d=d, all_users=all_users, all_followers=all_followers)
def test_user_get_absolute_url(user: User): assert user.get_absolute_url() == f"/users/{user.username}/"
import datetime from .models import Post, Like from blog.comments.models import Comment from blog.users.models import User from blog import app, db from flask import redirect, render_template, request, flash, url_for, abort from flask_login import login_required, current_user from blog.users.routes import all_followers_of_user all_users = User.all_users() # list of all FlaskBlog users user_ids = [user.id for user in all_users] # list of user ids all FlaskBlog users flwrs_dict = {usr: all_followers_of_user(usr) for usr in user_ids} # dict {user_id: [followers of user_id]} def get_user_by_post_id(id): # returns the "host" of a post return db.session.query(User).select_from(User).join(Post).filter( User.id == Post.post_author_id).first() @app.route('/users/<username>/wall', methods=['POST', 'GET']) @login_required def user(username): # user wall view function post_host = User.query.filter_by(username=username).first() # wall host all_users = User.all_users() # list of all FlaskBlog users d = {}
def mutate(self, info, **kwargs): user = UserModel(**kwargs) user.create_user() return CreateUser(user=user)