Ejemplo n.º 1
0
    def testSQLSyncToCouch(self):
        self.assertEqual(self.getCouchCount(), 0)
        self.assertEqual(self.getSQLCount(), 0)

        # Test Create
        sql_obj = Keyword()
        self.setRandomSQLObjectValues(sql_obj)
        self.assertEqual(self.getCouchCount(), 1)
        self.assertEqual(self.getSQLCount(), 1)

        couch_obj = SurveyKeyword.get(sql_obj.couch_id)
        self.compareObjects(couch_obj, sql_obj)
        self.assertTrue(SurveyKeyword.get_db().get_rev(couch_obj._id).startswith('2-'))

        # Test Update
        self.setRandomSQLObjectValues(sql_obj)
        self.assertEqual(self.getCouchCount(), 1)
        self.assertEqual(self.getSQLCount(), 1)
        couch_obj = SurveyKeyword.get(sql_obj.couch_id)
        self.compareObjects(couch_obj, sql_obj)
        self.assertTrue(SurveyKeyword.get_db().get_rev(couch_obj._id).startswith('3-'))

        # Test Delete
        couch_id = couch_obj._id
        sql_obj.delete()
        with self.assertRaises(ResourceNotFound):
            SurveyKeyword.get(couch_id)
        self.assertEqual(self.getCouchCount(), 0)
        self.assertEqual(self.getSQLCount(), 0)
Ejemplo n.º 2
0
    def test_unlink_keyword_returns_none_if_not_linked(self):
        keyword = Keyword()
        keyword.save()

        unlinked_keyword = unlink_keyword(keyword)

        self.assertIsNone(unlinked_keyword)
Ejemplo n.º 3
0
    def test_unlink_keywords_in_domain_returns_zero_if_no_linked_keywords(self):
        keyword = Keyword(domain=self.domain)
        keyword.save()

        unlinked_keywords = unlink_keywords_in_domain(self.domain)

        self.assertEqual(0, len(unlinked_keywords))
Ejemplo n.º 4
0
    def create_structured_sms_keyword(self, keyword, form_unique_id, reply_sms,
            delimiter=None, named_args=None, named_args_separator=None,
            override_open_sessions=True, initiator_filter=None):

        k = Keyword(
            domain=self.domain,
            keyword=keyword,
            description=keyword,
            delimiter=delimiter,
            override_open_sessions=override_open_sessions,
            initiator_doc_type_filter=initiator_filter or [],
        )
        k.save()

        k.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=KeywordAction.ACTION_SMS,
            message_content=reply_sms,
        )

        k.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=KeywordAction.ACTION_STRUCTURED_SMS,
            form_unique_id=form_unique_id,
            use_named_args=(named_args is not None),
            named_args=(named_args or {}),
            named_args_separator=named_args_separator,
        )
Ejemplo n.º 5
0
    def create_structured_sms_keyword(self, keyword, app_id, form_unique_id, reply_sms,
            delimiter=None, named_args=None, named_args_separator=None,
            override_open_sessions=True, initiator_filter=None):

        k = Keyword(
            domain=self.domain,
            keyword=keyword,
            description=keyword,
            delimiter=delimiter,
            override_open_sessions=override_open_sessions,
            initiator_doc_type_filter=initiator_filter or [],
        )
        k.save()

        k.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=KeywordAction.ACTION_SMS,
            message_content=reply_sms,
        )

        k.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=KeywordAction.ACTION_STRUCTURED_SMS,
            app_id=app_id,
            form_unique_id=form_unique_id,
            use_named_args=(named_args is not None),
            named_args=(named_args or {}),
            named_args_separator=named_args_separator,
        )
Ejemplo n.º 6
0
    def test_unlink_keyword_returns_unlinked_keyword(self):
        keyword = Keyword(upstream_id='abc123')
        keyword.save()

        unlinked_keyword = unlink_keyword(keyword)

        self.assertIsNone(unlinked_keyword.upstream_id)
        self.assertEqual(keyword.id, unlinked_keyword.id)
Ejemplo n.º 7
0
 def _create_new_keyword(self, keyword_name):
     keyword = Keyword(
         domain=self.domain_link.master_domain,
         keyword=keyword_name,
         description="The description",
         override_open_sessions=True,
     )
     keyword.save()
     return keyword
