Esempio n. 1
0
    def setupSession(self):
        """Setup the session for the context survey. This will rebuild the
        session tree if the profile has changed.

        Set up the survey session using a given profile.

        :param survey: the survey to use
        :type survey: :py:class:`euphorie.content.survey.Survey`
        :param survey_session: survey session to update
        :type survey_session: :py:class:`euphorie.client.model.SurveySession`
        :param dict profile: desired profile
        :rtype: :py:class:`euphorie.client.model.SurveySession`
        :return: the update session (this might be a new session)

        This will rebuild the survey session tree if the profile has changed.
        """
        survey = self.context.aq_parent
        survey_session = self.session
        profile = self.getDesiredProfile()
        if not survey_session.hasTree():
            BuildSurveyTree(survey, profile, survey_session)
            return survey_session

        current_profile = extractProfile(survey, survey_session)
        if current_profile == profile and not treeChanges(
                survey_session, survey, profile):
            # At this stage, we actually do not need to touch the session.
            # It is enough that it gets touched when a Risk is edited, or if the
            # tree gets rebuilt due to changes.
            # Touch means: the modification timestamp is set.
            # But we need to make sure the refreshed marker is up to date!
            survey_session.refresh_survey(survey)
            return survey_session

        params = {}
        # Some values might not be present, depending on the type of survey session
        _marker = object()
        for column in survey_session.__table__.columns:
            if column.key not in (
                    "id",
                    "account_id",
                    "title",
                    "created",
                    "modified",
                    "zodb_path",
            ):
                value = getattr(survey_session, column.key, _marker)
                if value is not _marker:
                    params[column.key] = value

        survey_view = api.content.get_view("index_html", survey, self.request)
        new_session = survey_view.create_survey_session(
            survey_session.title, survey_session.account, **params)
        BuildSurveyTree(survey, profile, new_session, survey_session)
        new_session.copySessionData(survey_session)
        object_session(survey_session).delete(survey_session)
        new_session.refresh_survey()
        return new_session
Esempio n. 2
0
 def test_profile_without_answers(self):
     BuildSurveyTree(
         {"one": createContainer("13", True)},
         profile={"13": []},
         dbsession=self.survey,
     )
     self.assertTrue(not self.survey.hasTree())
Esempio n. 3
0
 def testOptionalProfileNegative(self):
     BuildSurveyTree(
         {"one": createContainer("13", True)},
         profile={"13": False},
         dbsession=self.survey,
     )
     self.assertTrue(not self.survey.hasTree())
Esempio n. 4
0
 def test_empty_profile_with_container(self):
     BuildSurveyTree({"one": createContainer("13")}, {}, dbsession=self.survey)
     self.assertTrue(self.survey.hasTree())
     children = self.survey.children().all()
     self.assertEqual(len(children), 1)
     self.assertEqual(children[0].zodb_path, "13")
     self.assertEqual(children[0].children().count(), 0)
Esempio n. 5
0
 def test_empty_profile_with_risk(self):
     BuildSurveyTree({'one': createRisk("13")}, dbsession=self.survey)
     self.assertTrue(self.survey.hasTree())
     children = self.survey.children().all()
     self.assertEqual(len(children), 1)
     self.assertEqual(children[0].zodb_path, '13')
     self.assertEqual(children[0].children().count(), 0)
Esempio n. 6
0
 def test_ExistingSession_ProfileChanged(self):
     from euphorie.client.profile import BuildSurveyTree
     survey = self.createClientSurvey()
     survey.invokeFactory("euphorie.profilequestion", "1")
     view = self.makeView(survey)
     view.current_profile = {"1": ['London']}
     session = view.session
     BuildSurveyTree(survey, {"1": ['London']}, session)
     view.request.form["1"] = ['London', 'Paris']
     self.setupSession(view)
     self.failUnless(view.session is not session)
     self.assertEqual(view.session.hasTree(), False)
Esempio n. 7
0
 def test_ExistingSession_NoProfile(self):
     survey = self.createClientSurvey()
     survey.invokeFactory("euphorie.module", "1")
     mod = survey["1"]
     mod.title = "Module one"
     view = self.makeView(survey)
     with mock.patch("euphorie.client.profile.extractProfile", return_value={}):
         session = view.session
         BuildSurveyTree(survey, {}, session)
         with api.env.adopt_user(user=session.account):
             view.setupSession()
             self.assertEqual(view.session, session)
             self.assertTrue(session.hasTree())
