Example #1
0
def prepare_domain(domain_name):
    from corehq.apps.commtrack.tests import bootstrap_domain
    domain = bootstrap_domain(domain_name)
    previous = None
    for name, administrative in [("MOHSW", True), ("MSDZONE", True),
                                 ("REGION", True), ("DISTRICT", True),
                                 ("FACILITY", False)]:
        previous, _ = LocationType.objects.get_or_create(
            domain=domain_name,
            name=name,
            parent_type=previous,
            administrative=administrative,
        )

    generator.instantiate_accounting_for_tests()
    account = BillingAccount.get_or_create_account_by_domain(
        domain.name,
        created_by="automated-test",
    )[0]
    plan = DefaultProductPlan.get_default_plan_by_domain(
        domain, edition=SoftwarePlanEdition.ADVANCED)
    commtrack = domain.commtrack_settings
    commtrack.actions.append(
        CommtrackActionConfig(action='receipts',
                              keyword='delivered',
                              caption='Delivered'))
    commtrack.save()
    subscription = Subscription.new_domain_subscription(
        account, domain.name, plan)
    subscription.is_active = True
    subscription.save()
    ils_config = ILSGatewayConfig(enabled=True, domain=domain.name)
    ils_config.save()
    return domain
Example #2
0
    def settings_context(self):
        config = ILSGatewayConfig.for_domain(self.domain_object.name)

        if config:
            return {
                "source_config": config._doc,
            }
        else:
            return {"source_config": ILSGatewayConfig()._doc}
    def setUp(self):
        self.endpoint = MockEndpoint("http://test-api.com/", "dummy", "dummy")
        self.stock_api_object = MockILSStockDataSynchronization(TEST_DOMAIN, self.endpoint)
        self.datapath = os.path.join(os.path.dirname(__file__), "data")
        initial_bootstrap(TEST_DOMAIN)
        self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
        self.api_object.prepare_commtrack_config()
        config = ILSGatewayConfig()
        config.domain = TEST_DOMAIN
        config.enabled = True
        config.all_stock_data = True
        config.password = "******"
        config.username = "******"
        config.url = "http://test-api.com/"
        config.save()
        l1 = Location(name="Test location 1", external_id="3445", location_type="FACILITY", domain=TEST_DOMAIN)

        l2 = Location(name="Test location 2", external_id="4407", location_type="FACILITY", domain=TEST_DOMAIN)

        l1.save()
        l2.save()

        SupplyPointCase.create_from_location(TEST_DOMAIN, l1)
        SupplyPointCase.create_from_location(TEST_DOMAIN, l2)

        with open(os.path.join(self.datapath, "sample_products.json")) as f:
            for product_json in json.loads(f.read()):
                self.api_object.product_sync(Product(product_json))

        StockTransaction.objects.all().delete()
Example #4
0
def prepare_domain(domain_name):
    from corehq.apps.commtrack.tests.util import bootstrap_domain
    domain = bootstrap_domain(domain_name)
    previous = None
    for name, administrative in [
        ("MOHSW", True),
        ("MSDZONE", True),
        ("REGION", True),
        ("DISTRICT", True),
        ("FACILITY", False)
    ]:
        previous, _ = LocationType.objects.get_or_create(
            domain=domain_name,
            name=name,
            parent_type=previous,
            administrative=administrative,
        )

    generator.instantiate_accounting()
    account = BillingAccount.get_or_create_account_by_domain(
        domain.name,
        created_by="automated-test",
    )[0]
    plan = DefaultProductPlan.get_default_plan(
        edition=SoftwarePlanEdition.ADVANCED
    )
    commtrack = domain.commtrack_settings
    commtrack.actions.append(
        CommtrackActionConfig(action='receipts',
                              keyword='delivered',
                              caption='Delivered')
    )
    commtrack.save()
    subscription = Subscription.new_domain_subscription(
        account,
        domain.name,
        plan
    )
    subscription.is_active = True
    subscription.save()
    ils_config = ILSGatewayConfig(enabled=True, domain=domain.name, all_stock_data=True)
    ils_config.save()
    fields_definition = CustomDataFieldsDefinition.get_or_create(domain.name, 'LocationFields')
    fields_definition.fields.append(CustomDataField(
        slug='group',
        label='Group',
        is_required=False,
        choices=['A', 'B', 'C'],
        is_multiple_choice=False
    ))
    fields_definition.save()
    return domain
Example #5
0
def prepare_domain(domain_name):
    from corehq.apps.commtrack.tests.util import bootstrap_domain
    domain = bootstrap_domain(domain_name)
    previous = None
    for name, administrative in [
        ("MOHSW", True),
        ("MSDZONE", True),
        ("REGION", True),
        ("DISTRICT", True),
        ("FACILITY", False)
    ]:
        previous, _ = LocationType.objects.get_or_create(
            domain=domain_name,
            name=name,
            parent_type=previous,
            administrative=administrative,
        )

    generator.instantiate_accounting_for_tests()
    account = BillingAccount.get_or_create_account_by_domain(
        domain.name,
        created_by="automated-test",
    )[0]
    plan = DefaultProductPlan.get_default_plan_by_domain(
        domain, edition=SoftwarePlanEdition.ADVANCED
    )
    commtrack = domain.commtrack_settings
    commtrack.actions.append(
        CommtrackActionConfig(action='receipts',
                              keyword='delivered',
                              caption='Delivered')
    )
    commtrack.save()
    subscription = Subscription.new_domain_subscription(
        account,
        domain.name,
        plan
    )
    subscription.is_active = True
    subscription.save()
    ils_config = ILSGatewayConfig(enabled=True, domain=domain.name, all_stock_data=True)
    ils_config.save()
    fields_definition = CustomDataFieldsDefinition.get_or_create(domain.name, 'LocationFields')
    fields_definition.fields.append(CustomDataField(
        slug='group',
        label='Group',
        is_required=False,
        choices=['A', 'B', 'C'],
        is_multiple_choice=False
    ))
    fields_definition.save()
    return domain
Example #6
0
def ils_sync_stock_data(request, domain):
    config = ILSGatewayConfig.for_domain(domain)
    domain = config.domain
    endpoint = ILSGatewayEndpoint.from_config(config)
    apis = get_ilsgateway_data_migrations()
    stock_data_task.delay(domain, endpoint, apis, config, ILS_FACILITIES)
    return HttpResponse('OK')
