def init_db(db_file): db_dir = os.path.dirname(db_file) if not os.path.exists(db_dir): Path(db_dir).mkdir(parents=True, exist_ok=True) db.init(db_file) db.connect() db.create_tables([User, Course, Score]) user = list(User.select()) if not user: user = User.create(username='******', password=hash_pw('student')) course = Course.create(name='Java') score = Score.create(user_id=user.id, course_id=course.id, value=100)
def seed_db(): db.session.add( User(username='******', email="*****@*****.**", firstName='phu', lastName='sakulwongtana', password='******')) db.session.add( User(username='******', email="*****@*****.**", firstName='phu2', lastName='sakulwongtana2', password='******')) db.session.commit()
def post(self): ''' 创建一条内容:user ''' print("In UsersList.post()") post_data = request.get_json() username = post_data.get('username') email = post_data.get('email') #----------------------------------------------- response_object = {} user = User.query.filter_by(email=email).first() if user: response_object['message'] = 'Sorry. That email already exists.' return response_object, 400 # 添加 db.session.add( User(username=username, email=email) ) db.session.commit() response_object = { 'message': f'{email} was added!' } return response_object, 201
def wrapper(*args, **kwargs): response = {"status": "fail", "message": "Invalid Token"} header = request.headers.get('Authorization') if not header: return jsonify(response), 403 auth_token = header.split(" ") if len(auth_token) < 2: response["message"] = "Authorization Token not Found" return jsonify(response), 403 auth_token = auth_token[1] user_id_from_token = User.decode_auth_token(auth_token) if isinstance(user_id_from_token, str): response["message"] = user_id_from_token return jsonify(response), 401 user = User.query.filter_by(id=user_id_from_token).first() if not user or not user.isActive: response[ "message"] = "Invalid User either not active or not exists" return jsonify(response), 401 return func(user_id_from_token, *args, **kwargs)
def post(self): post_data = request.get_json() username = post_data.get('username') email = post_data.get('email') response_object = {} user = User.query.filter_by(email=email).first() if user: response_object['message'] = 'Sorry. That email already exists.' return response_object, 400 db.session.add(User(username=username, email=email)) db.session.commit() response_object['message'] = f'{email} was added!' return response_object, 201
def post(self): post_data = request.get_json() username = post_data.get("username") email = post_data.get("email") response_object = {} user = User.query.filter_by(email=email).first() if user: response_object["message"] = "Sorry. That email already exists." return response_object, 400 db.session.add(User(username=username, email=email)) db.session.commit() response_object["message"] = f"{email} was added!" return response_object, 201
def check_credentials(username, password): user = User.select().where(User.username == username) if not len(user) == 1: return user = user[0] expected_hash = user.password password = password.encode('utf8') expected_hash = expected_hash.encode('utf8') valid = bcrypt.checkpw(password, expected_hash) if valid: user_id = user.id else: user_id = None return user_id
def add_user(): post_data = request.get_json() response = {"status": "fail", "message": "Invalid Payload"} if not post_data: return jsonify(response), 400 firstName = post_data.get("firstName") lastName = post_data.get("lastName") email = post_data.get("email") username = post_data.get("username") password = post_data.get("password") all_input = [firstName, lastName, email, username, password] if any(inp is None for inp in all_input): response["message"] = "Not Enough Information to create account" return jsonify(response), 400 try: user = User.query.filter( or_(User.username == username, User.email == email)).first() if not user: new_user = User(username=username, email=email, firstName=firstName, lastName=lastName, password=password) db.session.add(new_user) db.session.commit() response["status"] = "success" response["message"] = "User successfully added" return jsonify(response), 201 else: response["message"] = "Username or Email Already Exists." return jsonify(response), 400 except (exc.IntegrityError, ValueError) as e: db.session.rollback() response["message"] = "Internal Error" + str(e) return jsonify(response), 400
def _add_user(username, email): user = User(username=username, email=email) db.session.add(user) db.session.commit() return user
def seed_db(): print("seed_db starts.") db.session.add(User(username='******', email="*****@*****.**")) db.session.add(User(username='******', email="*****@*****.**")) db.session.commit() print("seed_db ends.")
def seed_db(): db.session.add(User(username='******', email="*****@*****.**")) db.session.add(User(username='******', email="*****@*****.**")) db.session.commit()
def seed_db(): db.session.add(User(username='******', email='*****@*****.**')) db.session.add(User(username='******', email='*****@*****.**')) db.session.commit()