Ejemplo n.º 8
0
    def test_unlink_keywords_in_domain_successfully_unlinks_keyword(self):
        keyword = Keyword(domain=self.domain, upstream_id='abc123')
        keyword.save()

        unlinked_keywords = unlink_keywords_in_domain(self.domain)

        self.assertEqual(1, len(unlinked_keywords))
        self.assertIsNone(unlinked_keywords[0].upstream_id)
        self.assertEqual(keyword.id, unlinked_keywords[0].id)
Ejemplo n.º 9
0
def _create_keyword(domain, name="ping", upstream_id=None, should_save=True):
    keyword = Keyword(domain=domain,
                      keyword=name,
                      description="The description",
                      override_open_sessions=True,
                      upstream_id=upstream_id)
    if should_save:
        keyword.save()

    return keyword
Ejemplo n.º 10
0
def global_keyword_start(verified_number, text, msg, text_words,
                         open_sessions):
    outbound_metadata = MessageMetadata(workflow=WORKFLOW_KEYWORD, )

    if len(text_words) > 1:
        keyword = text_words[1]
        k = Keyword.get_keyword(verified_number.domain, keyword)
        if k:
            if not contact_can_use_keyword(verified_number, k):
                return False
            process_survey_keyword_actions(verified_number, k,
                                           text[6:].strip(), msg)
        else:
            message = get_message(MSG_KEYWORD_NOT_FOUND, verified_number,
                                  (keyword, ))
            send_sms_to_verified_number(verified_number,
                                        message,
                                        metadata=outbound_metadata)
    else:
        message = get_message(MSG_START_KEYWORD_USAGE, verified_number,
                              (text_words[0], ))
        send_sms_to_verified_number(verified_number,
                                    message,
                                    metadata=outbound_metadata)
    return True
Ejemplo n.º 11
0
 def page_context(self):
     domain_has_reminders_or_keywords = (domain_has_reminders(
         self.domain) or Keyword.domain_has_keywords(self.domain))
     bulk_sms_verification_enabled = (
         domain_has_reminders_or_keywords
         and domain_has_privilege(self.domain, privileges.INBOUND_SMS))
     return {
         'group':
         self.group,
         'group_members':
         self.members,
         'bulk_sms_verification_enabled':
         bulk_sms_verification_enabled,
         'num_users':
         len(self.members),
         'group_membership_form':
         self.group_membership_form,
         'can_edit_group_membership':
         (self.couch_user.can_edit_users_in_groups()
          or self.couch_user.can_edit_commcare_users()),
         'domain_uses_case_sharing':
         self.domain_uses_case_sharing,
         'show_disable_case_sharing':
         not has_privilege(self.request, privileges.CASE_SHARING_GROUPS)
         and self.group.case_sharing,
     }
Ejemplo n.º 12
0
    def create_survey_keyword(self, keyword, form_unique_id, delimiter=None,
            override_open_sessions=True, initiator_filter=None):

        k = Keyword(
            domain=self.domain,
            keyword=keyword,
            description=keyword,
            delimiter=delimiter,
            override_open_sessions=override_open_sessions,
            initiator_doc_type_filter=initiator_filter or [],
        )
        k.save()

        k.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=KeywordAction.ACTION_SMS_SURVEY,
            form_unique_id=form_unique_id,
        )
Ejemplo n.º 13
0
    def create_sms_keyword(self, keyword, reply_sms,
            override_open_sessions=True, initiator_filter=None,
            recipient=KeywordAction.RECIPIENT_SENDER, recipient_id=None):

        k = Keyword(
            domain=self.domain,
            keyword=keyword,
            description=keyword,
            override_open_sessions=override_open_sessions,
            initiator_doc_type_filter=initiator_filter or [],
        )
        k.save()

        k.keywordaction_set.create(
            recipient=recipient,
            recipient_id=recipient_id,
            action=KeywordAction.ACTION_SMS,
            message_content=reply_sms,
        )