Example #7
0
def migration_task():
    from custom.ilsgateway.stock_data import ILSStockDataSynchronization
    for config in ILSGatewayConfig.get_all_steady_sync_configs():
        if config.enabled:
            endpoint = ILSGatewayEndpoint.from_config(config)
            ils_bootstrap_domain(ILSGatewayAPI(config.domain, endpoint))
            stock_data_task.delay(ILSStockDataSynchronization(config.domain, endpoint))
Example #8
0
def migration_task():
    for config in ILSGatewayConfig.get_all_steady_sync_configs():
        if config.enabled:
            endpoint = ILSGatewayEndpoint.from_config(config)
            ils_bootstrap_domain(ILSGatewayAPI(config.domain, endpoint))
            apis = get_ilsgateway_data_migrations()
            stock_data_task.delay(config.domain, endpoint, apis, config, ILS_FACILITIES)
Example #9
0
def update_historical_data(domain, locations=None):
    """
    If we don't have a record of this supply point being updated, run
    through all historical data and just fill in with zeros.
    """
    org_summaries = OrganizationSummary.objects.order_by('date')
    if org_summaries.count() == 0:
        return

    start_date = org_summaries[0].date

    if locations is None:
        if not ILSGatewayConfig.for_domain(domain).all_stock_data:
            locations = _get_test_locations(domain)
        else:
            locations = Location.by_domain(domain)

    for sp in locations:
        try:
            SupplyPointWarehouseRecord.objects.get(supply_point=sp._id)
        except SupplyPointWarehouseRecord.DoesNotExist:
            # we didn't have a record so go through and historically update
            # anything we maybe haven't touched
            for year, month in months_between(start_date, sp.sql_location.created_at):
                window_date = datetime(year, month, 1)
                for cls in [OrganizationSummary, ProductAvailabilityData, GroupSummary]:
                    _init_warehouse_model(cls, sp, window_date)
            SupplyPointWarehouseRecord.objects.create(supply_point=sp._id,
                                                      create_date=datetime.utcnow())
Example #10
0
def update_historical_data(domain, locations=None):
    """
    If we don't have a record of this supply point being updated, run
    through all historical data and just fill in with zeros.
    """
    org_summaries = OrganizationSummary.objects.order_by('date')
    if org_summaries.count() == 0:
        return

    start_date = org_summaries[0].date

    if locations is None:
        if not ILSGatewayConfig.for_domain(domain).all_stock_data:
            locations = _get_test_locations(domain)
        else:
            locations = Location.by_domain(domain)

    for sp in locations:
        try:
            SupplyPointWarehouseRecord.objects.get(supply_point=sp._id)
        except SupplyPointWarehouseRecord.DoesNotExist:
            # we didn't have a record so go through and historically update
            # anything we maybe haven't touched
            for year, month in months_between(start_date,
                                              sp.sql_location.created_at):
                window_date = datetime(year, month, 1)
                for cls in [
                        OrganizationSummary, ProductAvailabilityData,
                        GroupSummary
                ]:
                    _init_warehouse_model(cls, sp, window_date)
            SupplyPointWarehouseRecord.objects.create(
                supply_point=sp._id, create_date=datetime.utcnow())
Example #11
0
def handle(verified_contact, text, msg=None):
    user = verified_contact.owner if verified_contact else None
    domain = user.domain

    if domain and not ILSGatewayConfig.for_domain(domain):
        return False

    args = text.split()
    if not args:
        return False
    keyword = args[0]
    args = args[1:]
    params = {
        'user': user,
        'domain': domain,
        'args': args,
        'msg': msg,
        'verified_contact': verified_contact
    }

    def not_function(word):
        if args and re.match("del", word):
            return NotDeliveredHandler
        elif args and re.match("sub", word):
            return NotSubmittedHandler
        return None

    handlers = {
        ('soh', 'hmk'): SOHHandler,
        ('submitted', 'nimetuma'): RandrHandler,
        ('delivered', 'dlvd', 'nimepokea'): DeliveredHandler,
        ('sijapokea',): NotDeliveredHandler,
        ('sijatuma',): NotSubmittedHandler,
        ('supervision', 'usimamizi'): SupervisionHandler,
        ('arrived', 'aliwasili'): ArrivedHandler,
        ('help', 'msaada'): HelpHandler,
        ('language', 'lang', 'lugha'): LanguageHandler,
        ('stop', 'acha', 'hapo'): StopHandler,
        ('yes', 'ndio', 'ndyo'): YesHandler,
        ('register', 'reg', 'join', 'sajili'): RegisterHandler,
        ('test',): MessageInitiatior,
        ('not',): not_function(args[0]) if args else None
    }

    def choose_handler(keyword):
        for k, v in handlers.iteritems():
            if keyword in k:
                return v
        return None

    handler_class = choose_handler(keyword)
    handler = handler_class(**params) if handler_class else None

    if handler:
        if args:
            handler.handle()
        else:
            handler.help()
    return False
