-
Notifications
You must be signed in to change notification settings - Fork 6
Dsetool: Choices #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Dsetool: Choices #837
Changes from all commits
6f00bb4
1bc4165
9be064d
3f76540
656fe7e
5a8c05a
2b08ec3
6c0174b
d1f0a56
9713aab
79f970d
199f46b
59f56fe
1350476
683c71b
64e083e
c39053b
f1c3850
b44bb4f
fc78f17
f7e472c
9e49d38
781195c
44abaad
567f48f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||||||
| from euphorie.client import utils | ||||||||||
| from euphorie.client.navigation import getTreeData | ||||||||||
| from plone import api | ||||||||||
| from plone.memoize.instance import memoize | ||||||||||
| from Products.Five import BrowserView | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class IdentificationView(BrowserView): | ||||||||||
| """A view for displaying a choice in the identification phase.""" | ||||||||||
|
|
||||||||||
| variation_class = "variation-risk-assessment" | ||||||||||
|
|
||||||||||
| @property | ||||||||||
| @memoize | ||||||||||
| def webhelpers(self): | ||||||||||
| return api.content.get_view("webhelpers", self.context.aq_parent, self.request) | ||||||||||
|
|
||||||||||
| def check_render_condition(self): | ||||||||||
| # Render the page only if the user can inspection rights, | ||||||||||
| # otherwise redirect to the start page of the session. | ||||||||||
| if not self.webhelpers.can_inspect_session: | ||||||||||
| return self.request.response.redirect( | ||||||||||
| "{session_url}/@@start".format( | ||||||||||
| session_url=self.webhelpers.traversed_session.absolute_url() | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
| if self.webhelpers.redirectOnSurveyUpdate(): | ||||||||||
| return | ||||||||||
|
Comment on lines
+27
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand this if clause.
Suggested change
To me
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from Euphorie/src/euphorie/client/browser/risk.py Line 627 in 781195c
My guess is that it's to make sure that any code that might be added after this line is not executed if the redirect kicks in. |
||||||||||
|
|
||||||||||
| @property | ||||||||||
| @memoize | ||||||||||
| def navigation(self): | ||||||||||
| return api.content.get_view("navigation", self.context, self.request) | ||||||||||
|
|
||||||||||
| def _get_next(self, reply): | ||||||||||
| _next = reply.get("next", None) | ||||||||||
| # In Safari browser we get a list | ||||||||||
| if isinstance(_next, list): | ||||||||||
| _next = _next.pop() | ||||||||||
| return _next | ||||||||||
|
|
||||||||||
| @property | ||||||||||
| def tree(self): | ||||||||||
| return getTreeData(self.request, self.context) | ||||||||||
|
|
||||||||||
| @property | ||||||||||
| @memoize | ||||||||||
| def session(self): | ||||||||||
| return self.webhelpers.traversed_session.session | ||||||||||
|
|
||||||||||
| @property | ||||||||||
| @memoize | ||||||||||
| def survey(self): | ||||||||||
| """This is the survey dexterity object.""" | ||||||||||
| return self.webhelpers._survey | ||||||||||
|
|
||||||||||
| @property | ||||||||||
| @memoize | ||||||||||
| def choice(self): | ||||||||||
| return self.webhelpers.traversed_session.restrictedTraverse( | ||||||||||
| self.context.zodb_path.split("/") | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| @property | ||||||||||
| @memoize | ||||||||||
| def selected(self): | ||||||||||
| return [option.zodb_path for option in self.context.options] | ||||||||||
|
|
||||||||||
| def set_answer_data(self, reply): | ||||||||||
| answer = reply.get("answer", []) | ||||||||||
| if not isinstance(answer, (list, tuple)): | ||||||||||
| answer = [answer] | ||||||||||
| # XXX Check if paths are valid? | ||||||||||
| # for path in answer[:]: | ||||||||||
| # try: | ||||||||||
| # self.webhelpers.traversed_session.restrictedTraverse(path) | ||||||||||
| # except KeyError: | ||||||||||
| # answer.remove(path) | ||||||||||
| return self.context.set_options_by_zodb_path(answer) | ||||||||||
|
|
||||||||||
| def __call__(self): | ||||||||||
| # Render the page only if the user has inspection rights, | ||||||||||
| # otherwise redirect to the start page of the session. | ||||||||||
| if not self.webhelpers.can_inspect_session: | ||||||||||
| return self.request.response.redirect( | ||||||||||
| self.context.aq_parent.absolute_url() + "/@@start" | ||||||||||
| ) | ||||||||||
| self.check_render_condition() | ||||||||||
|
|
||||||||||
| utils.setLanguage(self.request, self.survey, self.survey.language) | ||||||||||
|
|
||||||||||
| if self.request.method == "POST": | ||||||||||
| reply = self.request.form | ||||||||||
| if not self.webhelpers.can_edit_session: | ||||||||||
| return self.navigation.proceed_to_next(reply) | ||||||||||
| _next = self._get_next(reply) | ||||||||||
| # Don't persist anything if the user skipped the question | ||||||||||
| if _next == "skip": | ||||||||||
| return self.navigation.proceed_to_next(reply) | ||||||||||
|
|
||||||||||
| changed = self.set_answer_data(reply) | ||||||||||
|
|
||||||||||
| if changed: | ||||||||||
| self.session.touch() | ||||||||||
|
|
||||||||||
| return self.navigation.proceed_to_next(reply) | ||||||||||
| return super().__call__() | ||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||
| from euphorie.client import model | ||||||
| from euphorie.client.interfaces import CustomRisksModifiedEvent | ||||||
| from euphorie.client.navigation import FindNextQuestion | ||||||
| from euphorie.client.navigation import FindPreviousQuestion | ||||||
| from plone import api | ||||||
| from plone.memoize.instance import memoize | ||||||
| from Products.Five import BrowserView | ||||||
| from sqlalchemy import and_ | ||||||
| from z3c.saconfig import Session | ||||||
| from zope.event import notify | ||||||
|
|
||||||
|
|
||||||
| class NavigationView(BrowserView): | ||||||
| question_filter = None | ||||||
|
|
||||||
| @property | ||||||
| @memoize | ||||||
| def webhelpers(self): | ||||||
| return api.content.get_view("webhelpers", self.context, self.request) | ||||||
|
|
||||||
| @property | ||||||
| @memoize | ||||||
| def session(self): | ||||||
| return self.webhelpers.traversed_session.session | ||||||
|
|
||||||
| @property | ||||||
| @memoize | ||||||
| def previous_question(self): | ||||||
| return FindPreviousQuestion( | ||||||
| self.context, dbsession=self.session, filter=self.question_filter | ||||||
| ) | ||||||
|
|
||||||
| @property | ||||||
| @memoize | ||||||
| def next_question(self): | ||||||
| return FindNextQuestion( | ||||||
| self.context, dbsession=self.session, filter=self.question_filter | ||||||
| ) | ||||||
|
|
||||||
| def proceed_to_next(self, reply): | ||||||
| _next = reply.get("next", None) | ||||||
| # In Safari browser we get a list | ||||||
| if isinstance(_next, list): | ||||||
| _next = _next.pop() | ||||||
|
Comment on lines
+41
to
+44
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like more what you did on the other view, where you have a dedicated
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goal right now is only to move this from |
||||||
| if _next == "previous": | ||||||
| target = self.previous_question | ||||||
| if target is None: | ||||||
| # We ran out of questions, step back to intro page | ||||||
| url = "{session_url}/@@identification".format( | ||||||
| session_url=self.webhelpers.traversed_session.absolute_url() | ||||||
| ) | ||||||
| return self.request.response.redirect(url) | ||||||
| elif _next in ("next", "skip"): | ||||||
| target = self.next_question | ||||||
| if target is None: | ||||||
| # We ran out of questions, proceed to the action plan | ||||||
| if self.webhelpers.use_action_plan_phase: | ||||||
| next_view_name = "@@actionplan" | ||||||
| elif self.webhelpers.use_consultancy_phase: | ||||||
| next_view_name = "@@consultancy" | ||||||
| else: | ||||||
| next_view_name = "@@report" | ||||||
| base_url = self.webhelpers.traversed_session.absolute_url() | ||||||
| url = f"{base_url}/{next_view_name}" | ||||||
| return self.request.response.redirect(url) | ||||||
|
|
||||||
| elif _next == "add_custom_risk" and self.webhelpers.can_edit_session: | ||||||
| sql_module = ( | ||||||
| Session.query(model.Module) | ||||||
| .filter( | ||||||
| and_( | ||||||
| model.SurveyTreeItem.session == self.session, | ||||||
| model.Module.zodb_path == "custom-risks", | ||||||
| ) | ||||||
| ) | ||||||
| .first() | ||||||
| ) | ||||||
| if not sql_module: | ||||||
| url = self.context.absolute_url() + "/@@identification" | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am big fan of fstring:
Suggested change
They should be even faster, but what you did has nothing wrong. |
||||||
| return self.request.response.redirect(url) | ||||||
|
|
||||||
| view = api.content.get_view("identification", sql_module, self.request) | ||||||
| view.add_custom_risk() | ||||||
| notify(CustomRisksModifiedEvent(self.context.aq_parent)) | ||||||
| risk_id = self.context.aq_parent.children().count() | ||||||
| # Construct the path to the newly added risk: We know that there | ||||||
| # is only one custom module, so we can take its id directly. And | ||||||
| # to that we can append the risk id. | ||||||
| url = "{session_url}/{module}/{risk}/@@identification".format( | ||||||
| session_url=self.webhelpers.traversed_session.absolute_url(), | ||||||
| module=sql_module.getId(), | ||||||
| risk=risk_id, | ||||||
| ) | ||||||
| return self.request.response.redirect(url) | ||||||
| elif _next == "actionplan": | ||||||
| url = self.webhelpers.traversed_session.absolute_url() + "/@@actionplan" | ||||||
| return self.request.response.redirect(url) | ||||||
| # stay on current risk | ||||||
| else: | ||||||
| target = self.context | ||||||
| url = ("{session_url}/{path}/@@identification").format( | ||||||
| session_url=self.webhelpers.traversed_session.absolute_url(), | ||||||
| path="/".join(target.short_path), | ||||||
| ) | ||||||
| return self.request.response.redirect(url) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the code more readable I would add a
def redirectmethod that would make this possible:or something similar, IDK....
self.webhelpers.redirect_to_session_pathor whatever.That can be useful also for the other view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but let's refactor later and also include
browser/risk.pyto keep things halfways consistent.