Ejemplo n.º 14
0
    def create_sms_keyword(self, keyword, reply_sms,
            override_open_sessions=True, initiator_filter=None,
            recipient=KeywordAction.RECIPIENT_SENDER, recipient_id=None):

        k = Keyword(
            domain=self.domain,
            keyword=keyword,
            description=keyword,
            override_open_sessions=override_open_sessions,
            initiator_doc_type_filter=initiator_filter or [],
        )
        k.save()

        k.keywordaction_set.create(
            recipient=recipient,
            recipient_id=recipient_id,
            action=KeywordAction.ACTION_SMS,
            message_content=reply_sms,
        )
Ejemplo n.º 15
0
 def paginated_list(self):
     for keyword in Keyword.get_by_domain(
             self.domain,
             limit=self.limit,
             skip=self.skip,
     ):
         yield {
             'itemData': self._fmt_keyword_data(keyword),
             'template': 'keyword-row-template',
         }
Ejemplo n.º 16
0
    def create_survey_keyword(self, keyword, app_id, form_unique_id, delimiter=None,
            override_open_sessions=True, initiator_filter=None):

        k = Keyword(
            domain=self.domain,
            keyword=keyword,
            description=keyword,
            delimiter=delimiter,
            override_open_sessions=override_open_sessions,
            initiator_doc_type_filter=initiator_filter or [],
        )
        k.save()

        k.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=KeywordAction.ACTION_SMS_SURVEY,
            app_id=app_id,
            form_unique_id=form_unique_id,
        )
Ejemplo n.º 17
0
 def paginated_list(self):
     for keyword in Keyword.get_by_domain(
         self.domain,
         limit=self.limit,
         skip=self.skip,
     ):
         yield {
             'itemData': self._fmt_keyword_data(keyword),
             'template': 'keyword-row-template',
         }
Ejemplo n.º 18
0
    def test_unlink_keywords_in_domain_only_processes_linked_keywords(self):
        original_keyword = Keyword(domain=self.domain)
        linked_keyword = Keyword(domain=self.domain, upstream_id='abc123')
        original_keyword.save()
        linked_keyword.save()

        unlinked_keywords = unlink_keywords_in_domain(self.domain)

        self.assertEqual(1, len(unlinked_keywords))
        self.assertIsNone(unlinked_keywords[0].upstream_id)
        keyword_ids = [keyword.id for keyword in unlinked_keywords]
        self.assertFalse(original_keyword.id in keyword_ids)
        self.assertTrue(linked_keyword.id in keyword_ids)
Ejemplo n.º 19
0
 def clean_keyword(self):
     value = self.cleaned_data.get("keyword")
     if value is not None:
         value = value.strip().upper()
     if value is None or value == "":
         raise ValidationError(_("This field is required."))
     if len(value.split()) > 1:
         raise ValidationError(_("Keyword should be one word."))
     duplicate = Keyword.get_keyword(self._cchq_domain, value)
     if duplicate and duplicate.couch_id != self._sk_id:
         raise ValidationError(_("Keyword already exists."))
     return value
Ejemplo n.º 20
0
    def setUp(self):
        super(TestLinkedKeywords, self).setUp()

        module = self.master1.add_module(Module.new_module("M1", None))
        master_form = module.new_form(
            "f1", None,
            self.get_xml("very_simple_form").decode("utf-8"))
        self.keyword = Keyword(
            domain=self.domain_link.master_domain,
            keyword="ping",
            description="The description",
            override_open_sessions=True,
        )
        self.keyword.save()
        self.keyword.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=METHOD_SMS,
            message_content="pong",
            app_id=self.master1.get_id,
            form_unique_id=master_form.unique_id,
        )
Ejemplo n.º 21
0
 def clean_keyword(self):
     value = self.cleaned_data.get("keyword")
     if value is not None:
         value = value.strip().upper()
     if value is None or value == "":
         raise ValidationError(_("This field is required."))
     if len(value.split()) > 1:
         raise ValidationError(_("Keyword should be one word."))
     duplicate = Keyword.get_keyword(self.domain, value)
     if duplicate and duplicate.couch_id != self.survey_keyword_id:
         raise ValidationError(_("Keyword already exists."))
     return value
