def create_data(self): user = User() user.username = "******" user.name = "User" user.last_name = "Teste" user.hash_password("123456") user.confirmed_on = True db.session.add(user) db.session.commit()
def post(self): try: user = user_schema.load(request.get_json()) except ValidationError as err: msg = json.dumps({ 'message': err.messages, 'valid': err.valid_data }) return Response(msg, status=400, mimetype='application/json') exists = User.query.filter_by(email=user.email).first() if exists: return Response(json.dumps( {'message': 'Email already registered.'}), status=400, mimetype='application/json') user.uuid = uuid.uuid4() user.password = User.hash_password(user.password) db.session.add(user) db.session.commit() return Response(user_schema.dumps(user), status=201, mimetype='application/json')
def put(self, current_user, uuid): user = User.query.filter_by(id=uuid).first() if not user: return Response(json.dumps( {'message': 'User with id {} does not exist'.format(uuid)}), status=400, mimetype='application/json') try: json_input = user_schema.load(request.get_json(), partial=True) except ValidationError as err: msg = json.dumps({ 'message': err.messages, 'valid': err.valid_data }) return Response(msg, status=400, mimetype='application/json') for prop, value in json_input.__dict__.items(): if prop == 'email': exists = User.query.filter_by(email=value).first() if exists: return Response(json.dumps( {'message': 'Email already registered.'}), status=400, mimetype='application/json') setattr(user, prop, value) if prop == 'password': setattr(user, prop, User.hash_password(user.password)) db.session.commit() return user_schema.dump(user)
def save_new_user(json_body): username = json_body.get("username") email = json_body.get("email") password = json_body.get("password") password_confirm = json_body.get("password_confirm") if not username or not password or not password_confirm: return ResponseReturnKit.error400("Required field missing") if any(list(StringKit.password_check(password).values())): return ResponseReturnKit.error400( """A password is considered strong if: 6 characters length or more and 1 digit or more and 1 symbol or more and 1 uppercase letter or more and 1 lowercase letter or more""" ) if password_confirm != password: return ResponseReturnKit.error400("Password is not matching") find_user = User.query.filter_by(username=username).first() if find_user and not find_user.confirmed: return ResponseReturnKit.error400("User already exists") if find_user and find_user.confirmed: return ResponseReturnKit.error400("User or password invalid") if not ValidateKit.validate_email(email): return ResponseReturnKit.error400("Email is invalid") user = User(username=username) if email: user.email = email user.hash_password(password) user.registered_on = datetime.datetime.now() db.session.add(user) db.session.commit() token = StringKit.generate_confirmation_token(user.username) confirm_url = url_for('confirm_email', token=token, _external=True) return {"url": confirm_url}, 201