def init_users(): "填充测试用户数据" admin = User(name="admin", password="******", isAdmin=True) normal = User(name="normal", password="******") simple = User(name="simple") USER_UTIL.add_all([admin, normal, simple])
def setUp(self): self.connection = connect_test() drop_db(self.connection) create_db(self.connection) user = User(name=self.name1, min_=0, max_=100, current=50) add_user(self.connection, user) user = User(name=self.name2, min_=0, max_=20, current=10) add_user(self.connection, user) self.connection.commit() self.connection2 = connect_test()
def sign_up(): if current_user.is_authenticated: return redirect('/home') form = sign_up_form() session = db.getSession(engine) if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data, name=form.name.data) user.set_password(form.password.data) session.add(user) session.commit() flash('Congratulations, you are now a registered user!') return redirect('/') return render_template('sign_up.html', title='Register', form=form)
def insert(conn: connection, user: User): with conn.cursor() as cursor: cursor.execute( "INSERT INTO USERS (name, min_amount, max_amount, current_amount) VALUES (%s, %s, %s, %s) RETURNING id", [user.name, user.min_, user.max_, user.current] ) user.id_ = cursor.fetchone()[0]
def test_insert(self): user = User( name="User1", min_=0, max_=100, current=10 ) add_user(self.connection, user) self.assertIsNotNone(user.id_) user2 = get_user(self.connection, user.name) self.assertEqual(user, user2)
def get(conn: connection, name: str): with conn.cursor() as cursor: cursor.execute( "SELECT id, name, min_amount, max_amount, current_amount FROM USERS WHERE name = %s", [name] ) row = cursor.fetchone() if not row: return None user = User(id_=row[0], name=row[1], min_=row[2], max_=row[3], current=row[4]) return user