Example #12
0
def populate_report_data(start_date,
                         end_date,
                         domain,
                         runner,
                         locations=None,
                         strict=True):
    # first populate all the warehouse tables for all facilities
    # hard coded to know this is the first date with data
    start_date = max(start_date, default_start_date())

    # For QA purposes generate reporting data for only some small part of data.
    if not ILSGatewayConfig.for_domain(domain).all_stock_data:
        if locations is None:
            locations = _get_test_locations(domain)
        facilities = filter(
            lambda location: location.location_type == 'FACILITY', locations)
        non_facilities_types = ['DISTRICT', 'REGION', 'MSDZONE', 'MOHSW']
        non_facilities = []
        for location_type in non_facilities_types:
            non_facilities.extend(
                filter(
                    lambda location: location.location_type == location_type,
                    locations))
    else:
        facilities = Location.filter_by_type(domain, 'FACILITY')
        non_facilities = list(Location.filter_by_type(domain, 'DISTRICT'))
        non_facilities += list(Location.filter_by_type(domain, 'REGION'))
        non_facilities += list(Location.filter_by_type(domain, 'MSDZONE'))
        non_facilities += list(Location.filter_by_type(domain, 'MOHSW'))

    if runner.location:
        if runner.location.location_type.name.upper() != 'FACILITY':
            facilities = []
            non_facilities = itertools.dropwhile(
                lambda location: location.location_id != runner.location.
                location_id, non_facilities)
        else:
            facilities = itertools.dropwhile(
                lambda location: location.location_id != runner.location.
                location_id, facilities)

    facilities_chunked_list = chunked(facilities, 5)
    for chunk in facilities_chunked_list:
        res = chain(
            process_facility_warehouse_data.si(fac, start_date, end_date,
                                               runner) for fac in chunk)()
        res.get()

    non_facilities_chunked_list = chunked(non_facilities, 50)

    # then populate everything above a facility off a warehouse table
    for chunk in non_facilities_chunked_list:
        res = chain(
            process_non_facility_warehouse_data.si(
                org, start_date, end_date, runner, strict) for org in chunk)()
        res.get()

    runner.location = None
    runner.save()
    def setUp(self):
        self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)
        config = ILSGatewayConfig(
            domain=TEST_DOMAIN, enabled=True, all_stock_data=True, password='******', username='******',
            url='http//test-api.com/'
        )
        config.save()

        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Loc(**json.loads(f.read())[1])
        self.api_object.prepare_commtrack_config()
        self.api_object.location_sync(location)

        for user in WebUser.by_domain(TEST_DOMAIN):
            user.delete()
Example #14
0
    def handle(self):
        words = self.args
        if len(words) < 2 or len(words) > 3:
            self.respond(REGISTER_HELP)
            return

        name = words[0]
        code = words[1]
        params = {
            "msd_code": code
        }
        if not self.user:
            domains = [config.domain for config in ILSGatewayConfig.get_all_configs()]
            for domain in domains:
                loc = self._get_facility_location(domain, code)
                if not loc:
                    continue

                splited_name = name.split(' ', 1)
                first_name = splited_name[0]
                last_name = splited_name[1] if len(splited_name) > 1 else ""
                clean_name = name.replace(' ', '.')
                username = "******" % (clean_name, domain)
                password = User.objects.make_random_password()
                user = CommCareUser.create(domain=domain, username=username, password=password,
                                           commit=False)
                user.first_name = first_name
                user.last_name = last_name

                if len(words) == 3:
                    user.user_data = {
                        'role': words[2]
                    }

                try:
                    user.set_default_phone_number(self.msg.phone_number.replace('', ''))
                    user.save_verified_number(domain, self.msg.phone_number.replace('', ''), True, self.msg.backend_api)
                except PhoneNumberInUseException as e:
                    v = VerifiedNumber.by_phone(self.msg.phone_number, include_pending=True)
                    v.delete()
                    user.save_verified_number(domain, self.msg.phone_number.replace('', ''), True, self.msg.backend_api)
                except CommCareUser.Inconsistent:
                    continue

                user.language = Languages.DEFAULT

                params.update({
                    'sdp_name': loc.name,
                    'contact_name': name
                })

                dm = user.get_domain_membership(domain)
                dm.location_id = loc._id
                user.save()
                add_location(user, loc._id)

        self.respond(REGISTRATION_CONFIRM, **params)
Example #15
0
 def page_context(self):
     context = super(ILSConfigView, self).page_context
     context['oneoff_tasks'] = OneOffTaskProgress.objects.filter(domain=self.domain)
     ils_config = ILSGatewayConfig.for_domain(self.domain)
     enabled = False
     if ils_config:
         enabled = ils_config.enabled
     context['form'] = ILSConfigForm(initial={'enabled': enabled})
     return context
Example #16
0
def fix_stock_data(domain):
    start_date = '2015-07-01'
    end_date = StockDataCheckpoint.objects.get(domain=domain).date.strftime('%Y-%m-%d')
    with connection.cursor() as c:
        c.execute(
            'DELETE FROM ilsgateway_supplypointstatus WHERE location_id IN '
            '(SELECT location_id FROM locations_sqllocation WHERE domain=%s) AND status_date BETWEEN %s AND %s',
            [domain, start_date, end_date]
        )

        c.execute(
            'DELETE FROM ilsgateway_deliverygroupreport WHERE location_id IN '
            '(SELECT location_id FROM locations_sqllocation WHERE domain=%s) AND report_date BETWEEN %s AND %s',
            [domain, start_date, end_date]
        )

        c.execute(
            "DELETE FROM ilsgateway_groupsummary WHERE org_summary_id IN "
            "(SELECT id FROM ilsgateway_organizationsummary WHERE location_id IN "
            "(SELECT location_id FROM locations_sqllocation WHERE domain=%s) AND date BETWEEN %s AND %s)",
            [domain, start_date, end_date]
        )

        c.execute(
            "DELETE FROM ilsgateway_organizationsummary WHERE location_id IN "
            "(SELECT location_id FROM locations_sqllocation WHERE domain=%s AND date BETWEEN %s AND %s)",
            [domain, start_date, end_date]
        )

    config = ILSGatewayConfig.for_domain(domain)
    endpoint = ILSGatewayEndpoint.from_config(config)

    filters = {'status_date__gte': start_date, 'status_date__lte': end_date}

    offset = 0
    _, statuses = endpoint.get_supplypointstatuses(domain, filters=filters, limit=1000, offset=offset)
    while statuses:
        for status in statuses:
            try:
                SupplyPointStatus.objects.get(external_id=status.external_id, location_id=status.location_id)
            except SupplyPointStatus.DoesNotExist:
                status.save()
        offset += 1000
        _, statuses = endpoint.get_supplypointstatuses(domain, filters=filters, limit=1000, offset=offset)

    filters = {'report_date__gte': start_date, 'report_date__lte': end_date}

    offset = 0
    _, reports = endpoint.get_deliverygroupreports(domain, filters=filters, limit=1000, offset=offset)
    while reports:
        for report in reports:
            try:
                DeliveryGroupReport.objects.get(external_id=report.external_id, location_id=report.location_id)
            except DeliveryGroupReport.DoesNotExist:
                report.save()
        offset += 1000
        _, reports = endpoint.get_deliverygroupreports(domain, filters=filters, limit=1000, offset=offset)
