<commit_before># -*- test-case-name: go.apps.surveys.tests.test_vumi_app -*-

from twisted.internet.defer import inlineCallbacks, returnValue
from vxpolls.example import PollApplication
from vxpolls.manager import PollManager

from vumi.message import TransportUserMessage
from vumi import log

from go.vumitools.app_worker import GoApplicationMixin


def hacky_hack_hack(config):
    from vumi.persist.redis_manager import RedisManager
    return RedisManager.from_config(dict(config, key_separator=':'))


class SurveyApplication(PollApplication, GoApplicationMixin):

    worker_name = None

    def validate_config(self):
        self._go_validate_config()
        # vxpolls
        vxp_config = self.config.get('vxpolls', {})
        self.poll_prefix = vxp_config.get('prefix')

    @inlineCallbacks
    def setup_application(self):
        r_server = hacky_hack_hack(self.config.get('redis_manager'))
        self.pm = PollManager(r_server, self.poll_prefix)
        yield self._go_setup_application()

    @inlineCallbacks
    def teardown_application(self):
        yield self._go_teardown_application()
        self.pm.stop()

    @inlineCallbacks
    def consume_user_message(self, message):
        helper_metadata = message['helper_metadata']
        go = helper_metadata.get('go')
        poll_id = 'poll-%s' % (go.get('conversation_key'),)
        helper_metadata['poll_id'] = poll_id

        # If we've found a contact, grab it's dynamic-extra values
        # and update the participant with those before sending it
        # to the PollApplication
        contact = yield self.get_contact_for_message(message)
        print 'contact', contact
        if contact:
            participant = yield self.pm.get_participant(poll_id, message.user())
            print participant
            print participant.labels
            config = yield self.pm.get_config(poll_id)
            for key in config.get('include_labels', []):
                value = self.get_value_from_contact(contact, key)
                if value and key not in participant.labels:
                    participant.set_label(key, value)
            print 'saving participant', participant.dump()
            yield self.pm.save_participant(poll_id, participant)

        super(SurveyApplication, self).consume_user_message(message)

    def start_survey(self, to_addr, conversation, **msg_options):
        log.debug('Starting %r -> %s' % (conversation, to_addr))

        # We reverse the to_addr & from_addr since we're faking input
        # from the client to start the survey.
        from_addr = msg_options.pop('from_addr')
        msg = TransportUserMessage(from_addr=to_addr, to_addr=from_addr,
                content='', **msg_options)

        gmt = self.get_go_metadata(msg)
        gmt.set_conversation_info(conversation)

        self.consume_user_message(msg)

    @inlineCallbacks
    def end_session(self, participant, poll, message):
        # At the end of a session we want to store the user's responses<commit_msg>bugfix<commit_after># -*- test-case-name: go.apps.surveys.tests.test_vumi_app -*-

from twisted.internet.defer import inlineCallbacks, returnValue
from vxpolls.example import PollApplication
from vxpolls.manager import PollManager

from vumi.message import TransportUserMessage
from vumi import log

from go.vumitools.app_worker import GoApplicationMixin


def hacky_hack_hack(config):
    from vumi.persist.redis_manager import RedisManager
    return RedisManager.from_config(dict(config, key_separator=':'))


class SurveyApplication(PollApplication, GoApplicationMixin):

    worker_name = None

    def validate_config(self):
        self._go_validate_config()
        # vxpolls
        vxp_config = self.config.get('vxpolls', {})
        self.poll_prefix = vxp_config.get('prefix')

    @inlineCallbacks
    def setup_application(self):
        r_server = hacky_hack_hack(self.config.get('redis_manager'))
        self.pm = PollManager(r_server, self.poll_prefix)
        yield self._go_setup_application()

    @inlineCallbacks
    def teardown_application(self):
        yield self._go_teardown_application()
        self.pm.stop()

    @inlineCallbacks
    def consume_user_message(self, message):
        helper_metadata = message['helper_metadata']
        go = helper_metadata.get('go')
        poll_id = 'poll-%s' % (go.get('conversation_key'),)
        helper_metadata['poll_id'] = poll_id

        # If we've found a contact, grab it's dynamic-extra values
        # and update the participant with those before sending it
        # to the PollApplication
        contact = yield self.get_contact_for_message(message)
        print 'contact', contact
        if contact:
            participant = yield self.pm.get_participant(poll_id, message.user())
            print participant
            print participant.labels
            config = yield self.pm.get_config(poll_id)
            for key in config.get('include_labels', []):
                value = contact.extra[key]
                if value and key not in participant.labels:
                    participant.set_label(key, value)
            print 'saving participant', participant.dump()
            yield self.pm.save_participant(poll_id, participant)

        super(SurveyApplication, self).consume_user_message(message)

    def start_survey(self, to_addr, conversation, **msg_options):
        log.debug('Starting %r -> %s' % (conversation, to_addr))

        # We reverse the to_addr & from_addr since we're faking input
        # from the client to start the survey.
        from_addr = msg_options.pop('from_addr')
        msg = TransportUserMessage(from_addr=to_addr, to_addr=from_addr,
                content='', **msg_options)

        gmt = self.get_go_metadata(msg)
        gmt.set_conversation_info(conversation)

        self.consume_user_message(msg)

    @inlineCallbacks
    def end_session(self, participant, poll, message):
        # At the end of a session we want to store the user's responses
{'fraction_non_alphanumeric': 0.0890479360852197, 'fraction_numerical': 0.0, 'mean_word_length': 3.038306451612903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '939635', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1705, 'n_words': 398}