Example #1
0
def createOrUpdatePersonalExtension(profile_key, survey_key, **kwargs):
    """Creates personal extension for the specified survey and profile.

  In order to make sure that there is at most one personal extension between
  a particular profile and a survey, this function should be run within
  a transaction.

  The result of this function is saved in the datastore.

  Args:
    profile_key: profile_key.
    survey_key: survey key.

  Returns:
    newly created or updated personal extension entity.
  """
    extension = getPersonalExtension(profile_key, survey_key)
    if extension:
        extension.populate(**kwargs)
    else:
        # TODO(daniel): NDB migration; key does not need to be translated
        # when Profile and Survey models migrate to NDB
        ndb_survey_key = ndb.Key.from_old_key(survey_key)
        extension = survey_model.PersonalExtension(parent=profile_key,
                                                   survey=ndb_survey_key,
                                                   **kwargs)
    extension.put()

    return extension
Example #2
0
    def testSurveyActiveWithExtension(self):
        """Tests for survey that has personal extension for the profile."""
        # survey has ended
        self.survey.survey_start = timeline_utils.past(delta=200)
        self.survey.survey_end = timeline_utils.past(delta=100)

        # seed an extension
        self.extension = survey_model.PersonalExtension(
            parent=self.profile.key, survey=self.survey_key)
        self.extension.end_date = timeline_utils.future()
        self.extension.put()

        result = survey_logic.isSurveyActive(self.survey, self.profile.key)
        self.assertTrue(result)

        # survey has not started
        self.survey.survey_start = timeline_utils.future(delta=100)
        self.survey.survey_end = timeline_utils.future(delta=200)

        # set the extension so that the survey can be accessed now
        self.extension.end_date = None
        self.extension.start_date = timeline_utils.past()

        result = survey_logic.isSurveyActive(self.survey, self.profile.key)
        self.assertTrue(result)
Example #3
0
    def testExtensionIsUpdated(self):
        """Tests that extension can be updated."""
        extension = survey_model.PersonalExtension(parent=self.profile.key,
                                                   survey=self.survey_key)
        extension.put()

        # set new dates
        start_date = timeline_utils.past()
        end_date = timeline_utils.future()
        result = survey_logic.createOrUpdatePersonalExtension(
            self.profile.key,
            self.survey.key(),
            start_date=start_date,
            end_date=end_date)

        # check that the dates are updated
        self.assertEqual(result.start_date, start_date)
        self.assertEqual(result.end_date, end_date)

        # try cleaning the dates
        result = survey_logic.createOrUpdatePersonalExtension(
            self.profile.key,
            self.survey.key(),
            start_date=None,
            end_date=None)

        # check that the dates are cleared
        self.assertIsNone(result.start_date)
        self.assertIsNone(result.end_date)
Example #4
0
    def testExtensionAreadyExists(self):
        """Tests that another extension is not created if one exists."""
        extension = survey_model.PersonalExtension(parent=self.profile.key,
                                                   survey=self.survey_key)
        extension.put()

        result = survey_logic.createOrUpdatePersonalExtension(
            self.profile.key, self.survey.key())

        # check that result and extension are the same entity
        self.assertEqual(extension.key, result.key)
Example #5
0
    def testSurveyHasNotStartedWithExtension(self):
        """Tests for survey that has not started even with an extension."""
        # survey has not started yet
        self.survey.survey_start = timeline_utils.future(delta=100)
        self.survey.survey_end = timeline_utils.future(delta=200)

        # seed an extension
        self.extension = survey_model.PersonalExtension(
            parent=self.profile.key, survey=self.survey_key)
        self.extension.start_date = timeline_utils.future(delta=50)
        self.extension.put()

        result = survey_logic.hasSurveyStarted(self.survey, self.profile.key)
        self.assertFalse(result)
Example #6
0
    def testPersonalExtensionForAnotherSurvey(self):
        """Tests for no result even if extension exists for another survey."""
        # create an extension but for another survey
        other_survey = seeder_logic.seed(soc_survey_model.Survey)
        # TODO(daniel): NDB migration
        ndb_other_survey_key = ndb.Key.from_old_key(other_survey.key())
        extension = survey_model.PersonalExtension(parent=self.profile.key,
                                                   survey=ndb_other_survey_key)
        extension.put()

        # try getting the extension for the main survey
        result = survey_logic.getPersonalExtension(self.profile.key,
                                                   self.survey.key())

        # no extension should be returned
        self.assertIsNone(result)
Example #7
0
    def testPersonalExtensionExists(self):
        """Tests that if a personal extension exists, it will be returned."""
        # create personal extension
        # TODO(daniel): NDB migration
        ndb_survey_key = ndb.Key.from_old_key(self.survey.key())

        extension = survey_model.PersonalExtension(parent=self.profile.key,
                                                   survey=ndb_survey_key)
        extension.put()

        # try getting the extension
        result = survey_logic.getPersonalExtension(self.profile.key,
                                                   self.survey.key())

        # the extension should be returned
        self.assertEqual(extension.key, result.key)
Example #8
0
    def testPersonalExtensionForAnotherProfile(self):
        """Tests for no result even if extension exists for another profile."""
        # TODO(daniel): NDB migration
        ndb_survey_key = ndb.Key.from_old_key(self.survey.key())

        # create an extension but for another profile
        other_profile = profile_utils.seedNDBProfile(self.program.key())
        extension = survey_model.PersonalExtension(parent=other_profile.key,
                                                   survey=ndb_survey_key)
        extension.put()

        # try getting the extension for the main profile
        result = survey_logic.getPersonalExtension(self.profile.key,
                                                   self.survey.key())

        # no extension should be returned
        self.assertIsNone(result)
Example #9
0
    def testSurveyNotActiveWithExtension(self):
        """Tests that the survey is not active even with extension."""
        # survey has ended
        self.survey.survey_start = timeline_utils.past(delta=200)
        self.survey.survey_end = timeline_utils.past(delta=100)

        # seed an extension that also has ended
        self.extension = survey_model.PersonalExtension(
            parent=self.profile.key, survey=self.survey_key)
        self.extension.end_date = timeline_utils.past()
        self.extension.put()

        result = survey_logic.isSurveyActive(self.survey, self.profile.key)
        self.assertFalse(result)

        # survey has not started, neither the extension
        self.survey.survey_start = timeline_utils.future(delta=100)
        self.survey.survey_end = timeline_utils.future(delta=200)

        self.extension.end_date = None
        self.extension.start_date = timeline_utils.future()

        result = survey_logic.isSurveyActive(self.survey, self.profile.key)
        self.assertFalse(result)
Example #10
0
 def setUp(self):
     self.survey = seeder_logic.seed(soc_survey_model.Survey)
     self.extension = survey_model.PersonalExtension()