Example #17
0
 def page_context(self):
     context = super(ILSConfigView, self).page_context
     context['oneoff_tasks'] = OneOffTaskProgress.objects.filter(domain=self.domain)
     ils_config = ILSGatewayConfig.for_domain(self.domain)
     enabled = False
     if ils_config:
         enabled = ils_config.enabled
     context['form'] = ILSConfigForm(initial={'enabled': enabled})
     return context
Example #18
0
 def post(self, request, *args, **kwargs):
     payload = json.loads(request.POST.get('json'))
     ils = ILSGatewayConfig.wrap(self.settings_context['source_config'])
     ils.enabled = payload['source_config'].get('enabled', None)
     ils.domain = self.domain_object.name
     ils.url = payload['source_config'].get('url', None)
     ils.username = payload['source_config'].get('username', None)
     ils.password = payload['source_config'].get('password', None)
     ils.save()
     return self.get(request, *args, **kwargs)
Example #19
0
 def tearDownClass(cls):
     MobileBackend.load_by_name(TEST_DOMAIN, TEST_BACKEND).delete()
     CommCareUser.get_by_username('stella').delete()
     CommCareUser.get_by_username('bella').delete()
     CommCareUser.get_by_username('trella').delete()
     CommCareUser.get_by_username('msd_person').delete()
     for product in Product.by_domain(TEST_DOMAIN):
         product.delete()
     SQLProduct.objects.all().delete()
     ILSGatewayConfig.for_domain(TEST_DOMAIN).delete()
     DocDomainMapping.objects.all().delete()
     Location.by_site_code(TEST_DOMAIN, 'loc1').delete()
     Location.by_site_code(TEST_DOMAIN, 'loc2').delete()
     Location.by_site_code(TEST_DOMAIN, 'dis1').delete()
     Location.by_site_code(TEST_DOMAIN, 'reg1').delete()
     Location.by_site_code(TEST_DOMAIN, 'moh1').delete()
     SQLLocation.objects.all().delete()
     generator.delete_all_subscriptions()
     Domain.get_by_name(TEST_DOMAIN).delete()
Example #20
0
 def tearDownClass(cls):
     MobileBackend.load_by_name(TEST_DOMAIN, TEST_BACKEND).delete()
     CommCareUser.get_by_username('stella').delete()
     CommCareUser.get_by_username('bella').delete()
     CommCareUser.get_by_username('trella').delete()
     CommCareUser.get_by_username('msd_person').delete()
     for product in Product.by_domain(TEST_DOMAIN):
         product.delete()
     SQLProduct.objects.all().delete()
     ILSGatewayConfig.for_domain(TEST_DOMAIN).delete()
     DocDomainMapping.objects.all().delete()
     Location.by_site_code(TEST_DOMAIN, 'loc1').delete()
     Location.by_site_code(TEST_DOMAIN, 'loc2').delete()
     Location.by_site_code(TEST_DOMAIN, 'dis1').delete()
     Location.by_site_code(TEST_DOMAIN, 'reg1').delete()
     Location.by_site_code(TEST_DOMAIN, 'moh1').delete()
     SQLLocation.objects.all().delete()
     generator.delete_all_subscriptions()
     Domain.get_by_name(TEST_DOMAIN).delete()
Example #21
0
 def post(self, request, *args, **kwargs):
     payload = json.loads(request.POST.get('json'))
     ils = ILSGatewayConfig.wrap(self.settings_context['source_config'])
     ils.enabled = payload['source_config'].get('enabled', None)
     ils.domain = self.domain_object.name
     ils.url = payload['source_config'].get('url', None)
     ils.username = payload['source_config'].get('username', None)
     ils.password = payload['source_config'].get('password', None)
     ils.save()
     return self.get(request, *args, **kwargs)
 def setUp(self):
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     initial_bootstrap(TEST_DOMAIN)
     config = ILSGatewayConfig()
     config.domain = TEST_DOMAIN
     config.enabled = True
     config.password = '******'
     config.username = '******'
     config.url = 'http://test-api.com/'
     config.save()
     for product in Product.by_domain(TEST_DOMAIN):
         product.delete()
Example #23
0
def prepare_domain(domain_name):
    from corehq.apps.commtrack.tests import bootstrap_domain
    domain = bootstrap_domain(domain_name)
    previous = None
    for name, administrative in [
        ("MOHSW", True),
        ("REGION", True),
        ("DISTRICT", True),
        ("FACILITY", False)
    ]:
        previous, _ = LocationType.objects.get_or_create(
            domain=domain_name,
            name=name,
            parent_type=previous,
            administrative=administrative,
        )

    generator.instantiate_accounting_for_tests()
    account = BillingAccount.get_or_create_account_by_domain(
        domain.name,
        created_by="automated-test",
    )[0]
    plan = DefaultProductPlan.get_default_plan_by_domain(
        domain, edition=SoftwarePlanEdition.ADVANCED
    )
    commtrack = domain.commtrack_settings
    commtrack.actions.append(
        CommtrackActionConfig(action='receipts',
                              keyword='delivered',
                              caption='Delivered')
    )
    commtrack.save()
    subscription = Subscription.new_domain_subscription(
        account,
        domain.name,
        plan
    )
    subscription.is_active = True
    subscription.save()
    ils_config = ILSGatewayConfig(enabled=True, domain=domain.name)
    ils_config.save()
    return domain
Example #24
0
    def settings_context(self):
        config = ILSGatewayConfig.for_domain(self.domain_object.name)

        if config:
            return {
                "source_config": config._doc,
            }
        else:
            return {
                "source_config": ILSGatewayConfig()._doc
            }
Example #25
0
    def setUp(self):
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)
        sms_backend = TestSMSBackend(name="MOBILE_BACKEND_TEST",
                                     is_global=True)
        sms_backend._id = sms_backend.name
        sms_backend.save()

        config = ILSGatewayConfig()
        config.domain = TEST_DOMAIN
        config.enabled = True
        config.password = '******'
        config.username = '******'
        config.url = 'http://test-api.com/'
        config.save()
        for product in Product.by_domain(TEST_DOMAIN):
            product.delete()
