From f2be7d0a53edd92cc35bcf551c076e48af69d3f6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 26 Feb 2026 17:25:49 -0600 Subject: [PATCH] fix: add `get_farmos_client_for_user()` convenience function --- src/wuttafarm/web/util.py | 20 ++++++++++++++++++++ src/wuttafarm/web/views/farmos/master.py | 20 ++------------------ src/wuttafarm/web/views/quick/base.py | 20 ++------------------ 3 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/wuttafarm/web/util.py b/src/wuttafarm/web/util.py index 2d51851..977550a 100644 --- a/src/wuttafarm/web/util.py +++ b/src/wuttafarm/web/util.py @@ -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 diff --git a/src/wuttafarm/web/views/farmos/master.py b/src/wuttafarm/web/views/farmos/master.py index 742ce14..1e2ceab 100644 --- a/src/wuttafarm/web/views/farmos/master.py +++ b/src/wuttafarm/web/views/farmos/master.py @@ -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) diff --git a/src/wuttafarm/web/views/quick/base.py b/src/wuttafarm/web/views/quick/base.py index a40e8e3..9be6665 100644 --- a/src/wuttafarm/web/views/quick/base.py +++ b/src/wuttafarm/web/views/quick/base.py @@ -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)