[webrequest] Lazy-load search_state

  • The update_search_state function will hit the session data only if __mode is present in the form.
  • set_search_state keep the same semantics
  • Direct access to search_state in the session data is changed by an access to the new property.

Closes #4875761

authorChristophe de Vienne <christophe@unlish.com>
changeset1f84295bfe95
branchdefault
phasepublic
hiddenno
parent revision#ef74abcf369d [config] disable fckeditor by default (closes #1368900)
child revision#d6131c5045be [web/request] simplify search_state loading, fix comment
files modified by this revision
web/request.py
# HG changeset patch
# User Christophe de Vienne <christophe@unlish.com>
# Date 1422434815 -3600
# Wed Jan 28 09:46:55 2015 +0100
# Node ID 1f84295bfe959216eed849fb522f96bed78d3553
# Parent ef74abcf369dd7686fed69772e9cae4dae3e10a9
[webrequest] Lazy-load search_state

- The update_search_state function will hit the session data only if __mode is
present in the form.

- set_search_state keep the same semantics

- Direct access to search_state in the session data is changed by an access to
the new property.

Closes #4875761

diff --git a/web/request.py b/web/request.py
@@ -153,13 +153,11 @@
1          self.set_user_language(None)
2          #: dictionary that may be used to store request data that has to be
3          #: shared among various components used to publish the request (views,
4          #: controller, application...)
5          self.data = {}
6 -        #:  search state: 'normal' or 'linksearch' (eg searching for an object
7 -        #:  to create a relation with another)
8 -        self.search_state = ('normal',)
9 +        self._search_state = None
10          #: page id, set by htmlheader template
11          self.pageid = None
12          self._set_pageid()
13 
14      def _set_pageid(self):
@@ -352,25 +350,36 @@
15              del self._msg
16          if hasattr(self, '_msgid'):
17              self.session.data.pop(self._msgid, u'')
18              del self._msgid
19 
20 +    def _load_search_state(self, searchstate):
21 +        if searchstate is None or searchstate == 'normal':
22 +            self._search_state = (searchstate or 'normal',)
23 +        else:
24 +            self._search_state = ('linksearch', searchstate.split(':'))
25 +            assert len(self._search_state[-1]) == 4, 'invalid searchstate'
26 +
27 +    @property
28 +    def search_state(self):
29 +        """search state: 'normal' or 'linksearch' (eg searching for an object
30 +        to create a relation with another)"""
31 +        if self._search_state is None:
32 +            searchstate = self.session.data.get('search_state', 'normal')
33 +            self._load_search_state(searchstate)
34 +        return self._search_state
35 +
36      def update_search_state(self):
37 -        """update the current search state"""
38 +        """update the current search state if needed"""
39          searchstate = self.form.get('__mode')
40 -        if not searchstate:
41 -            searchstate = self.session.data.get('search_state', 'normal')
42 -        self.set_search_state(searchstate)
43 +        if searchstate:
44 +            self.set_search_state(searchstate)
45 
46      def set_search_state(self, searchstate):
47          """set a new search state"""
48 -        if searchstate is None or searchstate == 'normal':
49 -            self.search_state = (searchstate or 'normal',)
50 -        else:
51 -            self.search_state = ('linksearch', searchstate.split(':'))
52 -            assert len(self.search_state[-1]) == 4
53          self.session.data['search_state'] = searchstate
54 +        self._load_search_state(searchstate)
55 
56      def match_search_state(self, rset):
57          """when searching an entity to create a relation, return True if entities in
58          the given rset may be used as relation end
59          """
@@ -383,11 +392,11 @@
60                  return False
61          return True
62 
63      def update_breadcrumbs(self):
64          """stores the last visisted page in session data"""
65 -        searchstate = self.session.data.get('search_state')
66 +        searchstate = self.search_state[0]
67          if searchstate == 'normal':
68              breadcrumbs = self.session.data.get('breadcrumbs')
69              if breadcrumbs is None:
70                  breadcrumbs = SizeConstrainedList(10)
71                  self.session.data['breadcrumbs'] = breadcrumbs