Skip to content

Commit fc78f17

Browse files
committed
Do some checks and housekeeping when choice identification view is called
1 parent b44bb4f commit fc78f17

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/euphorie/client/browser/choice.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from euphorie.client import utils
12
from euphorie.client.navigation import getTreeData
23
from plone import api
34
from plone.memoize.instance import memoize
@@ -14,15 +15,45 @@ class IdentificationView(BrowserView):
1415
def webhelpers(self):
1516
return api.content.get_view("webhelpers", self.context.aq_parent, self.request)
1617

18+
def check_render_condition(self):
19+
# Render the page only if the user can inspection rights,
20+
# otherwise redirect to the start page of the session.
21+
if not self.webhelpers.can_inspect_session:
22+
return self.request.response.redirect(
23+
"{session_url}/@@start".format(
24+
session_url=self.webhelpers.traversed_session.absolute_url()
25+
)
26+
)
27+
if self.webhelpers.redirectOnSurveyUpdate():
28+
return
29+
1730
@property
1831
@memoize
1932
def navigation(self):
2033
return api.content.get_view("navigation", self.context, self.request)
2134

35+
def _get_next(self, reply):
36+
_next = reply.get("next", None)
37+
# In Safari browser we get a list
38+
if isinstance(_next, list):
39+
_next = _next.pop()
40+
return _next
41+
2242
@property
2343
def tree(self):
2444
return getTreeData(self.request, self.context)
2545

46+
@property
47+
@memoize
48+
def session(self):
49+
return self.webhelpers.traversed_session.session
50+
51+
@property
52+
@memoize
53+
def survey(self):
54+
"""This is the survey dexterity object."""
55+
return self.webhelpers._survey
56+
2657
@property
2758
@memoize
2859
def choice(self):
@@ -45,7 +76,7 @@ def set_answer_data(self, reply):
4576
# self.webhelpers.traversed_session.restrictedTraverse(path)
4677
# except KeyError:
4778
# answer.remove(path)
48-
self.context.set_options_by_zodb_path(answer)
79+
return self.context.set_options_by_zodb_path(answer)
4980

5081
def __call__(self):
5182
# Render the page only if the user has inspection rights,
@@ -54,8 +85,23 @@ def __call__(self):
5485
return self.request.response.redirect(
5586
self.context.aq_parent.absolute_url() + "/@@start"
5687
)
88+
self.check_render_condition()
89+
90+
utils.setLanguage(self.request, self.survey, self.survey.language)
91+
5792
if self.request.method == "POST":
5893
reply = self.request.form
59-
self.set_answer_data(reply)
94+
if not self.webhelpers.can_edit_session:
95+
return self.navigation.proceed_to_next(reply)
96+
_next = self._get_next(reply)
97+
# Don't persist anything if the user skipped the question
98+
if _next == "skip":
99+
return self.navigation.proceed_to_next(reply)
100+
101+
changed = self.set_answer_data(reply)
102+
103+
if changed:
104+
self.session.touch()
105+
60106
return self.navigation.proceed_to_next(reply)
61107
return self.index()

src/euphorie/client/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,15 +1539,19 @@ def is_visible(self):
15391539

15401540
def set_options_by_zodb_path(self, paths):
15411541
current = [option.zodb_path for option in self.options]
1542+
changed = False
15421543
for option in self.options:
15431544
if option.zodb_path not in paths:
15441545
self.options.remove(option)
15451546
Session.delete(option)
1547+
changed = True
15461548
for path in paths:
15471549
if path not in current:
15481550
option = Option(choice=self, zodb_path=path)
15491551
Session.add(option)
15501552
self.options.append(option)
1553+
changed = True
1554+
return changed
15511555

15521556

15531557
class Option(BaseObject):

0 commit comments

Comments
 (0)