Python flask_jwt_extended.get_jwt_identity() Examples

The following are 30 code examples of flask_jwt_extended.get_jwt_identity(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_jwt_extended , or try the search function .
Example #1
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def get(self):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You need to be {} in order to perform this request.".format(roles.DESIGNER), 401
        else: 
            # for group in user.groups:
            #     print("--- printing group of user.groups ---")
            #     print(group) 
            group_ids = [g.idx for g in user.groups]
            re = pipeline_service.get_pipelines(dbm, group_ids)
            dbm.close_session()
            # print("--- PipelineList result ---")
            # print(re) 
            return re 
Example #2
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 6 votes vote down vote up
def put(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        args = post_put_parser.parse_args(strict=True)
        if get_jwt_identity() != post.user_id:
            abort(403)
        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            print("Tags %s" % args['tags'])
            add_tags_to_post(post, args['tags'])

        db.session.merge(post)
        db.session.commit()
        return {'id': post.id}, 201 
Example #3
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def get(self):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.ANNOTATOR):
            dbm.close_session()
            return "You need to be {} in order to perform this request.".format(roles.ANNOTATOR), 401

        else:
            last_sia_image_id = sia.get_last_image_id(dbm, identity)
            if last_sia_image_id:
                re = sia.get_next(dbm, identity, last_sia_image_id, DATA_URL)
            else:
                re = sia.get_next(dbm, identity, -1, DATA_URL)
            dbm.close_session()
            return re 
Example #4
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def get(self):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You are not authorized.", 401
        else:
            users = dbm.get_users()
            for us in users:
                for g in us.groups:
                    if g.is_user_default:
                        us.groups.remove(g)
            dbm.close_session()
            ulist = {'users':users}
            return ulist 
Example #5
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 6 votes vote down vote up
def put(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        args = post_put_parser.parse_args(strict=True)
        if get_jwt_identity() != post.user_id:
            abort(403)
        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            print("Tags %s" % args['tags'])
            add_tags_to_post(post, args['tags'])

        db.session.merge(post)
        db.session.commit()
        return {'id': post.id}, 201 
Example #6
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 6 votes vote down vote up
def put(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        args = post_put_parser.parse_args(strict=True)
        if get_jwt_identity() != post.user_id:
            abort(403)
        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            print("Tags %s" % args['tags'])
            add_tags_to_post(post, args['tags'])

        db.session.merge(post)
        db.session.commit()
        return {'id': post.id}, 201 
Example #7
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def get(self):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.ANNOTATOR):
            dbm.close_session()
            return "You are not authorized.", 401
        else:
            group_ids = [g.idx for g in user.groups]
            annotask_list = annotask_service.get_available_annotasks(dbm, group_ids, identity)
            dbm.close_session()
            import json
            #with open('/code/backend/lost/api/annotask/test/annoTasks.json') as f:
            #    data = json.load(f)
            #return data
            return annotask_list 
Example #8
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def patch(self):
        args = update_user_parser.parse_args()
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if user:
            user.email = args.get('email') 
            user.first_name = args.get('first_name')
            user.last_name = args.get('last_name')
            if args.get('password'):
                user.set_password(args.get('password'))
            dbm.save_obj(user)
            dbm.close_session()
            return 'success', 200
        else:
            dbm.close_session()
            return "No user found.", 405 
Example #9
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 6 votes vote down vote up
def put(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        args = post_put_parser.parse_args(strict=True)
        if get_jwt_identity() != post.user_id:
            abort(403)
        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            print("Tags %s" % args['tags'])
            add_tags_to_post(post, args['tags'])

        db.session.merge(post)
        db.session.commit()
        return {'id': post.id}, 201 
Example #10
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def post(self):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You need to be {} in order to perform this request.".format(roles.DESIGNER), 401
        else:
            data = request.data
            # quick and dirty here, data was binary but should be dictonary without using json.loads locally.
            import json
            data = json.loads(data)
            group_id = None
            for user_group in dbm.get_user_groups_by_user_id(identity):
                if user_group.group.is_user_default:
                    group_id = user_group.group.idx
            if group_id:
                pipeline_service.start(dbm, data, identity, group_id)
                dbm.close_session()
                return "success"
            else:
                dbm.close_session()
                return "default group for user {} not found.".format(identity), 400 
Example #11
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 6 votes vote down vote up
def put(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        args = post_put_parser.parse_args(strict=True)
        if get_jwt_identity() != post.user_id:
            abort(403)
        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            print("Tags %s" % args['tags'])
            add_tags_to_post(post, args['tags'])

        db.session.merge(post)
        db.session.commit()
        return {'id': post.id}, 201 
Example #12
Source File: delete_user_controller.py    From TensorHive with Apache License 2.0 6 votes vote down vote up
def delete(id):
    try:
        current_user_id = get_jwt_identity()

        # User is not allowed to delete his own account
        assert id != current_user_id, R['delete']['self']

        # Fetch the user and destroy
        user_to_destroy = User.get(id)
        user_to_destroy.destroy()
    except AssertionError as error_message:
        content, status = {'msg': str(error_message)}, 403
    except NoResultFound:
        content, status = {'msg': R['not_found']}, 404
    except Exception as e:
        content, status = {'msg': G['internal_error'] + str(e)}, 500
    else:
        content, status = {'msg': R['delete']['success']}, 200
    finally:
        return content, status 
Example #13
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def post(self):
        dbm = access.DBMan(LOST_CONFIG) 
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        expires = datetime.timedelta(minutes=LOST_CONFIG.session_timeout)
        expires_refresh = datetime.timedelta(minutes=LOST_CONFIG.session_timeout + 2)
        if FLASK_DEBUG:
            expires = datetime.timedelta(days=365)
            expires_refresh = datetime.timedelta(days=366)
        if user:
            access_token = create_access_token(identity=user.idx, fresh=True, expires_delta=expires)
            refresh_token = create_refresh_token(user.idx, expires_delta=expires_refresh)
            ret = {
                'token': access_token,
                'refresh_token': refresh_token
            }
            dbm.close_session()
            return ret, 200
        dbm.close_session()
        return {'message': 'Invalid user'}, 401 
Example #14
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def delete(self, id):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You are not authorized.", 401

        group = dbm.get_group_by_id(id)
        
        if group:
            dbm.delete(group) 
            dbm.commit()
            dbm.close_session()
            return 'success', 200 
        else:
            dbm.close_session()
            return "Group with ID '{}' not found.".format(id), 400 
Example #15
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 6 votes vote down vote up
def put(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        args = post_put_parser.parse_args(strict=True)
        if get_jwt_identity() != post.user_id:
            abort(403)
        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            print("Tags %s" % args['tags'])
            add_tags_to_post(post, args['tags'])

        db.session.merge(post)
        db.session.commit()
        return {'id': post.id}, 201 
Example #16
Source File: task.py    From TensorHive with Apache License 2.0 6 votes vote down vote up
def spawn(id: TaskId) -> Tuple[Content, HttpStatusCode]:
    try:
        task = Task.get(id)
        assert task.user_id == get_jwt_identity(), 'Not an owner'
    except NoResultFound as e:
        log.error(e)
        content, status = {'msg': T['not_found']}, 404
    except AssertionError:
        content, status = {'msg': G['unpriviliged']}, 403
    else:
        content, status = business_spawn(id)
    finally:
        return content, status


# GET /tasks/{id}/terminate 
Example #17
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def post(self):
        args = create_label_parser.parse_args()
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You are not authorized.", 401
        else:
            label = model.LabelLeaf(name=args.get('name'),abbreviation=args.get('abbreviation'), \
            description=args.get('description'),external_id=args.get('external_id'), 
            is_root=args.get('is_root'))
            if args.get('parent_leaf_id'):
                label.parent_leaf_id = args.get('parent_leaf_id'),
            dbm.save_obj(label)
            dbm.close_session()
            return "success" 
Example #18
Source File: endpoint.py    From lost with MIT License 6 votes vote down vote up
def patch(self):
        args = update_label_parser.parse_args()
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You are not authorized.", 401
        else:
            label = dbm.get_label_leaf(int(args.get('id')))
            label.name = args.get('name')
            label.description = args.get('description')
            label.abbreviation = args.get('abbreviation')
            label.external_id = args.get('external_id')
            dbm.save_obj(label)
            dbm.close_session()
            return 'success' 
Example #19
Source File: task.py    From TensorHive with Apache License 2.0 5 votes vote down vote up
def destroy(id: TaskId) -> Tuple[Content, HttpStatusCode]:
    try:
        task = Task.get(id)
        assert task.user_id == get_jwt_identity(), 'Not an owner'
    except NoResultFound:
        content, status = {'msg': T['not_found']}, 404
    except AssertionError:
        content, status = {'msg': G['unpriviliged']}, 403
    else:
        content, status = business_destroy(id)
    finally:
        return content, status


# GET /tasks/{id}/spawn 
Example #20
Source File: task.py    From TensorHive with Apache License 2.0 5 votes vote down vote up
def get(id: TaskId) -> Tuple[Content, HttpStatusCode]:
    try:
        task = Task.get(id)
        assert get_jwt_identity() == task.user_id or is_admin()
    except NoResultFound:
        content, status = {'msg': T['not_found']}, 404
    except AssertionError:
        content, status = {'msg': G['unpriviliged']}, 403
    else:
        content, status = business_get(id)
    finally:
        return content, status


#  GET /tasks?userId=X?syncAll=1 
Example #21
Source File: task.py    From TensorHive with Apache License 2.0 5 votes vote down vote up
def update(id: TaskId, newValues: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]:
    try:
        task = Task.get(id)
        assert task.user_id == get_jwt_identity(), 'Not an owner'
    except NoResultFound:
        content, status = {'msg': T['not_found']}, 404
    except AssertionError:
        content, status = {'msg': G['unpriviliged']}, 403
    else:
        content, status = business_update(id, newValues)
    finally:
        return content, status


# DELETE /tasks/{id} 
Example #22
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def post(self):
        args = post_post_parser.parse_args(strict=True)
        new_post = Post(args['title'])
        new_post.user_id = get_jwt_identity()
        new_post.text = args['text']

        if args['tags']:
            add_tags_to_post(new_post, args['tags'])

        db.session.add(new_post)
        db.session.commit()
        return {'id': new_post.id}, 201 
Example #23
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def post(self):
        args = post_post_parser.parse_args(strict=True)
        new_post = Post(args['title'])
        new_post.user_id = get_jwt_identity()
        new_post.text = args['text']

        if args['tags']:
            add_tags_to_post(new_post, args['tags'])

        db.session.add(new_post)
        db.session.commit()
        return {'id': new_post.id}, 201 
Example #24
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def delete(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        if get_jwt_identity() != post.user_id:
            abort(401)

        db.session.delete(post)
        db.session.commit()
        return "", 204 
Example #25
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def post(self):
        print(request.data)
        args = post_post_parser.parse_args(strict=True)
        new_post = Post(args['title'])
        new_post.user_id = get_jwt_identity()
        new_post.text = args['text']

        if args['tags']:
            add_tags_to_post(new_post, args['tags'])

        db.session.add(new_post)
        db.session.commit()
        return {'id': new_post.id}, 201 
Example #26
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def post(self):
        args = post_post_parser.parse_args(strict=True)
        new_post = Post(args['title'])
        new_post.user_id = get_jwt_identity()
        new_post.text = args['text']

        if args['tags']:
            add_tags_to_post(new_post, args['tags'])

        db.session.add(new_post)
        db.session.commit()
        return {'id': new_post.id}, 201 
Example #27
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def delete(self, post_id=None):
        if not post_id:
            abort(400)
        post = Post.query.get(post_id)
        if not post:
            abort(404)
        if get_jwt_identity() != post.user_id:
            abort(401)

        db.session.delete(post)
        db.session.commit()
        return "", 204 
Example #28
Source File: controllers.py    From Mastering-Flask-Web-Development-Second-Edition with MIT License 5 votes vote down vote up
def post(self):
        args = post_post_parser.parse_args(strict=True)
        new_post = Post(args['title'])
        new_post.user_id = get_jwt_identity()
        new_post.text = args['text']

        if args['tags']:
            add_tags_to_post(new_post, args['tags'])

        db.session.add(new_post)
        db.session.commit()
        return {'id': new_post.id}, 201 
Example #29
Source File: endpoint.py    From lost with MIT License 5 votes vote down vote up
def get(self):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You are not authorized.", 401
        else:
            workers = dbm.get_worker()
            dbm.close_session()
            wlist = {'workers': workers}
            return wlist 
Example #30
Source File: endpoint.py    From lost with MIT License 5 votes vote down vote up
def delete(self,label_leaf_id):
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.DESIGNER):
            dbm.close_session()
            return "You are not authorized.", 401
        else:
            label = dbm.get_label_leaf(label_leaf_id)
            dbm.delete(label)
            dbm.commit()
            dbm.close_session()
            return "success"