Пример #1
0
def _remote_survey_url_myfoodrepo(transaction, account_id, source_id,
                                  language_tag):
    # assumes an instance of Transaction is already available
    st_repo = SurveyTemplateRepo(transaction)

    # do we already have an id?
    mfr_id, created = st_repo.get_myfoodrepo_id_if_exists(
        account_id, source_id)

    if mfr_id is None:
        # we need an ID so let's try and get one
        if created is None:
            # we need a slot and an id
            slot = st_repo.create_myfoodrepo_entry(account_id, source_id)
            if not slot:
                # we could not obtain a slot
                raise NotFound("Sorry, but the annotators are all allocated")

            mfr_id = myfoodrepo.create_subj()
        else:
            # we have a slot but no id
            mfr_id = myfoodrepo.create_subj()

        st_repo.set_myfoodrepo_id(account_id, source_id, mfr_id)
    else:
        # we already have an ID then just return the URL
        pass

    return myfoodrepo.gen_survey_url(mfr_id)
Пример #2
0
    def test_get_myfoodrepo_id_if_exists(self):
        with Transaction() as t:
            template_repo = SurveyTemplateRepo(t)
            obs = template_repo.get_myfoodrepo_id_if_exists(
                TEST1_ACCOUNT_ID, TEST1_SOURCE_ID)
            self.assertEqual(obs, (None, None))

            obs = template_repo.create_myfoodrepo_entry(
                TEST1_ACCOUNT_ID, TEST1_SOURCE_ID)
            self.assertTrue(obs)

            template_repo.set_myfoodrepo_id(TEST1_ACCOUNT_ID, TEST1_SOURCE_ID,
                                            "asubject")
            obs = template_repo.get_myfoodrepo_id_if_exists(
                TEST1_ACCOUNT_ID, TEST1_SOURCE_ID)
            self.assertEqual(obs[0], "asubject")
            self.assertTrue(obs[1] is not None)