Esempio n. 8
0
 def test_ExistingSession_ProfileChangedNoLocation(self):
     from euphorie.client.profile import BuildSurveyTree
     survey = self.createClientSurvey()
     survey.invokeFactory("euphorie.profilequestion", "1")
     survey.get('1').use_location_question = False
     view = self.makeView(survey)
     view.current_profile = {"1": True}
     session = view.session
     BuildSurveyTree(survey, {"1": True}, session)
     view.request.form["pq1.present"] = "no"
     self.setupSession(view)
     self.failUnless(view.session is not session)
     self.assertEqual(view.session.hasTree(), False)
Esempio n. 9
0
 def test_ExistingSession_NoProfile(self):
     from euphorie.client.profile import BuildSurveyTree
     survey = self.createClientSurvey()
     survey.invokeFactory("euphorie.module", "1")
     mod = survey["1"]
     mod.title = u"Module one"
     view = self.makeView(survey)
     view.current_profile = {}
     session = view.session
     BuildSurveyTree(survey, {}, session)
     self.setupSession(view)
     self.failUnless(view.session is session)
     self.assertEqual(session.hasTree(), True)
Esempio n. 10
0
 def test_profile_with_multiple_answers(self):
     BuildSurveyTree({'one': createContainer("13", True)},
                     profile={"13": ["one", "two"]},
                     dbsession=self.survey)
     self.assertTrue(self.survey.hasTree())
     children = self.survey.children().all()
     self.assertEqual(len(children), 1)
     self.assertEqual(children[0].zodb_path, '13')
     self.assertEqual(children[0].profile_index, -1)
     grandchildren = children[0].children().all()
     self.assertEqual(len(grandchildren), 2)
     self.assertEqual(grandchildren[0].title, 'one')
     self.assertEqual(grandchildren[0].profile_index, 0)
     self.assertEqual(grandchildren[1].title, 'two')
     self.assertEqual(grandchildren[1].profile_index, 1)
Esempio n. 11
0
    def test_ExistingSession_NoProfileChange(self):
        survey = self.createClientSurvey()
        survey.invokeFactory("euphorie.profilequestion", "1")
        survey.invokeFactory("euphorie.profilequestion", "2")
        survey.get("2").use_location_question = False

        view = self.makeView(survey)
        with mock.patch(
            "euphorie.client.profile.extractProfile",
            return_value={"1": ["London"], "2": True},
        ):
            session = view.session
            BuildSurveyTree(survey, {"1": ["London"], "2": True}, session)
            view.request.form["1"] = ["London"]
            view.request.form["pq1.present"] = "yes"
            view.request.form["pq2.present"] = "yes"
            with api.env.adopt_user(user=session.account):
                view.setupSession()
                self.assertEqual(view.session, session)
                self.assertEqual(view.session.hasTree(), True)
Esempio n. 12
0
 def test_ExistingSession_ProfileChanged(self):
     survey = self.createClientSurvey()
     survey.invokeFactory("euphorie.profilequestion", "1")
     view = self.makeView(survey)
     with mock.patch(
         "euphorie.client.profile.extractProfile",
         return_value={"1": ["London"]},
     ):
         session = view.session
         with api.env.adopt_user(user=session.account):
             BuildSurveyTree(survey, {"1": ["London"]}, session)
             view.request.form["1"] = ["London", "Paris"]
             # this will run an SQL statement incompatible with sqlite
             with mock.patch(
                 "euphorie.client.model.SurveySession.copySessionData",
                 return_value=None,
             ):
                 new_session = view.setupSession()
             view = self.makeView(survey, new_session)
             self.assertTrue(view.session is not session)
             self.assertEqual(view.session.hasTree(), False)
Esempio n. 13
0
 def test_empty_profile_with_profile_question(self):
     BuildSurveyTree({"one": createContainer("13", True)}, {}, dbsession=self.survey)
     self.assertTrue(not self.survey.hasTree())
Esempio n. 14
0
 def test_empty_profile_no_question(self):
     BuildSurveyTree({}, {}, dbsession=self.survey)
     self.assertTrue(not self.survey.hasTree())
Esempio n. 15
0
 def testOptionalProfilePositive(self):
     BuildSurveyTree({'one': createContainer("13", True)},
                     profile={"13": True},
                     dbsession=self.survey)
     self.assertTrue(self.survey.hasTree())