Ejemplo n.º 22
0
    def test_unlink_keywords_in_domain_unlinks_multiple_keywords(self):
        keyword1 = Keyword(domain=self.domain, upstream_id='abc123')
        keyword2 = Keyword(domain=self.domain, upstream_id='abc123')
        keyword1.save()
        keyword2.save()

        unlinked_keywords = unlink_keywords_in_domain(self.domain)

        self.assertEqual(2, len(unlinked_keywords))
        self.assertIsNone(unlinked_keywords[0].upstream_id)
        self.assertIsNone(unlinked_keywords[1].upstream_id)
        keyword_ids = [keyword.id for keyword in unlinked_keywords]
        self.assertTrue(keyword1.id in keyword_ids)
        self.assertTrue(keyword2.id in keyword_ids)
Ejemplo n.º 23
0
 def page_context(self):
     domain_has_reminders_or_keywords = (domain_has_reminders(
         self.domain) or Keyword.domain_has_keywords(self.domain))
     bulk_sms_verification_enabled = (
         domain_has_reminders_or_keywords
         and domain_has_privilege(self.domain, privileges.INBOUND_SMS))
     return {
         'group': self.group,
         'bulk_sms_verification_enabled': bulk_sms_verification_enabled,
         'num_users': len(self.members),
         'group_membership_form': self.group_membership_form,
         'domain_uses_case_sharing': self.domain_uses_case_sharing,
     }
Ejemplo n.º 24
0
def _create_keyword(domain,
                    name="ping",
                    upstream_id=None,
                    should_save=True,
                    is_grouped=False):
    keyword = Keyword(domain=domain,
                      keyword=name,
                      description="The description",
                      override_open_sessions=True,
                      upstream_id=upstream_id)

    if should_save:
        keyword.save()

    if is_grouped:
        keyword.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_USER_GROUP,
            recipient_id='abc123',
            action=KeywordAction.ACTION_SMS,
            message_content='Test',
        )

    return keyword
Ejemplo n.º 25
0
class TestLinkedKeywords(BaseLinkedAppsTest):
    def setUp(self):
        super(TestLinkedKeywords, self).setUp()

        module = self.master1.add_module(Module.new_module("M1", None))
        master_form = module.new_form(
            "f1", None,
            self.get_xml("very_simple_form").decode("utf-8"))
        self.keyword = Keyword(
            domain=self.domain_link.master_domain,
            keyword="ping",
            description="The description",
            override_open_sessions=True,
        )
        self.keyword.save()
        self.keyword.keywordaction_set.create(
            recipient=KeywordAction.RECIPIENT_SENDER,
            action=METHOD_SMS,
            message_content="pong",
            app_id=self.master1.get_id,
            form_unique_id=master_form.unique_id,
        )

    def tearDown(self):
        self.keyword.delete()
        super(TestLinkedKeywords, self).tearDown()

    def test_create_keyword_link(self):
        new_keyword_id = create_linked_keyword(self.domain_link,
                                               self.keyword.id)
        new_keyword = Keyword.objects.get(id=new_keyword_id)
        self.assertEqual(new_keyword.keyword, self.keyword.keyword)

        new_keyword_action = new_keyword.keywordaction_set.first()
        self.assertEqual(
            new_keyword_action.message_content,
            self.keyword.keywordaction_set.first().message_content,
        )

        self.assertEqual(new_keyword_action.app_id, self.linked_app.get_id)

    def test_update_keyword_link(self):
        new_keyword_id = create_linked_keyword(self.domain_link,
                                               self.keyword.id)

        self.keyword.keyword = "foo"
        self.keyword.save()
        keyword_action = self.keyword.keywordaction_set.first()
        keyword_action.message_content = "bar"
        keyword_action.save()

        update_keyword(self.domain_link, new_keyword_id)

        linked_keyword = Keyword.objects.get(id=new_keyword_id)
        self.assertEqual(linked_keyword.keyword, "foo")
        self.assertEqual(
            linked_keyword.keywordaction_set.first().message_content, "bar")
Ejemplo n.º 26
0
 def page_context(self):
     domain_has_reminders_or_keywords = (
         domain_has_reminders(self.domain) or
         Keyword.domain_has_keywords(self.domain)
     )
     bulk_sms_verification_enabled = (
         domain_has_reminders_or_keywords and
         domain_has_privilege(self.domain, privileges.INBOUND_SMS)
     )
     return {
         'group': self.group,
         'bulk_sms_verification_enabled': bulk_sms_verification_enabled,
         'num_users': len(self.members),
         'group_membership_form': self.group_membership_form,
         'domain_uses_case_sharing': self.domain_uses_case_sharing,
     }
