Exemple #1
0
    def post(self):
        username = request.form['username']
        password = request.form['password']
        session = get_db().session
        user = session.query(UserModel).filter(
            UserModel.username == username).first()
        if user is not None:
            abort(400, message=_('{} already exists!'))
        session.add(UserModel(username=username, password=password))
        session.commit()

        return 'success', 201
Exemple #2
0
    def post(self):
        payload = to_snake_case(request.get_json())
        user = UserSchema().load(payload)
        user = UserModel(**user)

        try:
            db.session.add(user)
            db.session.commit()
            return 'User Successfully Created!', 201
        except Exception as e:
            db.session.rollback()
            raise e
Exemple #3
0
def create_user():
    ''' Create new user in Database '''
    username = request.json['username'].lower()
    password = request.json['password']

    user = UserModel.query.filter_by(username=username).first()
    if user is None:
        hashed_password = generate_password_hash(password, method='sha256')
        new_user = UserModel(public_id=str(uuid.uuid4().hex),
                             username=username,
                             password=hashed_password,
                             admin=False)
        db.session.add(new_user)
        db.session.commit()
        return user_schema.jsonify(new_user)

    return jsonify({'status': f'{username} is already in database'})
Exemple #4
0
    def post(self):
        data = self.parser.parse_args()
        username = data['username']
        password = data['password']
        # Missing Parameters
        if username is None or password is None:
            return {
                'message': 'No username or password passed for registration.'
            }, 400
        # Existing User
        if UserModel.query.filter_by(username=username).first() is not None:
            return {
                'message': 'UserModel has already been created, aborting.'
            }, 400

        user = UserModel(username=username)
        user.hash_password(password)
        db.session.add(user)
        db.session.commit()

        return {'username': user.username}, 201
    def setUp(self):
        """
        Test Setup
        """
        self.app = create_app("testing")
        self.client = self.app.test_client
        user_schema = UserSchema()
        user_data = {
            'last_name': 'olawale',
            'first_name': 'shindeiru',
            'email': '*****@*****.**',
            'password': '******',
            'address': 'Missing data for required field.',
            'city': 'Eindhoven',
            'zip_code': '3123ND',
            'age': '21',
            'phone_number': '0631566246',
        }
        user1 = {
            'password': '******',
            'email': '*****@*****.**',
        }
        data = user_schema.load(user_data)
        user = UserModel(data)
        self.daily_schedule = {
            'user_id': user.user_id,
            'schedule_date': '22-01-2020'
        }

        with self.app.app_context():
            # create all tables
            db.create_all()
            user.save()
            res = self.client().post(
                '/api/v1/users/login',
                headers={'Content-Type': 'application/json'},
                data=json.dumps(user1))
            json_data = json.loads(res.data)
            self.token = json_data.get('jwt_token')
    def post(self):
        data = user_parser.parse_args()

        if UserModel.find_by_username(data['username']):
            return {
                'message': 'User {} already exists'.format(data['username'])
            }

        new_user = UserModel(username=data['username'],
                             password=UserModel.generate_hash(
                                 data['password']))
        try:
            new_user.save_to_db()
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])
            return {
                'message': 'User {} was created'.format(data['username']),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except:
            return {'message': 'Something went wrong'}, 500
Exemple #7
0
    def post(self):
        data = parser.parse_args()

        if UserModel.find_by_username(data['username']):
            return create_response({'message': 'User {} already exists'
                                   .format(data['username'])}, 409)

        new_user = UserModel(
            username=data['username'],
            password=UserModel.generate_hash(data['password'])
        )

        try:
            new_user.save_to_db()
            access_token = create_access_token(identity=data['username'],
                                               expires_delta=False)
            return create_response({
                'message': 'User {} was created'.format(data['username']),
                'access_token': access_token
            }, 201)
        except Exception:
            return create_response({'message': 'Something went wrong'}, 500)