Example #26
0
def ils_clear_stock_data_task(domain):
    assert ILSGatewayConfig.for_domain(domain)
    locations = SQLLocation.objects.filter(domain=domain)
    SupplyPointStatus.objects.filter(location_id__in=locations.values_list('location_id', flat=True)).delete()
    DeliveryGroupReport.objects.filter(location_id__in=locations.values_list('location_id', flat=True)).delete()
    products = Product.ids_by_domain(domain)
    StockState.objects.filter(product_id__in=products).delete()
    StockTransaction.objects.filter(
        case_id__in=locations.exclude(supply_point_id__isnull=True).values_list('supply_point_id', flat=True)
    ).delete()
    StockReport.objects.filter(domain=domain).delete()
    StockDataCheckpoint.objects.filter(domain=domain).delete()
Example #27
0
def ils_sync_stock_data(request, domain):
    config = ILSGatewayConfig.for_domain(domain)
    domain = config.domain
    endpoint = ILSGatewayEndpoint.from_config(config)
    apis = (
        ('product_stock', get_product_stock),
        ('stock_transaction', get_stock_transaction),
        ('supply_point_status', get_supply_point_statuses),
        ('delivery_group', get_delivery_group_reports)
    )
    stock_data_task.delay(domain, endpoint, apis, ILS_FACILITIES)
    return HttpResponse('OK')
Example #28
0
def soh_thank_you_task():
    """
    Last business day before the 20th at 4:00 PM Tanzania time
    """
    now = datetime.utcnow()
    business_day = get_business_day_of_month_before(month=now.month, year=now.year, day=20)
    if now.day != business_day.day:
        return

    last_month = datetime(now.year, now.month, 1) - timedelta(days=1)
    for domain in ILSGatewayConfig.get_all_enabled_domains():
        SOHThankYouReminder(domain=domain, date=last_month).send()
Example #29
0
def stock_data_task(domain):
    ilsgateway_config = ILSGatewayConfig.for_domain(domain)
    domain = ilsgateway_config.domain
    endpoint = ILSGatewayEndpoint.from_config(ilsgateway_config)
    commtrack_settings_sync(domain)
    for product in endpoint.get_products():
        sync_ilsgateway_product(domain, product)
    get_locations(domain, endpoint)
    get_product_stock(domain, endpoint)
    get_stock_transaction(domain, endpoint)
    get_supply_point_statuses(domain, endpoint)
    get_delivery_group_reports(domain, endpoint)
Example #30
0
def soh_summary_task():
    """
        6th business day of the month @ 3pm Tanzania time
    """
    now = datetime.utcnow()
    sixth_business_day = get_business_day_of_month(month=now.month, year=now.year, count=6)
    if now.day != sixth_business_day.day:
        return

    for domain in ILSGatewayConfig.get_all_enabled_domains():
        for user in get_district_people(domain):
            send_translated_message(user, REMINDER_MONTHLY_SOH_SUMMARY, **construct_soh_summary(user.location))
Example #31
0
def soh_summary_task():
    """
        6th business day of the month @ 3pm Tanzania time
    """
    now = datetime.utcnow()
    sixth_business_day = get_business_day_of_month(month=now.month, year=now.year, count=6)
    if now.day != sixth_business_day.day:
        return

    for domain in ILSGatewayConfig.get_all_enabled_domains():
        for user in get_district_people(domain):
            send_translated_message(user, REMINDER_MONTHLY_SOH_SUMMARY, **construct_soh_summary(user.location))
Example #32
0
def populate_report_data(start_date, end_date, domain, runner, locations=None, strict=True):
    # first populate all the warehouse tables for all facilities
    # hard coded to know this is the first date with data
    start_date = max(start_date, default_start_date())

    # For QA purposes generate reporting data for only some small part of data.
    if not ILSGatewayConfig.for_domain(domain).all_stock_data:
        if locations is None:
            locations = _get_test_locations(domain)
        facilities = filter(lambda location: location.location_type == 'FACILITY', locations)
        non_facilities_types = ['DISTRICT', 'REGION', 'MSDZONE', 'MOHSW']
        non_facilities = []
        for location_type in non_facilities_types:
            non_facilities.extend(filter(lambda location: location.location_type == location_type, locations))
    else:
        facilities = Location.filter_by_type(domain, 'FACILITY')
        non_facilities = list(Location.filter_by_type(domain, 'DISTRICT'))
        non_facilities += list(Location.filter_by_type(domain, 'REGION'))
        non_facilities += list(Location.filter_by_type(domain, 'MSDZONE'))
        non_facilities += list(Location.filter_by_type(domain, 'MOHSW'))

    if runner.location:
        if runner.location.location_type.name.upper() != 'FACILITY':
            facilities = []
            non_facilities = itertools.dropwhile(
                lambda location: location._id != runner.location.location_id,
                non_facilities
            )
        else:
            facilities = itertools.dropwhile(
                lambda location: location._id != runner.location.location_id,
                facilities
            )

    facilities_chunked_list = chunked(facilities, 5)
    for chunk in facilities_chunked_list:
        res = chain(process_facility_warehouse_data.si(fac, start_date, end_date, runner) for fac in chunk)()
        res.get()

    non_facilities_chunked_list = chunked(non_facilities, 50)

    # then populate everything above a facility off a warehouse table
    for chunk in non_facilities_chunked_list:
        res = chain(
            process_non_facility_warehouse_data.si(org, start_date, end_date, runner, strict)
            for org in chunk
        )()
        res.get()
    runner.location = None
    runner.save()
    # finally go back through the history and initialize empty data for any
    # newly created facilities
    update_historical_data(domain)
Example #33
0
def soh_thank_you_task():
    """
    Last business day before the 20th at 4:00 PM Tanzania time
    """
    now = datetime.utcnow()
    business_day = get_business_day_of_month_before(month=now.month, year=now.year, day=20)
    if now.day != business_day.day:
        return

    last_month = datetime(now.year, now.month, 1) - timedelta(days=1)
    for domain in ILSGatewayConfig.get_all_enabled_domains():
        SOHThankYouReminder(domain=domain, date=last_month).send()
