Example #1
0
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument('email')
        parser.add_argument('first_name')
        parser.add_argument('last_name')
        data = parser.parse_args()

        try:
            email = get_jwt_identity()
            session_user = User.objects(email=email).first()

            if email == data['email']:
                session_user.update(first_name=data['first_name'],
                                    last_name=data['last_name'],
                                    updated_at=datetime.utcnow())
            else:
                user = User.objects(email=data['email']).first()
                if user is not None:
                    return error_409("Email already exist!")

                session_user.update(email=data['email'],
                                    first_name=data['first_name'],
                                    last_name=data['last_name'],
                                    updated_at=datetime.utcnow())

            user = User.objects(email=data['email']).first()
            response = user.serialize(excludes=['password'])
            return make_response(response, 200)

        except Exception as e:
            print(e)
            return error_500()
Example #2
0
    def put(self, id):
        parser = reqparse.RequestParser()
        roles = ("Admin", "User")
        parser.add_argument('role',
                            choices=roles,
                            required=True,
                            help='Invalid role!')
        parser.add_argument('first_name')
        parser.add_argument('last_name')
        data = parser.parse_args()

        try:
            email = get_jwt_identity()
            session_user = User.objects(email=email).first()

            user = User.objects(id=id, active=True).first()
            if user is None:
                return error_404()

            user.first_name = data['first_name']
            user.last_name = data['last_name']
            user.role = data['role']
            user.updated_at = datetime.utcnow()
            user.save()

            response = user.serialize(excludes=['password'])
            return make_response(response, 200)

        except Exception as e:
            print(e)
            return error_500()
Example #3
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('email', required=True, help='Email required!')
        parser.add_argument('password',
                            required=True,
                            help='Password required!')
        data = parser.parse_args()

        try:
            user = User.objects(email=data['email']).first()
            if user is None:
                return error_404("User not found!")

            if verify_hash(data['password'], user.password):
                response = {
                    'access_token':
                    create_access_token(identity=data['email']),
                    'refresh_token':
                    create_refresh_token(identity=data['email'])
                }
                return make_response(response, 200)
            else:
                return error_400("Invalid password!")
        except Exception as e:
            print(e)
            return error_500()
Example #4
0
    def delete(self, id):
        try:
            email = get_jwt_identity()
            session_user = User.objects(email=email).first()

            user = User.objects(id=id, active=True).first()
            if user is None:
                return error_404()

            user.active = False
            user.save()
            response = {'message': 'User deleted'}
            return make_response(response, 204)

        except Exception as e:
            print(e)
            return error_500()
Example #5
0
    def get(self, id):
        try:
            user = User.objects(id=id, active=True).first()
            if user is None:
                return error_404()

            response = user.serialize(excludes=['password'])
            return make_response(response, 200)
        except Exception as e:
            print(e)
            return error_500()
Example #6
0
    def get(self):
        email = get_jwt_identity()
        try:
            user = User.objects(email=email).first()
            if user is None:
                return error_404()

            response = user.serialize(excludes=['password'])
            return make_response(response, 200)
        except Exception as e:
            print(e)
            return error_500()
Example #7
0
def login(user):
    # print user['email'], user['password']
    user = User.objects(email=user['email'], password=user['password']).first()
    print user
    if not user:
        raise CustomException("Invalid credential")
    else:
        token = UserToken.objects.create(token=user.get_id(), user=user)
    return {
        'name': user.name,
        'email': user.email,
        'id': user.get_id(),
        'username': user.username,
        'token': token.token
    }
Example #8
0
    def get(self):
        try:
            user_list = User.objects(active=True)
            if user_list is None:
                return error_404()

            users = []
            for user in user_list:
                users.append(user.serialize(excludes=['password']))

            print(users)
            response = jsonify(users)
            return make_response(response, 200)
        except Exception as e:
            print(e)
            return error_500()
Example #9
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('email', required=True, help='Email required!')
        parser.add_argument('password',
                            required=True,
                            help='Password required!')
        roles = ("Admin", "User")
        parser.add_argument('role',
                            choices=roles,
                            required=True,
                            help='Invalid role!')
        parser.add_argument('first_name')
        parser.add_argument('last_name')
        data = parser.parse_args()

        try:
            user = User.objects(email=data['email']).first()
            if user is not None:
                return error_409("User already exist!")

            user = User(email=data['email'],
                        password=generate_hash(data['password']),
                        role=data['role'],
                        first_name=data['first_name'],
                        last_name=data['last_name'],
                        verified=False,
                        active=True,
                        created_at=datetime.utcnow(),
                        updated_at=datetime.utcnow())
            user.save()
            response = {
                'access_token': create_access_token(identity=data['email']),
                'refresh_token': create_refresh_token(identity=data['email'])
            }
            return make_response(response, 201)
        except Exception as e:
            print(e)
            return error_500()
Example #10
0
def findUser(id):
    return User.objects(id=id).first()