Exemplo n.º 1
0
    def test_programs(self):
        self.domain = bootstrap_domain(TEST_DOMAIN)
        self.addCleanup(self.domain.delete)
        bootstrap_products(self.domain.name)
        self.products = sorted(Product.by_domain(self.domain.name), key=lambda p: p._id)
        self.default_program = Program.by_domain(self.domain.name, wrap=True).one()
        self.new_program = make_program(
            self.domain.name,
            'new program',
            'newprogram'
        )
        self.assertTrue(self.default_program.default)
        self.assertFalse(self.new_program.default)

        with self.assertRaises(Exception) as context:
            self.default_program.delete()

        self.assertEqual(six.text_type(context.exception), 'You cannot delete the default program')

        # assign some product to the new program
        self.products[0].program_id = self.new_program._id
        self.products[0].save()

        # make sure start state is okay
        self.assertEqual(
            2,
            len(Program.by_domain(self.domain.name))
        )
        self.assertEqual(2, self.default_program.get_products_count())
        self.assertEqual(1, self.new_program.get_products_count())
        self.assertEqual(
            self.new_program._id,
            self.products[0].program_id
        )
        self.assertEqual(
            self.new_program._id,
            SQLProduct.objects.get(product_id=self.products[0]._id).program_id
        )

        # stash the id before we delete
        new_program_id = self.new_program._id
        self.new_program.delete()

        with self.assertRaises(ResourceNotFound):
            Program.get(new_program_id)

        self.assertEqual(1, len(Program.by_domain(self.domain.name)))
        self.assertEqual(3, self.default_program.get_products_count())
        self.assertEqual(
            self.default_program._id,
            Product.get(self.products[0]._id).program_id
        )
        self.assertEqual(
            self.default_program._id,
            SQLProduct.objects.get(product_id=self.products[0]._id).program_id
        )
Exemplo n.º 2
0
    def test_delete(self):
        # assign some product to the new program
        self.products[0].program_id = self.new_program._id
        self.products[0].save()

        # make sure start state is okay
        self.assertEqual(
            2,
            len(Program.by_domain(self.domain.name))
        )
        self.assertEqual(
            2,
            Product.by_program_id(self.domain.name, self.default_program._id).count()
        )
        self.assertEqual(
            1,
            Product.by_program_id(self.domain.name, self.new_program._id).count()
        )
        self.assertEqual(
            self.new_program._id,
            self.products[0].program_id
        )
        self.assertEqual(
            self.new_program._id,
            SQLProduct.objects.get(product_id=self.products[0]._id).program_id
        )

        # stash the id before we delete
        new_program_id = self.new_program._id
        self.new_program.delete()

        with self.assertRaises(ResourceNotFound):
            Program.get(new_program_id)

        self.assertEqual(
            1,
            len(Program.by_domain(self.domain.name))
        )
        self.assertEqual(
            3,
            Product.by_program_id(self.domain.name, self.default_program._id).count()
        )
        self.assertEqual(
            self.default_program._id,
            Product.get(self.products[0]._id).program_id
        )
        self.assertEqual(
            self.default_program._id,
            SQLProduct.objects.get(product_id=self.products[0]._id).program_id
        )
Exemplo n.º 3
0
    def product_data(self):
        data = []
        if self.show_inactive:
            products = Product.archived_by_domain(
                domain=self.domain,
                limit=self.limit,
                skip=self.skip(),
            )
        else:
            products = Product.by_domain(
                domain=self.domain,
                limit=self.limit,
                skip=self.skip(),
            )

        for p in products:
            if p.program_id:
                program = Program.get(p.program_id)
            else:
                program = get_or_create_default_program(self.domain)
                p.program_id = program.get_id
                p.save()

            info = p._doc
            info['program'] = program.name
            info['edit_url'] = reverse('commtrack_product_edit', kwargs={'domain': self.domain, 'prod_id': p._id})
            info['archive_action_desc'] = self.get_archive_text(self.show_inactive)
            info['archive_action_text'] = _("Un-Archive") if self.show_inactive else _("Archive")
            info['archive_url'] = reverse(
                'unarchive_product' if self.show_inactive else 'archive_product',
                kwargs={'domain': self.domain, 'prod_id': p._id}
            )
            data.append(info)
        return data