Example #34
0
    def post(self, request, *args, **kwargs):
        ils_config = ILSGatewayConfig.for_domain(self.domain)
        ils_config_form = ILSConfigForm(request.POST)
        if not ils_config_form.is_valid():
            return self.get(request, *args, **kwargs)

        enabled = ils_config_form.cleaned_data['enabled']
        if not ils_config and enabled:
            ils_config = ILSGatewayConfig(enabled=True, domain=self.domain)
            ils_config.save()
        elif ils_config and ils_config.enabled != enabled:
            ils_config.enabled = enabled
            ils_config.save()
        return self.get(request, *args, **kwargs)
Example #35
0
    def setUpClass(cls):
        cls.datapath = os.path.join(os.path.dirname(__file__), 'data')
        cls.sms_backend, cls.sms_backend_mapping = setup_default_sms_test_backend()
        initial_bootstrap(TEST_DOMAIN)

        config = ILSGatewayConfig()
        config.domain = TEST_DOMAIN
        config.enabled = True
        config.password = '******'
        config.username = '******'
        config.url = 'http://test-api.com/'
        config.save()
        for product in Product.by_domain(TEST_DOMAIN):
            product.delete()
    def handle(self, domain, *args, **options):
        if len(args) == 1:
            ilsgateway_id = args[0]
        else:
            ilsgateway_id = 1166  # defaults to bondenzi: http://ilsgateway.com/tz/facility/1166/

        # monkey patch the default start date to cover less data
        updater.default_start_date = lambda: datetime(2015, 1, 1)
        config = ILSGatewayConfig.for_domain(domain)
        assert config.enabled, 'ilsgateway sync must be configured for this domain'
        locations = _get_locations_from_ilsgateway_id(domain, ilsgateway_id)
        _clear_data(domain)
        report_run(domain, locations, strict=False)
Example #37
0
def delivery_summary_task():
    """
        last business day of month 3pm Tanzania time
    """
    now = datetime.utcnow()
    last_business_day = get_business_day_of_month(month=now.month, year=now.year, count=-1)
    if now.day != last_business_day.day:
        return

    for domain in ILSGatewayConfig.get_all_enabled_domains():
        for user in get_district_people(domain):
            send_translated_message(
                user, REMINDER_MONTHLY_DELIVERY_SUMMARY, **construct_delivery_summary(user.location)
            )
Example #38
0
def delivery_summary_task():
    """
        last business day of month 3pm Tanzania time
    """
    now = datetime.utcnow()
    last_business_day = get_business_day_of_month(month=now.month, year=now.year, count=-1)
    if now.day != last_business_day.day:
        return

    for domain in ILSGatewayConfig.get_all_enabled_domains():
        for user in get_district_people(domain):
            send_translated_message(
                user, REMINDER_MONTHLY_DELIVERY_SUMMARY, **construct_delivery_summary(user.location)
            )
    def setUp(self):
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)
        self.sms_backend = TestSMSBackend(name="MOBILE_BACKEND_TEST", is_global=True)
        self.sms_backend._id = self.sms_backend.name
        self.sms_backend.save()

        config = ILSGatewayConfig()
        config.domain = TEST_DOMAIN
        config.enabled = True
        config.password = '******'
        config.username = '******'
        config.url = 'http://test-api.com/'
        config.save()
        for product in Product.by_domain(TEST_DOMAIN):
            product.delete()
Example #40
0
    def setUp(self):
        self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        self.stock_api_object = MockILSStockDataSynchronization(
            TEST_DOMAIN, self.endpoint)
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)
        self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
        self.api_object.prepare_commtrack_config()
        config = ILSGatewayConfig()
        config.domain = TEST_DOMAIN
        config.enabled = True
        config.all_stock_data = True
        config.password = '******'
        config.username = '******'
        config.url = 'http://test-api.com/'
        config.save()
        l1 = Location(name='Test location 1',
                      external_id='3445',
                      location_type='FACILITY',
                      domain=TEST_DOMAIN)

        l2 = Location(name='Test location 2',
                      external_id='4407',
                      location_type='FACILITY',
                      domain=TEST_DOMAIN)

        l1.save()
        l2.save()

        SupplyPointCase.create_from_location(TEST_DOMAIN, l1)
        SupplyPointCase.create_from_location(TEST_DOMAIN, l2)

        with open(os.path.join(self.datapath, 'sample_products.json')) as f:
            for product_json in json.loads(f.read()):
                self.api_object.product_sync(Product(product_json))

        StockTransaction.objects.all().delete()
Example #41
0
def randr_summary_task():
    """
        on 17th day of month or before if it's not a business day @ 3pm Tanzania time
    """

    now = datetime.utcnow()
    business_day = get_business_day_of_month_before(month=now.month, year=now.year, day=17)
    if now.day != business_day.day:
        return

    for domain in ILSGatewayConfig.get_all_enabled_domains():
        for user in get_district_people(domain):
            send_translated_message(
                user, REMINDER_MONTHLY_RANDR_SUMMARY, **construct_randr_summary(user.location)
            )
    def handle(self, domain, *args, **options):
        if len(args) == 1:
            ilsgateway_id = args[0]
        else:
            ilsgateway_id = 1166  # defaults to bondenzi: http://ilsgateway.com/tz/facility/1166/

        # cleanup
        _cleanup_existing_data(domain, ilsgateway_id)

        # migrate
        config = ILSGatewayConfig.for_domain(domain)
        assert config.enabled, 'ilsgateway sync must be configured for this domain'
        endpoint = ILSGatewayEndpoint.from_config(config)
        stock_data_task(domain, endpoint, get_ilsgateway_data_migrations(), config,
                        test_facilities=[ilsgateway_id])
Example #43
0
    def handle(self, domain, *args, **options):
        if len(args) == 1:
            ilsgateway_id = args[0]
        else:
            ilsgateway_id = 1166  # defaults to bondenzi: http://ilsgateway.com/tz/facility/1166/

        # cleanup
        _cleanup_existing_data(domain, ilsgateway_id)

        # migrate
        config = ILSGatewayConfig.for_domain(domain)
        assert config.enabled, 'ilsgateway sync must be configured for this domain'
        endpoint = ILSGatewayEndpoint.from_config(config)
        stock_data_task(domain, endpoint, get_ilsgateway_data_migrations(), config,
                        test_facilities=[ilsgateway_id])