Ejemplo n.º 27
0
def handle_domain_keywords(v, text, msg, text_words, sessions):
    any_session_open = len(sessions) > 0
    for survey_keyword in Keyword.get_by_domain(v.domain):
        args = split_args(text, survey_keyword)
        keyword = args[0].upper()
        if keyword == survey_keyword.keyword.upper():
            if any_session_open and not survey_keyword.override_open_sessions:
                # We don't want to override any open sessions, so just pass and
                # let the form session handler handle the message
                return False
            elif not contact_can_use_keyword(v, survey_keyword):
                # The contact type is not allowed to invoke this keyword
                return False
            else:
                inbound_metadata = MessageMetadata(workflow=WORKFLOW_KEYWORD, )
                add_msg_tags(msg, inbound_metadata)
                process_survey_keyword_actions(v, survey_keyword, text, msg)
                return True
    # No keywords matched, so pass the message onto the next handler
    return False
Ejemplo n.º 28
0
def global_keyword_start(v, text, msg, text_words, open_sessions):
    outbound_metadata = MessageMetadata(
        workflow=WORKFLOW_KEYWORD,
    )

    if len(text_words) > 1:
        keyword = text_words[1]
        k = Keyword.get_keyword(v.domain, keyword)
        if k:
            if not contact_can_use_keyword(v, k):
                return False
            process_survey_keyword_actions(v, k, text[6:].strip(), msg)
        else:
            message = get_message(MSG_KEYWORD_NOT_FOUND, v, (keyword,))
            send_sms_to_verified_number(v, message, metadata=outbound_metadata)
    else:
        message = get_message(MSG_START_KEYWORD_USAGE, v, 
            (text_words[0],))
        send_sms_to_verified_number(v, message, metadata=outbound_metadata)
    return True
Ejemplo n.º 29
0
    def test_already_linked_keyword_is_pushed(self):
        keyword = self._create_new_keyword('keyword')
        self.addCleanup(keyword.delete)
        linked_keyword_id = create_linked_keyword(self.domain_link, keyword.id)
        self.addCleanup(Keyword(id=linked_keyword_id).delete)
        # after creating the link, update the upstream keyword
        keyword.keyword = "updated-keyword"
        keyword.save()
        model = self._linked_data_view_model(
            MODEL_KEYWORD,
            detail=KeywordLinkDetail(keyword_id=str(keyword.id)).to_json())
        manager = ReleaseManager(self.domain, self.user.username)

        errors = manager._release_keyword(self.domain_link, model, 'test-user')
        self.assertIsNone(errors)

        downstream_keyword = get_downstream_keyword(self.linked_domain,
                                                    keyword.id)
        self.addCleanup(downstream_keyword.delete)
        self.assertIsNotNone(downstream_keyword)
        self.assertEqual("updated-keyword", downstream_keyword.keyword)
Ejemplo n.º 30
0
def handle_domain_keywords(v, text, msg, text_words, sessions):
    any_session_open = len(sessions) > 0
    for survey_keyword in Keyword.get_by_domain(v.domain):
        args = split_args(text, survey_keyword)
        keyword = args[0].upper()
        if keyword == survey_keyword.keyword.upper():
            if any_session_open and not survey_keyword.override_open_sessions:
                # We don't want to override any open sessions, so just pass and
                # let the form session handler handle the message
                return False
            elif not contact_can_use_keyword(v, survey_keyword):
                # The contact type is not allowed to invoke this keyword
                return False
            else:
                inbound_metadata = MessageMetadata(
                    workflow=WORKFLOW_KEYWORD,
                )
                add_msg_tags(msg, inbound_metadata)
                process_survey_keyword_actions(v, survey_keyword, text, msg)
                return True
    # No keywords matched, so pass the message onto the next handler
    return False
Ejemplo n.º 31
0
 def keyword(self):
     return Keyword(domain=self.domain)
Ejemplo n.º 32
0
 def total(self):
     return Keyword.get_by_domain(self.domain).count()
Ejemplo n.º 33
0
 def total(self):
     return Keyword.get_by_domain(self.domain).count()
Ejemplo n.º 34
0
 def _all_keywords(self):
     return Keyword.get_by_domain(self.domain)