fix: add get_farmos_client_for_user() convenience function
This commit is contained in:
parent
9b4afb845b
commit
f2be7d0a53
3 changed files with 24 additions and 36 deletions
|
|
@ -23,9 +23,29 @@
|
|||
Misc. utilities for web app
|
||||
"""
|
||||
|
||||
from pyramid import httpexceptions
|
||||
from webhelpers2.html import HTML
|
||||
|
||||
|
||||
def get_farmos_client_for_user(request):
|
||||
token = request.session.get("farmos.oauth2.token")
|
||||
if not token:
|
||||
raise httpexceptions.HTTPForbidden()
|
||||
|
||||
# nb. must give a *copy* of the token to farmOS client, since it
|
||||
# will mutate it in-place and we don't want that to happen for our
|
||||
# original copy in the user session. (otherwise the auto-refresh
|
||||
# will not work correctly for subsequent calls.)
|
||||
token = dict(token)
|
||||
|
||||
def token_updater(token):
|
||||
save_farmos_oauth2_token(request, token)
|
||||
|
||||
config = request.wutta_config
|
||||
app = config.get_app()
|
||||
return app.get_farmos_client(token=token, token_updater=token_updater)
|
||||
|
||||
|
||||
def save_farmos_oauth2_token(request, token):
|
||||
"""
|
||||
Common logic for saving the given OAuth2 token within the user
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ from wuttaweb.views import MasterView
|
|||
from wuttaweb.forms.schema import WuttaDateTime
|
||||
from wuttaweb.forms.widgets import WuttaDateTimeWidget
|
||||
|
||||
from wuttafarm.web.util import save_farmos_oauth2_token, use_farmos_style_grid_links
|
||||
from wuttafarm.web.util import get_farmos_client_for_user, use_farmos_style_grid_links
|
||||
from wuttafarm.web.grids import (
|
||||
ResourceData,
|
||||
StringFilter,
|
||||
|
|
@ -70,28 +70,12 @@ class FarmOSMasterView(MasterView):
|
|||
|
||||
def __init__(self, request, context=None):
|
||||
super().__init__(request, context=context)
|
||||
self.farmos_client = self.get_farmos_client()
|
||||
self.farmos_client = get_farmos_client_for_user(self.request)
|
||||
self.farmos_4x = self.app.is_farmos_4x(self.farmos_client)
|
||||
self.normal = self.app.get_normalizer(self.farmos_client)
|
||||
self.raw_json = None
|
||||
self.farmos_style_grid_links = use_farmos_style_grid_links(self.config)
|
||||
|
||||
def get_farmos_client(self):
|
||||
token = self.request.session.get("farmos.oauth2.token")
|
||||
if not token:
|
||||
raise self.forbidden()
|
||||
|
||||
# nb. must give a *copy* of the token to farmOS client, since
|
||||
# it will mutate it in-place and we don't want that to happen
|
||||
# for our original copy in the user session. (otherwise the
|
||||
# auto-refresh will not work correctly for subsequent calls.)
|
||||
token = dict(token)
|
||||
|
||||
def token_updater(token):
|
||||
save_farmos_oauth2_token(self.request, token)
|
||||
|
||||
return self.app.get_farmos_client(token=token, token_updater=token_updater)
|
||||
|
||||
def get_fallback_templates(self, template):
|
||||
""" """
|
||||
templates = super().get_fallback_templates(template)
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ from pyramid.renderers import render_to_response
|
|||
|
||||
from wuttaweb.views import View
|
||||
|
||||
from wuttafarm.web.util import save_farmos_oauth2_token
|
||||
from wuttafarm.web.util import get_farmos_client_for_user
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
|
@ -42,7 +42,7 @@ class QuickFormView(View):
|
|||
|
||||
def __init__(self, request, context=None):
|
||||
super().__init__(request, context=context)
|
||||
self.farmos_client = self.get_farmos_client()
|
||||
self.farmos_client = get_farmos_client_for_user(self.request)
|
||||
self.farmos_4x = self.app.is_farmos_4x(self.farmos_client)
|
||||
self.normal = self.app.get_normalizer(self.farmos_client)
|
||||
|
||||
|
|
@ -127,22 +127,6 @@ class QuickFormView(View):
|
|||
def get_template_context(self, context):
|
||||
return context
|
||||
|
||||
def get_farmos_client(self):
|
||||
token = self.request.session.get("farmos.oauth2.token")
|
||||
if not token:
|
||||
raise self.forbidden()
|
||||
|
||||
# nb. must give a *copy* of the token to farmOS client, since
|
||||
# it will mutate it in-place and we don't want that to happen
|
||||
# for our original copy in the user session. (otherwise the
|
||||
# auto-refresh will not work correctly for subsequent calls.)
|
||||
token = dict(token)
|
||||
|
||||
def token_updater(token):
|
||||
save_farmos_oauth2_token(self.request, token)
|
||||
|
||||
return self.app.get_farmos_client(token=token, token_updater=token_updater)
|
||||
|
||||
@classmethod
|
||||
def defaults(cls, config):
|
||||
cls._defaults(config)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue