From: Karl O. Pinc Date: Tue, 16 Jul 2024 18:48:48 +0000 (-0500) Subject: Refactor to support state kept in both POST and session X-Git-Url: https://papio.biology.duke.edu/gitweb/?a=commitdiff_plain;h=bef19b288ec30b7223d25a8bd043bcde20f73576;p=pgwui_core Refactor to support state kept in both POST and session --- diff --git a/src/pgwui_core/core.py b/src/pgwui_core/core.py index 2267b30..b13dc59 100644 --- a/src/pgwui_core/core.py +++ b/src/pgwui_core/core.py @@ -208,6 +208,27 @@ class LoadedForm(collections.abc.MutableMapping): def __delitem__(self, key): del self._store[key] + def read_post_and_session(self, post, session, key): + '''Read an attribute into self, from either POST or the session, + and synchronize the session with the POST value when there is a POST + value. + + post POST + session The session + key The attribute to read + + Returns: Boolean. True when a value is set; the key is in + either POST or the session. + ''' + if key in post: + self[key] = post[key] + self.session_put(key, self[key]) + elif key in session: + self[key] = session[key] + else: + return False + return True + def read(self): ''' In the children this loads form from pyramid self.uh.request @@ -289,18 +310,9 @@ class CredsLoadedForm(LoadedForm): # Defaults are now in place in self._form for password # and user. Ignore these since we want to know whether # to go to the session for data values. - if 'password' in post: - self['password'] = post['password'] - self.session_put('password', self['password']) - elif 'password' in session: - self['password'] = session['password'] - - if 'user' in post: - self['user'] = post['user'] - self.session_put('user', self['user']) - elif 'user' in session: - self['user'] = session['user'] - else: + self.read_post_and_session(post, session, 'password') + + if not self.read_post_and_session(post, session, 'user'): self['user'] = '' # Other, hidden, POST variables