Example #44
0
def randr_summary_task():
    """
        on 17th day of month or before if it's not a business day @ 3pm Tanzania time
    """

    now = datetime.utcnow()
    business_day = get_business_day_of_month_before(month=now.month, year=now.year, day=17)
    if now.day != business_day.day:
        return

    for domain in ILSGatewayConfig.get_all_enabled_domains():
        for user in get_district_people(domain):
            send_translated_message(
                user, REMINDER_MONTHLY_RANDR_SUMMARY, **construct_randr_summary(user.location)
            )
Example #45
0
def populate_report_data(start_date, end_date, domain, runner, locations=None, strict=True):
    # first populate all the warehouse tables for all facilities
    # hard coded to know this is the first date with data
    start_date = max(start_date, default_start_date())

    # For QA purposes generate reporting data for only some small part of data.
    if not ILSGatewayConfig.for_domain(domain).all_stock_data:
        if locations is None:
            locations = _get_test_locations(domain)
        facilities = filter(lambda location: location.location_type == "FACILITY", locations)
        non_facilities_types = ["DISTRICT", "REGION", "MSDZONE", "MOHSW"]
        non_facilities = []
        for location_type in non_facilities_types:
            non_facilities.extend(filter(lambda location: location.location_type == location_type, locations))
    else:
        facilities = Location.filter_by_type(domain, "FACILITY")
        non_facilities = list(Location.filter_by_type(domain, "DISTRICT"))
        non_facilities += list(Location.filter_by_type(domain, "REGION"))
        non_facilities += list(Location.filter_by_type(domain, "MSDZONE"))
        non_facilities += list(Location.filter_by_type(domain, "MOHSW"))

    if runner.location:
        if runner.location.location_type.name.upper() != "FACILITY":
            facilities = []
            non_facilities = itertools.dropwhile(
                lambda location: location.location_id != runner.location.location_id, non_facilities
            )
        else:
            facilities = itertools.dropwhile(
                lambda location: location.location_id != runner.location.location_id, facilities
            )

    facilities_chunked_list = chunked(facilities, 5)
    for chunk in facilities_chunked_list:
        res = chain(process_facility_warehouse_data.si(fac, start_date, end_date, runner) for fac in chunk)()
        res.get()

    non_facilities_chunked_list = chunked(non_facilities, 50)

    # then populate everything above a facility off a warehouse table
    for chunk in non_facilities_chunked_list:
        res = chain(
            process_non_facility_warehouse_data.si(org, start_date, end_date, runner, strict) for org in chunk
        )()
        res.get()

    runner.location = None
    runner.save()
Example #46
0
    def post(self, request, *args, **kwargs):
        ils_config = ILSGatewayConfig.for_domain(self.domain)
        ils_config_form = ILSConfigForm(request.POST)
        if not ils_config_form.is_valid():
            return self.get(request, *args, **kwargs)

        enabled = ils_config_form.cleaned_data['enabled']
        if not ils_config and enabled:
            ils_config = ILSGatewayConfig(enabled=True, domain=self.domain)
            ils_config.save()
        elif ils_config and ils_config.enabled != enabled:
            ils_config.enabled = enabled
            ils_config.save()
        return self.get(request, *args, **kwargs)
Example #47
0
    def tearDownClass(cls):
        delete_domain_phone_numbers(TEST_DOMAIN)
        if cls.sms_backend_mapping.id is not None:
            cls.sms_backend_mapping.delete()
        if cls.sms_backend.id is not None:
            cls.sms_backend.delete()
        users = get_user_docs_by_username([
            'stella',
            'bella',
            'trella',
            'msd_person',
        ])
        if users:
            CommCareUser.bulk_delete([
                CommCareUser.wrap_correctly(user)
                for user in users
            ])

        for product in Product.by_domain(TEST_DOMAIN):
            product.delete()
        SQLProduct.objects.all().delete()
        ils_gateway_config = ILSGatewayConfig.for_domain(TEST_DOMAIN)
        if ils_gateway_config:
            ils_gateway_config.delete()
        DocDomainMapping.objects.all().delete()
        for site_code in [
            'loc1',
            'loc2',
            'dis1',
            'reg1',
            'moh1',
        ]:
            location = cls.get_location_by_site_code(site_code)
            if location:
                location.delete()
        SQLLocation.objects.all().delete()
        test_domain = Domain.get_by_name(TEST_DOMAIN, strict=True)
        if test_domain:
            test_domain.delete()
        super(ILSTestScript, cls).tearDownClass()
Example #48
0
 def tearDownClass(cls):
     delete_domain_phone_numbers(TEST_DOMAIN)
     if cls.sms_backend_mapping.id is not None:
         cls.sms_backend_mapping.delete()
     if cls.sms_backend.id is not None:
         cls.sms_backend.delete()
     for username in [
         'stella',
         'bella',
         'trella',
         'msd_person',
     ]:
         user = CommCareUser.get_by_username(username)
         if user:
             user.delete()
     for product in Product.by_domain(TEST_DOMAIN):
         product.delete()
     SQLProduct.objects.all().delete()
     ils_gateway_config = ILSGatewayConfig.for_domain(TEST_DOMAIN)
     if ils_gateway_config:
         ils_gateway_config.delete()
     DocDomainMapping.objects.all().delete()
     for site_code in [
         'loc1',
         'loc2',
         'dis1',
         'reg1',
         'moh1',
     ]:
         location = Location.by_site_code(TEST_DOMAIN, site_code)
         if location:
             location.delete()
     SQLLocation.objects.all().delete()
     generator.delete_all_subscriptions()
     test_domain = Domain.get_by_name(TEST_DOMAIN)
     if test_domain:
         test_domain.delete()
     super(ILSTestScript, cls).tearDownClass()
Example #49
0
def migration_task():
    configs = ILSGatewayConfig.get_all_configs()
    for config in configs:
        if config.enabled:
            bootstrap_domain(config)
Example #50
0
def bootstrap_domain_task(domain):
    ilsgateway_config = ILSGatewayConfig.for_domain(domain)
    return bootstrap_domain(ilsgateway_config)
Example #51
0
def test_domains_report_run_periodic_task():
    for domain in ILSGatewayConfig.get_all_enabled_domains():
        if domain == 'ils-gateway':
            # skip live domain
            continue
        report_run(domain)
Example #52
0
 def endpoint(self):
     return ILSGatewayEndpoint.from_config(
         ILSGatewayConfig.for_domain(self.domain))
