def get(self,uid): user = User.query.filter_by(uid=uid).first() schema = UserSchema() if user is not None: return Response(schema.dumps(user,indent=1).data,mimetype='application/json'),201 return jsonify({"get":"fail"})
def post(self): data = request.get_json() or {} user = UserSchema.model_validate(data) if user: user.save() return 'User created', 200 return 'User error', 409
def register(): data = request.json schema = UserSchema() try: user = schema.load(data) except ValidationError as error: return error.messages try: user_exists = User.select().where(User.email == user.email).get() return response_data(409, 'Already exists an user with that email') except peewee.DoesNotExist: pass password = user.password.encode() user.password = bcrypt.hashpw(password, bcrypt.gensalt()) user.save() return response_data(200, 'User registered')
class UserService: def __init__(self): self.anonymous_email = "*****@*****.**" self.anonymous_gaia = "ewpewawn" self.user_single = UserSchema() self.user_multiple = UserSchema(many=True) def serialize_single(self, user): return self.user_single.dump(user) def serialize_multiple(self, users): return self.user_multiple.dump(users) def get_or_create(self, email, gaia): user = self.get(email) if user is not None: print("User <'email={0} gaia={1}'> already exists.".format(email, gaia)) return user user = User(email=email, gaia=gaia) database.session.add(user) database.session.commit() return user def get(self, email): user = User.query.filter_by(email=email).first() if user is None: print("User <'email={0}'> does not exist yet.".format(email)) return None return user def get_all(self): return User.query.order_by(User.create_time.desc()).all() def get_anonymous(self): return self.get_or_create(self.anonymous_email, self.anonymous_gaia)
def __init__(self): self.anonymous_email = "*****@*****.**" self.anonymous_gaia = "ewpewawn" self.user_single = UserSchema() self.user_multiple = UserSchema(many=True)
def get(self): users = User.query.filter(User.admin == 1).all() if users == []: return jsonify({'get':'fail'}) schema = UserSchema(many = True) return Response(schema.dumps(users,indent=1).data,mimetype='application/json')
from flask_jwt_extended import (JWTManager, jwt_required, create_access_token, get_jwt_identity) jwt = JWTManager(app) app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False app.config['SECRET_KEY'] = 'some-secret-string' app.config['JWT_SECRET_KEY'] = 'super-secret' # reszta importóww from models import User, Post from flask_restful import Api from serializers import UserSchema, PostSchema from werkzeug.security import generate_password_hash, \ check_password_hash user_schema = UserSchema() users_schema = UserSchema(many=True) post_schema = PostSchema() posts_schema = PostSchema(many=True) #custom error # @app.errorhandler(404) # def page_not_found(e): # return "error 404" @app.route('/') def index(): # abort(404) return jsonify({"msg": "Hello Api"})
def api_login(email, password): user = User.query.filter_by(email=email).one_or_none() if user is not None and check_password_hash(user.password, password): user.update(last_login_at=datetime.now()) token = create_access_token(identity=user.id) return jsonify({'status': 200, 'access_token': token}) return jsonify({'status': 404, 'message': 'not found your email'}) @blueprint.route('/api/users', methods=['POST']) @jwt_optional @use_kwargs(CreateUserSchema()) @marshal_with(UserSchema()) def create(email, name, password, **kwargs): if User.query.filter_by(email=email).one_or_none() is None: user = User( email=email, name=name, password=generate_password_hash(password) ).save() return user else: return jsonify({'status': 500, 'message': 'email has exists'}) @blueprint.route('/api/users', methods=['GET']) @jwt_required