def registerUser(): if not request.data or request.is_json is not True: raise APIException('Missing JSON object', status_code=405) data = request.json userSchema = UserSchema() try: user = userSchema.load(data) except ValidationError as err: raise APIException(err.messages, status_code=400) existUsername = User.query.filter_by(username=data.get("username")).first() existEmail = User.query.filter_by(email=data.get("email")).first() if existUsername: raise APIException("Already exist other user with same username", status_code=409) if existEmail: raise APIException("Already exist other user with same email", status_code=409) newUser = User() newUser.name = data.get("name") newUser.email = data.get("email") newUser.username = data.get("username") newUser.password = generate_password_hash(data.get("password")) current_app.db.session.add(newUser) current_app.db.session.commit() return jsonify(), 201
def getUserByName(self, user_id): command = users.select().where(users.c.id == user_id) resultsProxy = engine.execute(command) results = resultsProxy.fetchone() schema = UserSchema() resultDic = schema.dump(results) resultsProxy.close() return resultDic.data
def list(self): command = users.select() resultsProxy = engine.execute(command) results = resultsProxy.fetchall() schema = UserSchema(many=True) resultDic = schema.dump(results) resultsProxy.close() return resultDic.data
def getProfile(): userQuery = User.query.get(current_user.id) if userQuery is None: raise APIException("User not found", 400) userSchema = UserSchema(only=["name", "email", "username"]) return jsonify({ "info": userSchema.dump(userQuery) }), 200
def post(self): data = request.get_json() try: email = data['email'] password = data['password'] except Exception as e: print(e) abort(400) user = User.query.filter_by(email=email).first() if user and flask_bcrypt.check_password_hash(user.password, password): _user = query_serializer(UserSchema(), [user])[0] _user['login_at'] = datetime.now() del _user['password'] access_token = create_access_token(identity=_user) refresh_token = create_refresh_token(identity=_user) jti = get_jti(refresh_token) _user['token'] = access_token _user['refresh'] = refresh_token login_session = LoginSession.query.filter_by( user_id=user.id).first() if login_session: login_session.jti = jti else: new_login_session = LoginSession(user.id, jti) db.session.add(new_login_session) try: db.session.commit() except Exception as e: print(e) abort(400, e) return jsonify({'ok': True, 'data': _user}) else: abort(400, "email or password is incorrect")
def post(self): formData1 = request.form['formData'] formData2 = json.loads(formData1) formData3 = formData2['formData'] try: result = UserSchema().load(formData3) except ValidationError as err: print('err:', err.messages) return json.dumps(err.messages) return 'submit success'
def put(self): data = request.get_json() try: email = data['email'] password = data['password'] secret_key = data['secretKey'] except Exception as e: abort(400, e) if secret_key != BaseConfig.RESET_PASSWORD_SECRET_KEY: abort(400, "Invalid reset password key") user = User.query.filter_by(email=email).first() if user is None: abort(400, "{} is not exists".format(email)) hashpass = flask_bcrypt.generate_password_hash(password).decode( 'utf-8') user.password = hashpass try: db.session.commit() except Exception as e: abort(500, e) serialized_result = query_serializer(UserSchema(), [user])[0] return jsonify(serialized_result)