def seed_db(): from website.models import Note from website.models import User from website.__init__ import bcrypt from faker import Faker import random faker = Faker() users = [] for i in range(5): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(20): note = Note() note.title = faker.catch_phrase() note.user_id = random.choice(users).id db.session.add(note) db.session.commit() print("Tables seeded")
def create_user(self): user=User() user.username=self.username.data user.email=self.email.data user.password=self.password.data db.session.add(user) db.session.commit() return user
def create_user(username): user = User.query.filter_by(username=username).first() if user is None: admin = User() admin.sid = 111111 admin.username = username admin.password = generate_password_hash("123456") admin.email = "*****@*****.**" admin.nickname = "超级管理员" admin.is_superuser = True db.session.add(admin) db.session.commit()
def signup(): recaptcha = RecaptchaField() if request.method == "POST": username = request.form["username"] password = generate_password_hash(request.form["password"]) confirm_password = request.form["confirm-password"] about = request.form["about"] email = request.form["email"] letters = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890" letters = list(letters) if username == "": return render_template("signup.html", error="*You did not enter any username.", confirm=confirm_password, username=username, password=password, about=about) elif len(username.split(" ")) > 1: return render_template( "signup.html", error="*Your username cannot contain any spaces.", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) elif compare_digest(confirm_password, password): return render_template("signup.html", error="*Your passwords do not match.", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) elif compare_digest(password, ""): return render_template("signup.html", error="*You did not enter a password", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) elif bool(User.query.filter_by(username=username).first()): return render_template("signup.html", error="*The username is already taken.", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) elif username.lower in rude_words: return render_template("signup.html", error="*Rude words are not allowed.", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) else: for char in username: if char not in letters: return render_template( "signup.html", error= "*Your username can only contain letters and numbers. ", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) i = 0 for char in username: if char in "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM": i = i + 1 if i < 4: return render_template( "signup.html", error="*Your username must contain at least 4 letters.", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) for user in User.query.all(): if user.username.lower() == username.lower(): return render_template( "signup.html", error="*The username is already taken.", confirm=confirm_password, username=username, password=password, about=about, recaptcha=recaptcha) user = User() user.username = username user.top_progress = "" user.password = generate_password_hash(password) user.admin = 0 user.email = email db.session.add(user) db.session.commit() login_user(User.query.filter_by(password=password, username=username).first(), remember=True) user_id = current_user.id profile = User() profile.user_id = user_id profile.about = about db.session.add(profile) db.session.commit() return redirect(url_for('home')) else: return render_template("signup.html", error="", username="", password="", confirm="", about="", recaptcha=recaptcha)