librelist archives

« back to archive

How to share object between blueprint in Flask

How to share object between blueprint in Flask

From:
赵磊
Date:
2014-11-29 @ 05:17
I have two blueprints, such as auth and sprints, the structure is as follows:


auth
    __init__.py
    views.py
    forms.py
sprints
    __init__.py
    views.py
    forms.py
I want to share api object after login successfully


@auth.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
    try:
        jira_url = app.config["JIRA_URL"]
        app_id = app.config["JIRA_APPID"]
        api = GreenHopper(options={"server": jira_url, "appid": 37},
                          basic_auth=(form.username.data, form.password.data))
        g.api = api
    except JIRAError:
        api = None
    ...
    ...
    return redirect(request.args.get('next') or url_for('scrumworks.index'))
return render_template('auth/login.html', form=form)
On the sprints blueprint, I want to get the api object like codes below:


@scrumworks.route("/sprints")
@login_required
def show_sprints():
    if current_user.admin:
        sprints = g.api.sprints(37)
        return render_template("scrumworks/sprints.html")
    else:
      flash("Only admin users can access this page")
       return redirect(url_for(".index"))
When the codes run, it will complain AttributeError: '_AppCtxGlobals' 
object has no attribute 'api'.


It seems the life cycle of g is per request, not global, so I can't store api in g


Another choice is session, but the api object cannot be serializable, so 
it can't be stored in session, what should I do next?


Any ideas? Thanks :-)‍

Re: [flask] How to share object between blueprint in Flask

From:
Artem Chekunov
Date:
2014-11-29 @ 06:41
Hi.

I think do you can in 'blueprint login' create login request for jira
And after return jira cookies like token of yourself application.
Then you can require token in other blueprints and use for request to jira.
I have two blueprints, such as auth and sprints, the structure is as
follows:

auth
    __init__.py
    views.py
    forms.py
sprints
    __init__.py
    views.py
    forms.py
I want to share api object after login successfully

@auth.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
    try:
        jira_url = app.config["JIRA_URL"]
        app_id = app.config["JIRA_APPID"]
        api = GreenHopper(options={"server": jira_url, "appid": 37},
                          basic_auth=(form.username.data,
form.password.data))
< div>        g.api = api
    except JIRAError:
        api = None
    ...
    ...
    return redirect(request.args.get('next') or url_for('scrumworks.index'))
return render_template('auth/login.html', form=form)
On the sprints blueprint, I want to get the api object like codes below:

@scrumworks.route("/sprints")
@login_required
def show_sprints():
    if current_user.admin:
        sprints = g.api.sprints(37)
        return render_template("scrumworks/sprints.html")
    else:
      flash("Only admin users can access this page")
       return redirect(url_for(".index"))
When the codes run, it will complain AttributeError: '_AppCtx Globals'
object has no attribute 'api'.

It seems the life cycle of g is per request, not global, so I can't store
api in g

Another choice is session, but the api object cannot be serializable, so it
can't be stored in session, what should I do next?

Any ideas? Thanks :-)‍

Re: [flask] How to share object between blueprint in Flask

From:
Artem Chekunov
Date:
2014-11-29 @ 06:41
Hi.

I think do you can in 'blueprint login' create login request for jira
And after return jira cookies like token of yourself application.
Then you can require token in other blueprints and use for request to jira.
I have two blueprints, such as auth and sprints, the structure is as
follows:

auth
    __init__.py
    views.py
    forms.py
sprints
    __init__.py
    views.py
    forms.py
I want to share api object after login successfully

@auth.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
    try:
        jira_url = app.config["JIRA_URL"]
        app_id = app.config["JIRA_APPID"]
        api = GreenHopper(options={"server": jira_url, "appid": 37},
                          basic_auth=(form.username.data,
form.password.data))
< div>        g.api = api
    except JIRAError:
        api = None
    ...
    ...
    return redirect(request.args.get('next') or url_for('scrumworks.index'))
return render_template('auth/login.html', form=form)
On the sprints blueprint, I want to get the api object like codes below:

@scrumworks.route("/sprints")
@login_required
def show_sprints():
    if current_user.admin:
        sprints = g.api.sprints(37)
        return render_template("scrumworks/sprints.html")
    else:
      flash("Only admin users can access this page")
       return redirect(url_for(".index"))
When the codes run, it will complain AttributeError: '_AppCtx Globals'
object has no attribute 'api'.

It seems the life cycle of g is per request, not global, so I can't store
api in g

Another choice is session, but the api object cannot be serializable, so it
can't be stored in session, what should I do next?

Any ideas? Thanks :-)‍