Exemplo n.º 4
0
def stock_data_submission(sender, cases, endpoint=None, **kwargs):
    project = Domain.get_by_name(cases[0].domain)

    if project.commtrack_enabled and project.commtrack_settings.openlmis_enabled:

        if endpoint is None:
            endpoint = OpenLMISEndpoint.from_config(project.commtrack_settings.openlmis_config)

        # get agentCode and programCode - I assume all cases are part of the same program
        agentCode = (cases[0].get_supply_point_case()).location.site_code
        programCode = Program.get(cases[0].get_product().program_id).code

        products = []
        for case in cases:
            product = case.get_product()

            product_json = {'productCode': product.code}
            product_json['stockInHand'] = int(case.get_default_value())

            products.append(product_json)

        stock_data = {  'agentCode': agentCode,
                        'programCode': programCode,
                        'products': products
        }
        response = sync_stock_data_to_openlmis(submission=stock_data, openlmis_endpoint=endpoint)

        if response['requisitionId'] is not None:
            for case in cases:
                case.external_id = response['requisitionId']
                case.save()

            cases, send_notification = sync_requisition_from_openlmis(project.name, response['requisitionId'], endpoint)
            if send_notification:
                send_notifications(xform=None, cases=cases)
Exemplo n.º 5
0
def delete_program(request, domain, prog_id):
    program = Program.get(prog_id)
    program.delete()
    return JsonResponse({
        'success': True,
        'message': _("Program '{program_name}' has successfully been deleted.").format(
            program_name=program.name,
        )
    })
Exemplo n.º 6
0
def delete_program(request, domain, prog_id):
    program = Program.get(prog_id)
    program.delete()
    return json_response({
        'success': True,
        'message': _("Program '{program_name}' has successfully been deleted.").format(
            program_name=program.name,
        )
    })
Exemplo n.º 7
0
 def _report_info(self):
     program_id = self.request.GET.get('filter_by_program')
     return [['Title of report', 'Location', 'Date range', 'Program'],
             [
                 self.title,
                 self.location.name if self.location else 'NATIONAL',
                 '{} - {}'.format(ews_date_format(self.datespan.startdate),
                                  ews_date_format(self.datespan.enddate)),
                 'all' if not program_id or program_id == 'all' else
                 Program.get(docid=program_id).name
             ], []]
Exemplo n.º 8
0
 def _report_info(self):
     program_id = self.request.GET.get('filter_by_program')
     return [
         ['Title of report', 'Location', 'Date range', 'Program'],
         [
             self.title,
             self.active_location.name,
             '{} - {}'.format(self.datespan.startdate_display, self.datespan.enddate_display),
             'all' if not program_id or program_id == 'all' else Program.get(docid=program_id).name
         ],
         []
     ]
Exemplo n.º 9
0
 def test_create_product(self):
     with open(os.path.join(self.datapath, 'sample_products.json')) as f:
         product = Product(json.loads(f.read())[0])
     self.assertEqual(0, len(Prod.by_domain(TEST_DOMAIN)))
     ewsghana_product = self.api_object.product_sync(product)
     self.assertEqual(product.sms_code, ewsghana_product.code.lower())
     self.assertEqual(product.name, ewsghana_product.name)
     self.assertEqual(product.description, ewsghana_product.description)
     self.assertEqual(product.units, str(ewsghana_product.unit))
     self.assertIsNotNone(ewsghana_product.program_id)
     program = Program.get(ewsghana_product.program_id)
     self.assertEqual(product.program.name, program.name)
     self.assertEqual(product.program.code, program.code)
Exemplo n.º 10
0
    def test_delete(self):
        # assign some product to the new program
        self.products[0].program_id = self.new_program._id
        self.products[0].save()

        # make sure start state is okay
        self.assertEqual(
            2,
            len(Program.by_domain(self.domain.name))
        )
        self.assertEqual(2, self.default_program.get_products_count())
        self.assertEqual(1, self.new_program.get_products_count())
        self.assertEqual(
            self.new_program._id,
            self.products[0].program_id
        )
        self.assertEqual(
            self.new_program._id,
            SQLProduct.objects.get(product_id=self.products[0]._id).program_id
        )

        # stash the id before we delete
        new_program_id = self.new_program._id
        self.new_program.delete()

        with self.assertRaises(ResourceNotFound):
            Program.get(new_program_id)

        self.assertEqual(1, len(Program.by_domain(self.domain.name)))
        self.assertEqual(3, self.default_program.get_products_count())
        self.assertEqual(
            self.default_program._id,
            Product.get(self.products[0]._id).program_id
        )
        self.assertEqual(
            self.default_program._id,
            SQLProduct.objects.get(product_id=self.products[0]._id).program_id
        )
Exemplo n.º 11
0
 def _report_info(self):
     program_id = self.request.GET.get('filter_by_program')
     return [
         ['Title of report', 'Location', 'Date range', 'Program'],
         [
             self.title,
             self.location.name if self.location else 'NATIONAL',
             '{} - {}'.format(
                 ews_date_format(self.datespan.startdate),
                 ews_date_format(self.datespan.enddate)
             ),
             'all' if not program_id or program_id == 'all' else Program.get(docid=program_id).name
         ],
         []
     ]