Example #53
0
def send_for_all_domains(date, reminder_class, **kwargs):
    for domain in ILSGatewayConfig.get_all_enabled_domains():
        reminder_class(domain=domain, date=date, **kwargs).send()
Example #54
0
def send_for_all_domains(date, fn, **kwargs):
    for domain in ILSGatewayConfig.get_all_enabled_domains():
        fn(domain, date, **kwargs)
Example #55
0
    def handle(self):
        text = ' '.join(self.msg.text.split()[1:])
        is_district = False
        sp = ""
        msd_code = ""

        if text.find(self.DISTRICT_REG_DELIMITER) != -1:
            phrases = [x.strip() for x in text.split(":")]
            if len(phrases) != 2:
                self.respond(REGISTER_HELP)
                return
            name = phrases[0]
            sp = phrases[1]
            role = Roles.DISTRICT_PHARMACIST
            message = REGISTRATION_CONFIRM_DISTRICT
            params = {}
            is_district = True
        else:
            names = []
            msd_codes = []
            location_regex = '^({prefs})\d+'.format(prefs='|'.join(
                p.lower() for p in DISTRICT_PREFIXES))
            for the_string in self.args:
                if re.match(location_regex, the_string.strip().lower()):
                    msd_codes.append(the_string.strip().lower())
                else:
                    names.append(the_string)

            name = " ".join(names)
            if len(msd_codes) != 1:
                self.respond(REGISTER_HELP)
                return
            else:
                [msd_code] = msd_codes

            role = Roles.IN_CHARGE
            message = REGISTRATION_CONFIRM
            params = {"msd_code": msd_code}

        if not self.user:
            domains = [
                config.domain for config in ILSGatewayConfig.get_all_configs()
            ]
            for domain in domains:
                if is_district:
                    loc = self._get_district_location(domain, sp)
                else:
                    loc = self._get_facility_location(domain, msd_code)
                if not loc:
                    continue
                splited_name = name.split(' ', 1)
                first_name = splited_name[0]
                last_name = splited_name[1] if len(splited_name) > 1 else ""
                clean_name = name.replace(' ', '.')
                username = "******" % (clean_name, domain)
                password = User.objects.make_random_password()
                user = CommCareUser.create(domain=domain,
                                           username=username,
                                           password=password,
                                           commit=False)
                user.first_name = first_name
                user.last_name = last_name
                try:
                    user.set_default_phone_number(
                        self.msg.phone_number.replace('+', ''))
                    user.save_verified_number(
                        domain, self.msg.phone_number.replace('+', ''), True,
                        self.msg.backend_api)
                except PhoneNumberInUseException as e:
                    v = VerifiedNumber.by_phone(self.msg.phone_number,
                                                include_pending=True)
                    v.delete()
                    user.save_verified_number(
                        domain, self.msg.phone_number.replace('+', ''), True,
                        self.msg.backend_api)
                except CommCareUser.Inconsistent:
                    continue
                user.language = Languages.DEFAULT
                params.update({'sdp_name': loc.name, 'contact_name': name})

                user.user_data = {'role': role}

                dm = user.get_domain_membership(domain)
                dm.location_id = loc._id
                user.save()
                add_location(user, loc._id)
        if params:
            self.respond(message, **params)
Example #56
0
def handle(verified_contact, text, msg):
    if verified_contact:
        user = verified_contact.owner
        domain = verified_contact.domain
    elif msg.domain:
        user = None
        domain = msg.domain
    else:
        return False

    if not ILSGatewayConfig.for_domain(domain):
        return False

    text = text.replace('\r', ' ').replace('\n', ' ').strip()
    args = text.split()
    if not args:
        return False
    keyword = args[0]

    if keyword.startswith('#'):
        return False

    args = args[1:]
    params = {
        'user': user,
        'domain': domain,
        'args': args,
        'msg': msg,
        'verified_contact': verified_contact
    }

    def not_function(word):
        if args and re.match("del", word):
            return NotDeliveredHandler
        elif args and re.match("sub", word):
            return NotSubmittedHandler
        return None

    handlers_for_unregistered_or_registered_users = {
        ('register', 'reg', 'join', 'sajili'): RegisterHandler,
    }

    handlers_for_registered_users = {
        ('help', 'msaada'): HelpHandler,
        ('language', 'lang', 'lugha'): LanguageHandler,
        ('stop', 'acha', 'hapo'): StopHandler,
        ('test', ): MessageInitiatior,
    }

    handlers_for_registered_users_with_location = {
        ('soh', 'hmk'): SOHHandler,
        ('delivered', 'dlvd', 'nimepokea'): DeliveredHandler,
        ('sijapokea', ): NotDeliveredHandler,
        ('sijatuma', ): NotSubmittedHandler,
        ('la', 'um'): LossAndAdjustment,
        ('stockout', 'hakuna'): StockoutHandler,
        ('not', ): not_function(args[0]) if args else None,
        ('trans', ): TransferHandler
    }

    if EMG_AND_REC_SMS_HANDLERS.enabled(domain):
        handlers_for_registered_users_with_location[(
            'emg', )] = EmergencyHandler
        handlers_for_registered_users_with_location[('rec', )] = ReceiptHandler

    handler_class = (choose_handler(
        keyword, handlers_for_unregistered_or_registered_users)
                     or choose_handler(keyword, handlers_for_registered_users)
                     or choose_handler(
                         keyword, handlers_for_registered_users_with_location))

    if (not user and handler_class not in list(
            handlers_for_unregistered_or_registered_users.values())):
        return True

    if (handler_class in list(
            handlers_for_registered_users_with_location.values())
            and (not user or not user.location_id)):
        return True

    handler = handler_class(**params) if handler_class else None
    if handler:
        if args:
            return handler.handle()
        else:
            return handler.help()
    elif keyword != 'l':
        with localize(verified_contact.owner.get_language_code()):
            send_sms_to_verified_number(verified_contact,
                                        six.text_type(CONTACT_SUPERVISOR))
        return True
Example #57
0
 def _get_logistics_domains(self):
     from custom.ewsghana.models import EWSGhanaConfig
     return ILSGatewayConfig.get_all_enabled_domains(
     ) + EWSGhanaConfig.get_all_enabled_domains()