def register(): # db.drop_all() # db.create_all() # 只有在数据库模型修改时使用 if request.method == 'POST': user_name = request.form['username'] passport = request.form['passport'] passport_check = request.form['passport-check'] name_pattern = re.compile(r"\w{4,8}") # 逗号后面不需要空格 pass_pattern = re.compile(r".{6,20}") if passport_check != passport: flash("两次输入密码不匹配") elif name_pattern.match(user_name) == None: flash("用户名为4~8位数字字母") elif pass_pattern.match(passport) == None: flash("密码为6~20位") elif User.query.filter_by(username="******").all(): flash("该用户名已存在") else: temp_user = User(username=user_name) temp_user.set_password(passport) db.session.add(temp_user) db.session.commit() flash("注册成功") return redirect(url_for('login')) print("Test!") return render_template("SignIn.html")
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] confirmpassword = request.form['Confirmpassword'] user = User.query.filter_by(username=username).first() if user is not None: flash('register fail! this username already exsit!') return redirect(url_for('register')) if password != confirmpassword: flash('Two password entries are inconsistent!') return redirect(url_for('register')) user = User(username=username, name=username) user.set_password(password) db.session.add(user) db.session.commit() flash('register success!') # 如果验证失败,显示错误消息 return redirect(url_for('login')) # 重定向回登录页面 return render_template('register.html')
def forge(): """Generate fake data.""" db.create_all() name = 'He wei' username = '******' password = '******' movies = [ { 'title': 'My Neighbor Totoro', 'year': '1988' }, { 'title': 'Dead Poets Society', 'year': '1989' }, { 'title': 'A Perfect World', 'year': '1993' }, { 'title': 'Leon', 'year': '1994' }, { 'title': 'Mahjong', 'year': '1996' }, { 'title': 'Swallowtail Butterfly', 'year': '1996' }, { 'title': 'King of Comedy', 'year': '1999' }, { 'title': 'Devils on the Doorstep', 'year': '1999' }, { 'title': 'WALL-E', 'year': '2008' }, { 'title': 'The Pork of Music', 'year': '2012' }, ] user = User(name=name, username=username) user.set_password(password) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo('Done.')
def inject_vars(): # 函数名可以随意修改 """模板上下文处理函数""" from watchlist.models import User user = User.query.first() # 用户对象 if not user: user = User() user.name = 'BL00D' return locals() # 需要返回字典
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name='Test', username='******') user.set_password('123') movie = Movie(title='Test Movie Title', year='2019') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建测试客户端 self.runner = app.test_cli_runner() # 创建命令运行器
def teacherinfo(): if not current_user.is_authenticated: return redirect(url_for('index')) if current_user.isAdmin != 1: return redirect(url_for('index')) if request.method == 'POST': name = request.form['name'] username = request.form['username'] password = request.form['password'] validate_password = request.form['validate_password'] if not name or len(name) > 20: flash('姓名必须小于20位') return redirect(url_for('settings')) if not username or len(username) > 20: flash('用户名必须小于20位') return redirect(url_for('settings')) if not password or len(password) > 20: flash('密码必须小于20位') return redirect(url_for('settings')) if password != validate_password: flash('密码不一致') return redirect(url_for('settings')) user = User(name=name, username=username) user.set_password(password) db.session.add(user) count = db.session.commit() print("count", count) flash('添加成功!') return redirect(url_for('teacherinfo')) teacherlist = User.query.order_by(User.id.asc()).all() #瞎搞 result = db.session.query( Movie.teacherId, func.count(Movie.teacherId).label('count'), ).group_by(Movie.teacherId).all() student_count_dict = {} for item in result: student_count_dict[item[0]] = item[1] print(student_count_dict) return render_template('teacherinfo.html', teacherlist=teacherlist, student_count_dict=student_count_dict)
def admin(username, password): db.create_all() user = User.query.first() if user is not None: user.username = username user.set_password(password) else: user = User(username=username, name='admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo('Done')
def admin(username, password): db.create_all() if all([username, password ]) and not User.query.filter_by(username=username).first(): user = User(username=username) user.set_password(password) db.session.add(user) else: click.echo('未成功创建,原因可能是重复或者信息填写错误') exit() click.echo('创建管理员账号完成')
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name="Test", username="******") user.set_password("123") movie = Movie(title="Test Movie Title", year="2019") db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() self.runner = app.test_cli_runner()
def admin(username, password): user = User.query.first() if user is not None: click.echo("更新管理员账户") user.username = username user.set_password(password) else: click.echo("创建管理员账户") user = User(username=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo("管理员账号更新/创建完成")
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') # 创建数据库 db.create_all() # 创建测试数据, 一个用户, 一个电影条目 user = User(name='Test', username='******') # 新建用户 user.set_password('123') # 设置密码 movie = Movie(title='Test Movie Title', year='2020') # 创建一条电影记录 db.session.add_all([user, movie]) # add_all方法一次性传入多个模型类实例,以列表传入 db.session.commit() self.client = app.test_client() # 创建测试客户端 self.runner = app.test_cli_runner()
def admin(username, password): user = User.query.first() if user is not None: click.echo("update user..") user.name = "admin" user.username = username user.set_password(password) else: click.echo("creating user..") user = User(username=username, name="admin") user.set_password(password) DB.session.add(user) db.session.commit() click.echo("done..")
def register(): if request.method == 'POST': # 判断是否是 POST 请求 try: form = UserForm(formdata=request.form) if not form.validate(): flash('Invalid input!', category="danger") return render_template('register.html', form=form) email = form.email.data captcha = form.captcha.data password = form.password.data password_valid = form.password_valid.data mc_captcha = mc.get(email) logger.info(mc_captcha) if mc_captcha is None: flash( u'Wrong captcha or captcha has been expired, please resend!', category="danger") return render_template('register.html', form=form) elif mc_captcha != captcha: flash(u'Wrong captcha!', category="danger") return render_template('register.html', form=form) if not (len(email) <= 50 and 8 <= len(password) <= 20): flash('Invalid input length!', category="warning") return render_template('register.html', form=form) if password != password_valid: flash('Password is inconsistent!', category="danger") return render_template('register.html', form=form) user = User.query.get(email) if user is not None: flash('Email exists.', category="danger") return render_template('register.html', form=form) user = User(email=email) user.set_password(password) db.session.add(user) db.session.commit() flash('Register success.', category="success") return redirect(url_for('index')) except Exception: logger.exception('Faild to Register.') flash('Get a Exception.', category="danger") return render_template('register.html', form=UserForm())
def admin(username, password): db.create_all() user = User.query.first() if user is not None: click.echo('Updating user...') user.username = username user.set_password(password) else: click.echo('Creating user...') user = User(name='Tom Li', username=username) user.set_password(password) db.session.add(user) db.session.commit() click.echo('Done.')
def admin(username, password): user = User.query.first() if user is not None: click.echo('Updating user...') user.username = username user.set_password(password) # 设置密码 else: click.echo('Creating user...') user = User(username=username, name='Admin') user.set_password(password) # 设置密码 db.session.add(user) db.session.commit() # 提交数据库会话 click.echo('Done.')
def admin(username,password): db.create_all() user = User.query.first() if user is not None: click.echo('更新用户') user.username = username user.set_password(password) else: click.echo('创建用户') user = User(username=username,name="Admin") user.set_password(password) db.session.add(user) db.session.commit() click.echo('创建管理员账号完成')
def admin(username, password): """Create user.""" db.create_all() user = User.query.filter_by(username=username).first() if user is not None: click.echo('Updating user...') user.set_password(password) else: click.echo('Creating user...') user = User(username=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo('Done.')
def admin(username, password): """create user""" db.create_all() user = User.query.first() if user is not None: click.echo('Updating user...') user.username = username user.set_password(password) # 设置密码 else: click.echo('Creating user') user = User(username=username, name="Admin") user.set_password(password) db.session.add(user) db.session.commit() click.echo('Done')
def admin(username, password): db.create_all() user = User.query.first() if user is not None: click.echo("Updating user...") user.username = username user.set_password(password) else: click.echo("Creating user...") user = User(username=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo("Done.")
def setUp(self): #更新配置 app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URL='sqlite:///:memory:') #创建数据库和表 db.create_all() #创建测试数据,一个用户,一个电影条目 user = User(name='Test', username='******') user.set_password('123456') movie = Movie(title='Test Movie Title', year='2019') #使用add_all()方法一次添加多个模型类实例,传入列表 db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() #创建测试客户端 self.runner = app.test_cli_runner() #创建测试命令运行器
def forge(): name = 'lion' movies = [ { "title": "萨霍", "year": "2019" }, { "title": "星游记", "year": "2020" }, { "title": "急速备战", "year": "2019" }, { "title": "叶问4", "year": "2019" }, { "title": "三人行", "year": "2016" }, ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo("导入数据完成")
def forge(): # Generate fake data. db.create_all() # 全局的两个变量移动到这个函数内 name = 'yision' movies = [ {'title': 'My Neighbor Totoro', 'year': '1988'}, {'title': 'Dead Poers Society', 'year': '1989'}, {'title': 'A Perfect World', 'year': '1993'}, {'title': 'Leon', 'year': '1994'}, {'title': 'Mahjong', 'year': '1996'}, {'title': 'Swallowtail Butterfly', 'year': '1996'}, {'title': 'King of Comedy', 'year': '1996'}, {'title': 'Devils on the Doorstep', 'year': '1999'}, {'title': 'WALL-E', 'year': '2008'}, {'title': 'The Pork of Music', 'year': '2012'}, ] user = User(name = name) db.session.add(user) for m in movies: movie = Movie(title = m['title'], year = m['year']) db.session.add(movie) db.session.commit() click.echo('Done.')
def register(): form = RegistrationForm( request.form ) # 自定义类 RegistrationForm,它继承了类Form的所有属性。实例化RegistrationForm(用参数request.form),并用form接住。 if User.query.filter_by(email=form.email.data).first(): flash('The email is already exist!') return redirect(url_for('login')) elif request.method == 'POST' and form.validate(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash('Thanks for registering') return redirect(url_for('login')) else: return render_template('register.html', form=form)
def forge(): """Generate fake data.""" db.create_all() name = '杨福' movies = [ {'title': '肖申克的救赎', 'year': '1994'}, {'title': '霸王别姬', 'year': '1993'}, {'title': '这个杀手不太冷', 'year': '1994'}, {'title': '阿甘正传', 'year': '1994'}, {'title': '泰坦尼克号', 'year': '1997'}, {'title': '千与千寻', 'year': '2001'}, {'title': '盗梦空间', 'year': '2010'}, {'title': '忠犬八公的故事', 'year': '2009'}, {'title': '摔跤吧!爸爸', 'year': '2016'}, {'title': '罗马假日', 'year': '1953'}, ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo('Done.')
def forge(): db.create_all() name = 'Ricky Weng' movies = [ {'title': 'Zelig', 'year': '1983'}, {'title': '童年往事', 'year': '1985'}, {'title': '戀戀風塵', 'year': '1986'}, {'title': '重慶森林', 'year': '1994'}, {'title': '阳光灿烂的日子', 'year': '1994'}, {'title': 'キッズ・リターン', 'year': '1996'}, {'title': '小武', 'year': '1998'}, {'title': '一一', 'year': '2000'}, {'title': '三峡好人', 'year': '2006'}, {'title': '海よりもまだ深', 'year': '2016'} ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo('Done.')
def forge(): db.create_all() name = "peter" movies = [{ 'title': '流浪地球', 'year': '2019' }, { 'title': '千与千寻', 'year': '2001' }, { 'title': '海上钢琴师', 'year': '1998' }, { 'title': '复仇者联盟4', 'year': '2019' }, { 'title': '疯狂的外星人', 'year': '2019' }, { 'title': '寄生虫', 'year': '2019' }] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo('Done.')
def admin(username, password): db.create_all() user = User.query.first() if user is not None: # 用户存在,设置密码 click.echo('Updating user...') user.user_name = username user.set_password(password) else: # 用户不存在,新建并设置密码 click.echo('Creating user...') user = User(user_name=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() # 保存到数据库 click.echo('Done.')
def forge(): """Generate fake data""" db.create_all() name = "Hao Wang" movies = [ {"title": "My Neighbor Totoro", "year": "1988"}, {"title": "Dead Poets Society", "year": "1989"}, {"title": "A Perfect World", "year": "1993"}, {"title": "Leon", "year": "1994"}, {"title": "Mahjong", "year": "1996"}, {"title": "Swallowtail Butterfly", "year": "1996"}, {"title": "King of Comedy", "year": "1999"}, {"title": "Devils on the Doorstep", "year": "1999"}, {"title": "WALL-E", "year": "2008"}, {"title": "The Pork of Music", "year": "2012"}, ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m["title"], year=m["year"]) db.session.add(movie) db.session.commit() click.echo("Done.")
def setUp(self): # 更新配置 app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI="sqlite:///:memory:") # 创建数据库和表 db.create_all() # 创建测试数据,一个用户,一个电影条目 user = User(name="Test", username="******") user.set_password("123") movie = Movie(title="Test Movie Title", year="2019") # 使用 add_all() 方法一次添加多个模型类实例,传入列表 db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建测试客户端 self.runner = app.test_cli_runner() # 创建测试命令运行器
def forge(): """生成构造数据""" db.create_all() name = 'hanqingqing' movies = [ { 'title': 'My macos', 'year': '2018' }, { 'title': 'My windows', 'year': '2013' }, { 'title': 'My ubuntu', 'year': '2018' }, ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo('Done')