def test_get_by_id(self): """Get user by ID.""" user = User("foo", "*****@*****.**", "123") user.save() retrieved = User.get_by_id(user.id) assert retrieved == user
def user_del(id): try: user = User.get_by_id(id) user.delete() except Exception as e: return ApiResult({"r": 1, "msg": str(e)}) return ApiResult(dict())
def create_admin(): user = User.create( username="******", email="*****@*****.**", password="******", is_active=True ) create_fake_address(user.id) create_fake_address(user.id) create_fake_address(user.id) UserRole.create(user_id=user.id, role_id=4) yield f"Admin {user.username} created" user = User.create(username="******", email="*****@*****.**", password="******", is_active=True) UserRole.create(user_id=user.id, role_id=3) yield f"Admin {user.username} created" user = User.create( username="******", email="*****@*****.**", password="******", is_active=True ) UserRole.create(user_id=user.id, role_id=2) yield f"Admin {user.username} created"
def create_fake_user(): email = get_email(fake.first_name(), fake.last_name()) user, _ = User.get_or_create( username=fake.first_name() + fake.last_name(), email=email, password="******", is_active=True, ) return user
def load_user_from_request(request): token = request.headers.get("Authorization") try: user_id = verify_token(token) user = User.get_by_id(user_id) if user: return user except: return None
def create_admin(): user = User.create(username="******", email="localhost", password=os.getenv('DB_PASSWD', '123456'), is_active=True) # create_fake_address(user.id) # create_fake_address(user.id) # create_fake_address(user.id) UserRole.create(user_id=user.id, role_id=4) yield f"Admin {user.username} created"
def user_edit(user_id): user = User.get_by_id(user_id) form = UserForm(obj=user) if form.validate_on_submit(): if not form.password.data: del form.password form.populate_obj(user) user.save() return redirect(url_for("dashboard.user", user_id=user_id)) return render_template("user/edit.html", form=form)
def post(self): args = parser.parse_args() res = requests.get(WECHAT_LOGIN_URL.format(WECHAT_APP_ID, WECHAT_APP_SECRET, args['code'])).json() open_id, session_key = res['openid'], res['session_key'] user = User.query.filter_by(open_id=open_id).first() if not user: user = User.create(username=open_id, email=open_id, password=open_id, open_id=open_id, session_key=session_key) data = { 'token': generate_token(user.id).decode(), 'cart_lines': len(user.cart.lines) } return data
def post(self): args = parser.parse_args() res = requests.get( WECHAT_LOGIN_URL.format(WECHAT_APP_ID, WECHAT_APP_SECRET, args["code"])).json() open_id, session_key = res["openid"], res["session_key"] user = User.query.filter_by(open_id=open_id).first() if not user: user = User.create( username=open_id, email=open_id, password=open_id, open_id=open_id, session_key=session_key, ) data = { "token": generate_token(user.id).decode(), "cart_lines": len(user.cart.lines), } return data
def user_edit(user_id): user = User.get_by_id(user_id) form = UserForm(obj=user) if form.validate_on_submit(): if not form.password.data: del form.password form.populate_obj(user) user.save() selected_role = request.form.get(form.role.label.text) if selected_role != '0': selected_role = Role.query.filter( Role.name == selected_role).first() user_role = UserRole.query.filter( UserRole.user_id == current_user.id, UserRole.role_id >= selected_role.id).first() user_role = Role.query.filter(Role.id == user_role.role_id).first() if selected_role.permissions > user_role.permissions: flash('You have no access rights', "warning") else: UserRole.query.filter(UserRole.user_id == user.id).delete() UserRole.create(user_id=user.id, role_id=selected_role.id) return redirect(url_for("dashboard.user", user_id=user_id)) return render_template("user/edit.html", form=form)
def user(self): return User.get_by_id(self.user_id)
def user(user_id): user = User.get_by_id(user_id) addresses = user.addresses orders = Order.get_user_orders(user_id) context = {"user": user, "addresses": addresses, "orders": orders} return render_template("user/detail.html", **context)
def load_user(user_id): """Load user by ID.""" return User.get_by_id(int(user_id))
def test_check_password(self): """Check password.""" user = User.create(username="******", email="*****@*****.**", password="******") assert user.check_password("foobarbaz123") is True assert user.check_password("barfoobaz") is False
def test_created_at_defaults_to_datetime(self): """Test creation date.""" user = User(username="******", email="*****@*****.**", password="******") user.save() assert bool(user.created_at) assert isinstance(user.created_at, dt.datetime)