Exemplo n.º 1
0
    def test_cache_should_be_clean_up_after_invalidate(self):
        cache = InMemoryCache()
        cache.update(flow=FLOW)

        cache.invalidate()

        self.assertEquals(cache.caching, {})
        self.assertEquals(cache.last_sync_time, None)
Exemplo n.º 2
0
 def __init__(self):
     super(RapidProService, self).__init__()
     self.cache = InMemoryCache()
Exemplo n.º 3
0
class RapidProService(object):
    rapid_pro_label_map = {
        'IMPLEMENTING_PARTNER': 'IMPLEMENTING_PARTNER',
        'MIDDLE_MAN': 'SUB_CONSIGNEES',
        'END_USER': '******'
    }

    @staticmethod
    def rapid_pro_label(flow_label):
        if flow_label in RapidProService.rapid_pro_label_map:
            return RapidProService.rapid_pro_label_map[flow_label]

        return None

    @staticmethod
    def internal_flow_label(label):
        flow_label_result = []
        for flow_label, rapid_pro_label in RapidProService.rapid_pro_label_map.items():
            if label[0] == rapid_pro_label:
                flow_label_result.append(flow_label)
                return flow_label_result

    def __init__(self):
        super(RapidProService, self).__init__()
        self.cache = InMemoryCache()

    def create_run(self, contact, flow, item, sender):
        logger.info('contact = %s' % contact)

        payload = FlowRequestTemplate().build(phone=contact['phone'],
                                              flow=self.flow_id(flow), sender=sender, item=item,
                                              contact_name="%s %s" % (contact['firstName'], contact['lastName']))

        if settings.RAPIDPRO_LIVE:
            response = requests.post(settings.RAPIDPRO_URLS['RUNS'], data=json.dumps(payload), headers=HEADER,
                                     verify=settings.RAPIDPRO_SSL_VERIFY)
            logger.info("Response from RapidPro: %s, %s" % (response.status_code, response.json()))

    def flow(self, flow_id):
        logger.info("flow_id: %s" % flow_id)

        self.__sync_if_required()

        # flow_id = int(flow_id)
        if flow_id not in self.cache.flow_label_mapping:
            raise FlowLabelNonExistException()

        rapid_pro_flow_label = self.cache.flow_label_mapping[flow_id]

        flow_label = RapidProService.internal_flow_label(rapid_pro_flow_label)

        return Flow.objects.filter(label__in=flow_label).first()

    def flow_id(self, flow):
        self.__sync_if_required()
        rapid_pro_flow_label = RapidProService.rapid_pro_label(flow.label)
        if rapid_pro_flow_label is not None:
            for flow_id, labels in self.cache.flow_label_mapping.iteritems():
                if rapid_pro_flow_label in labels:
                    return flow_id

        raise FlowLabelNonExistException()

    def __sync_if_required(self):
        if self.cache.expired:
            logger.info("Rapid Pro cache has been expired, start to sync.")
            self.__sync()

    def __sync(self):
        response = requests.get(settings.RAPIDPRO_URLS['FLOWS'], headers=HEADER, verify=settings.RAPIDPRO_SSL_VERIFY)
        if response.status_code == 200:
            self.cache.invalidate()
            flows = response.json()['results']
            logger.info(flows)
            self.cache.update(
                flow_label_mapping={rapid['uuid']: map((lambda label: label['name']), rapid['labels']) for rapid in flows if len(rapid['labels']) > 0})
        else:
            logger.warning("Failed to get flows information from Rapidpro, response=%s" % response)
Exemplo n.º 4
0
 def __init__(self):
     super(RapidProService, self).__init__()
     self.cache = InMemoryCache()
Exemplo n.º 5
0
class RapidProService(object):
    rapid_pro_label_map = {
        'IMPLEMENTING_PARTNER': 'IMPLEMENTING_PARTNER',
        'MIDDLE_MAN': 'SUB_CONSIGNEES',
        'END_USER': '******'
    }

    @staticmethod
    def rapid_pro_label(flow_label):
        if flow_label in RapidProService.rapid_pro_label_map:
            return RapidProService.rapid_pro_label_map[flow_label]

        return None

    @staticmethod
    def internal_flow_label(label):
        flow_label_result = []
        for flow_label, rapid_pro_label in RapidProService.rapid_pro_label_map.items(
        ):
            if label[0] == rapid_pro_label:
                flow_label_result.append(flow_label)
                return flow_label_result

    def __init__(self):
        super(RapidProService, self).__init__()
        self.cache = InMemoryCache()

    def create_run(self, contact, flow, item, sender):
        logger.info('contact = %s' % contact)

        payload = FlowRequestTemplate().build(
            phone=contact['phone'],
            flow=self.flow_id(flow),
            sender=sender,
            item=item,
            contact_name="%s %s" % (contact['firstName'], contact['lastName']))

        if settings.RAPIDPRO_LIVE:
            response = requests.post(settings.RAPIDPRO_URLS['RUNS'],
                                     data=json.dumps(payload),
                                     headers=HEADER,
                                     verify=settings.RAPIDPRO_SSL_VERIFY)
            logger.info("Response from RapidPro: %s, %s" %
                        (response.status_code, response.json()))

    def flow(self, flow_id):
        logger.info("flow_id: %s" % flow_id)

        self.__sync_if_required()

        # flow_id = int(flow_id)
        if flow_id not in self.cache.flow_label_mapping:
            raise FlowLabelNonExistException()

        rapid_pro_flow_label = self.cache.flow_label_mapping[flow_id]

        flow_label = RapidProService.internal_flow_label(rapid_pro_flow_label)

        return Flow.objects.filter(label__in=flow_label).first()

    def flow_id(self, flow):
        self.__sync_if_required()
        rapid_pro_flow_label = RapidProService.rapid_pro_label(flow.label)
        if rapid_pro_flow_label is not None:
            for flow_id, labels in self.cache.flow_label_mapping.iteritems():
                if rapid_pro_flow_label in labels:
                    return flow_id

        raise FlowLabelNonExistException()

    def __sync_if_required(self):
        if self.cache.expired:
            logger.info("Rapid Pro cache has been expired, start to sync.")
            self.__sync()

    def __sync(self):
        response = requests.get(settings.RAPIDPRO_URLS['FLOWS'],
                                headers=HEADER,
                                verify=settings.RAPIDPRO_SSL_VERIFY)
        if response.status_code == 200:
            self.cache.invalidate()
            flows = response.json()['results']
            logger.info(flows)
            self.cache.update(
                flow_label_mapping={
                    rapid['uuid']: map((
                        lambda label: label['name']), rapid['labels'])
                    for rapid in flows if len(rapid['labels']) > 0
                })
        else:
            logger.warning(
                "Failed to get flows information from Rapidpro, response=%s" %
                response)
Exemplo n.º 6
0
 def test_cache_should_be_expired_without_sync(self):
     self.assertTrue(InMemoryCache().expired)
Exemplo n.º 7
0
    def test_should_be_able_to_update_cache(self):
        cache = InMemoryCache()
        cache.update(flow=FLOW)

        self.assertEqual(cache.flow, FLOW)