Exemplo n.º 12
0
 def report_subtitles(self):
     if self.is_rendered_as_email:
         program = self.request.GET.get('filter_by_program')
         products = self.request.GET.getlist('filter_by_product')
         return mark_safe("""
         <br>For Filters:<br>
         Location: {0}<br>
         Program: {1}<br>
         Product: {2}<br>
         Date range: {3} - {4}
         """.format(
             self.location.name,
             Program.get(program).name if program and program != ALL_OPTION else ALL_OPTION.title(),
             ", ".join(
                 [p.name for p in SQLProduct.objects.filter(product_id__in=products)]
             ) if products != ALL_OPTION and products else ALL_OPTION.title(),
             ews_date_format(self.datespan.startdate_utc),
             ews_date_format(self.datespan.enddate_utc)
         ))
     return None
Exemplo n.º 13
0
 def report_subtitles(self):
     if self.is_rendered_as_email:
         program = self.request.GET.get('filter_by_program')
         products = self.request.GET.getlist('filter_by_product')
         return mark_safe("""
         <br>For Filters:<br>
         Location: {0}<br>
         Program: {1}<br>
         Product: {2}<br>
         Date range: {3} - {4}
         """.format(
             self.location.name,
             Program.get(program).name if program and program != ALL_OPTION
             else ALL_OPTION.title(), ", ".join([
                 p.name
                 for p in SQLProduct.objects.filter(product_id__in=products)
             ])
             if products != ALL_OPTION and products else ALL_OPTION.title(),
             ews_date_format(self.datespan.startdate_utc),
             ews_date_format(self.datespan.enddate_utc)))
     return None
Exemplo n.º 14
0
def stock_data_submission(sender, cases, endpoint=None, **kwargs):
    project = Domain.get_by_name(cases[0].domain)

    if project.commtrack_enabled and project.commtrack_settings.openlmis_enabled:

        if endpoint is None:
            endpoint = OpenLMISEndpoint.from_config(
                project.commtrack_settings.openlmis_config)

        # get agentCode and programCode - I assume all cases are part of the same program
        agentCode = (cases[0].get_supply_point_case()).location.site_code
        programCode = Program.get(cases[0].get_product().program_id).code

        products = []
        for case in cases:
            product = case.get_product()

            product_json = {'productCode': product.code}
            product_json['stockInHand'] = int(case.get_default_value())

            products.append(product_json)

        stock_data = {
            'agentCode': agentCode,
            'programCode': programCode,
            'products': products
        }
        response = sync_stock_data_to_openlmis(submission=stock_data,
                                               openlmis_endpoint=endpoint)

        if response['requisitionId'] is not None:
            for case in cases:
                case.external_id = response['requisitionId']
                case.save()

            cases, send_notification = sync_requisition_from_openlmis(
                project.name, response['requisitionId'], endpoint)
            if send_notification:
                send_notifications(xform=None, cases=cases)
Exemplo n.º 15
0
    def test_create_webuser(self):
        with open(os.path.join(self.datapath, 'sample_webusers.json')) as f:
            webuser = EWSUser(json.loads(f.read())[0])

        self.assertEqual(0, len(WebUser.by_domain(TEST_DOMAIN)))
        ewsghana_webuser = self.api_object.web_user_sync(webuser)
        self.assertEqual(webuser.email, ewsghana_webuser.username)
        self.assertEqual(webuser.password, ewsghana_webuser.password)
        self.assertEqual(webuser.first_name, ewsghana_webuser.first_name)
        self.assertEqual(webuser.last_name, ewsghana_webuser.last_name)
        self.assertEqual(webuser.is_active, ewsghana_webuser.is_active)
        self.assertEqual(False, ewsghana_webuser.is_superuser)
        self.assertEqual(False, ewsghana_webuser.is_staff)

        membership = ewsghana_webuser.get_domain_membership(TEST_DOMAIN)
        self.assertIsNotNone(membership.program_id)
        self.assertEqual(Program.get(membership.program_id).code, 'hiv')
        self.assertIsNone(membership.location_id)
        domain_name = ewsghana_webuser.get_domains()[0]
        self.assertEqual(TEST_DOMAIN, domain_name)
        self.assertEqual(UserRole.get_read_only_role_by_domain(TEST_DOMAIN)._id,
                         membership.role_id)
        ewsghana_webuser.delete()
Exemplo n.º 16
0
 def program(self):
     try:
         return Program.get(self.program_id)
     except ResourceNotFound:
         raise Http404()
Exemplo n.º 17
0
 def program(self):
     try:
         return Program.get(self.program_id)
     except ResourceNotFound:
         raise Http404()