def handle(self, *args, **options):
        self.stdout.write("Fixing default programs...\n")

        for domain in Domain.get_all():
            if not domain.commtrack_enabled:
                continue

            if Program.default_for_domain(domain.name):
                continue

            programs = Program.by_domain(domain.name)

            # filter anything named 'default' or 'Default'
            current_default = [
                p for p in programs
                if p.name == 'Default' or p.name == 'default'
            ]

            # if they never changed their default programs
            # name, we don't want to add a confusing new one
            # so just flip this to the default
            if len(current_default) == 1:
                p.default = True
                p.save()
            else:
                get_or_create_default_program(domain.name)
示例#2
0
class SignalsTest(OpenLMISTestBase):
    requisitions_enabled = True
    program = None

    def createProgram(self):
        self.program = Program()
        self.program.domain = TEST_DOMAIN
        self.program.code = 'QYZ'
        self.program.name = "hiv_program"

        self.program.save()

    def createProducts(self):
        with open(os.path.join(self.datapath, 'sample_product_1.json')) as f:
            lmis_product_1 = json.loads(f.read())

        with open(os.path.join(self.datapath, 'sample_product_2.json')) as f:
            lmis_product_2 = json.loads(f.read())

        lmis_product_1['program_id'] =  self.program._id
        lmis_product_2['program_id'] =  self.program._id

        product_1 = Product(lmis_product_1)
        product_2 = Product(lmis_product_2)
        product_1.save()
        product_2.save()

        self.products = []
        self.products.append(product_1)
        self.products.append(product_2)

    def setUp(self):
        super(SignalsTest, self).setUp()
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        self.spps.clear()
        self.createProgram()
        self.createProducts()

    def fixmetestSyncStockRequisition(self):
        from corehq.apps.commtrack.stockreport import Requisition
        requisition_cases = []
        config = self.domain.commtrack_settings
        for spp in self.spps.values():
            transaction = Requisition(
                config=config,
                product_id=spp.product,
                case_id=spp._id,
                action_name=config.get_action_by_type(RequisitionActions.REQUEST).action_name,
                value=20,
            )
            req = create_requisition(self.user._id, spp, transaction)
            requisition_cases.append(req)
        endpoint = MockOpenLMISSubmitEndpoint("uri://mock/lmis/endpoint", username='ned', password='honor')
        stock_data_submission(sender=None, cases=requisition_cases, endpoint=endpoint)

        for req in requisition_cases:
            self.assertEqual(req.external_id, 'REQ_123')
示例#3
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
        )
示例#4
0
def sync_openlmis_program(domain, lmis_program):
    program = get_program(domain, lmis_program)
    if program is None:
        program = Program(domain=domain)
    else:
        # currently impossible
        raise NotImplementedError('updating existing programs is not yet supported')
    program.name = lmis_program.name
    program.code = lmis_program.code
    program.save()
    if lmis_program.products:
        for lmis_product in lmis_program.products:
            sync_openlmis_product(domain, program, lmis_product)
    return program
示例#5
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)
示例#6
0
 def __init__(self, product, *args, **kwargs):
     self.product = product
     kwargs["initial"] = self.product._doc
     kwargs["initial"]["code"] = self.product.code
     super(ProductForm, self).__init__(*args, **kwargs)
     programs = Program.by_domain(self.product.domain, wrap=False)
     self.fields["program_id"].choices = tuple((prog["_id"], prog["name"]) for prog in programs)
示例#7
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
示例#8
0
 def __init__(self, product, *args, **kwargs):
     self.product = product
     kwargs['initial'] = self.product._doc
     kwargs['initial']['code'] = self.product.code
     super(ProductForm, self).__init__(*args, **kwargs)
     programs = Program.by_domain(self.product.domain, wrap=False)
     self.fields['program_id'].choices = tuple((prog['_id'], prog['name']) for prog in programs)
示例#9
0
    def createProgram(self):
        self.program = Program()
        self.program.domain = TEST_DOMAIN
        self.program.code = 'QYZ'
        self.program.name = "hiv_program"

        self.program.save()
示例#10
0
    def test_program_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = ''
        for program in program_list:
            program_xml += '''
                <program id="{id}">
                    <name>{name}</name>
                    <code>{code}</code>
                </program>
            '''.format(
                id=program.get_id,
                name=program.name,
                code=program.code
            )

        fixture = program_fixture_generator(user, V1, None)

        self.assertXmlEqual('''<fixture id="commtrack:programs" user_id="{user_id}">
                                    <programs>
                                        {programs}
                                    </programs>
                                </fixture>'''.format(user_id=user.user_id, programs=program_xml),
                            ElementTree.tostring(fixture[0]))
示例#11
0
 def setUp(self):
     super(ProgramsTest, self).setUp()
     self.default_program = Program.by_domain(self.domain.name, wrap=True).one()
     self.new_program = make_program(
         self.domain.name,
         'new program',
         'newprogram'
     )
示例#12
0
    def __init__(self, product, *args, **kwargs):
        self.product = product

        kwargs['initial'] = self.product._doc
        kwargs['initial']['code'] = self.product.code

        super(ProductForm, self).__init__(*args, **kwargs)

        programs = Program.by_domain(self.product.domain, wrap=False)
        self.fields['program_id'].choices = tuple((prog['_id'], prog['name']) for prog in programs)

        # make sure to select default program if
        # this is a new product
        if not product._id:
            self.initial['program_id'] = Program.default_for_domain(
                self.product.domain
            )._id
示例#13
0
 def update_params(self):
     self.selected = self.request.GET.get('program')
     user = WebUser.get_by_username(str(self.request.user))
     if not self.selected and self.selected != '':
         self.selected = user.program_id
     self.programs = Program.by_domain(self.domain)
     opts = [dict(val=program.get_id, text=program.name) for program in self.programs]
     self.options = opts
示例#14
0
 def program_data(self):
     data = []
     programs = Program.by_domain(self.domain)
     for p in programs:
         info = p._doc
         info['edit_url'] = reverse('commtrack_program_edit', kwargs={'domain': self.domain, 'prog_id': p._id})
         data.append(info)
     return data
示例#15
0
    def clean_name(self):
        name = self.cleaned_data['name']

        other_programs = [p for p in Program.by_domain(self.program.domain) if p._id != self.program._id]
        if name in [p.name for p in other_programs]:
            raise forms.ValidationError(_('Name already in use'))

        return name
示例#16
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,
        )
    })
示例#17
0
def make_program(domain, name, code, default=False):
    p = Program()
    p.domain = domain
    p.name = name
    p.code = code.lower()
    p.default = default
    p.save()
    return p
示例#18
0
 def __init__(self, *args, **kwargs):
     domain = None
     if 'domain' in kwargs:
         domain = kwargs['domain']
         del kwargs['domain']
     super(CommtrackUserForm, self).__init__(*args, **kwargs)
     self.fields['supply_point'].widget = SupplyPointSelectWidget(domain=domain)
     programs = Program.by_domain(domain, wrap=False)
     choices = list((prog['_id'], prog['name']) for prog in programs)
     choices.insert(0, ('', ''))
     self.fields['program_id'].choices = choices
示例#19
0
    def clean_name(self):
        name = self.cleaned_data['name']

        other_program_names = [
            p['name'] for p in Program.by_domain(self.program.domain, wrap=False)
            if p['_id'] != self.program._id
        ]
        if name in other_program_names:
            raise forms.ValidationError(_('Name already in use'))

        return name
示例#20
0
def get_or_create_default_program(domain):
    program = Program.default_for_domain(domain)

    if program:
        return program
    else:
        return make_program(
            domain,
            _('Uncategorized'),
            _('uncategorized'),
            default=True
        )
示例#21
0
def make_program(domain, name, code):
    p = Program()
    p.domain = domain
    p.name = name
    p.code = code.lower()
    p.save()
    return p
示例#22
0
 def __init__(self, data=None, excluded_emails=None, *args, **kwargs):
     domain = None
     if 'domain' in kwargs:
         domain = Domain.get_by_name(kwargs['domain'])
         del kwargs['domain']
     super(AdminInvitesUserForm, self).__init__(data=data, *args, **kwargs)
     if domain and domain.commtrack_enabled:
         self.fields['supply_point'] = forms.CharField(label='Supply Point:', required=False, widget=SupplyPointSelectWidget(domain=domain.name))
         self.fields['program'] = forms.ChoiceField(label="Program", choices=(), required=False)
         programs = Program.by_domain(domain.name, wrap=False)
         choices = list((prog['_id'], prog['name']) for prog in programs)
         choices.insert(0, ('', ''))
         self.fields['program'].choices = choices
     self.excluded_emails = excluded_emails or []
示例#23
0
    def product_data(self):
        data = []
        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_make_def_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})
            data.append(info)
        return data
示例#24
0
 def __init__(self, data=None, excluded_emails=None, *args, **kwargs):
     domain = None
     if "domain" in kwargs:
         domain = Domain.get_by_name(kwargs["domain"])
         del kwargs["domain"]
     super(AdminInvitesUserForm, self).__init__(data=data, *args, **kwargs)
     if domain and domain.commtrack_enabled:
         self.fields["supply_point"] = forms.CharField(
             label="Supply Point:", required=False, widget=SupplyPointSelectWidget(domain=domain.name)
         )
         self.fields["program"] = forms.ChoiceField(label="Program", choices=(), required=False)
         programs = Program.by_domain(domain.name, wrap=False)
         choices = list((prog["_id"], prog["name"]) for prog in programs)
         choices.insert(0, ("", ""))
         self.fields["program"].choices = choices
     self.excluded_emails = excluded_emails or []
示例#25
0
 def __init__(self, *args, **kwargs):
     domain = None
     if "domain" in kwargs:
         domain = kwargs["domain"]
         del kwargs["domain"]
     if "is_admin" in kwargs:
         attrs = {"is_admin": kwargs["is_admin"]}
         del kwargs["is_admin"]
     else:
         attrs = {"is_admin": False}
     super(CommtrackUserForm, self).__init__(*args, **kwargs)
     self.fields["supply_point"].widget = SupplyPointSelectWidget(domain=domain, attrs=attrs)
     programs = Program.by_domain(domain, wrap=False)
     choices = list((prog["_id"], prog["name"]) for prog in programs)
     choices.insert(0, ("", ""))
     self.fields["program_id"].choices = choices
示例#26
0
    def test_program_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture = program_fixture_generator(user, V1, None, None)

        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture[0])
        )
示例#27
0
def sync_openlmis_program(domain, lmis_program):
    program = get_program(domain, lmis_program)
    if program is None:
        program = Program(domain=domain)

    program.name = lmis_program.name
    program.code = lmis_program.code.lower()
    program._doc_type_attr = "Program"
    program.save()
    if lmis_program.products:
        for lmis_product in lmis_program.products:
            sync_openlmis_product(domain, program, lmis_product)
    return program
示例#28
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 rq in cases:
            product = rq.get_product()
            products.append({'productCode': product.code,
                             'beginningBalance': product.beginningBalance,
                             'quantityReceived': product.quantityReceived,
                             'quantityDispensed': product.quantityDispensed,
                             'lossesAndAdjustments': product.lossesAndAdjustments,
                             'newPatientCount': product.newPatientCount,
                             'stockOnHand': product.stockOnHand,
                             'stockOutDays': product.stockOutDays,
                             'quantityRequested': product.quantityRequested,
                             'reasonForRequestedQuantity': product.reasonForRequestedQuantity,
                             'remarks': product.remarks})

        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 rq in cases:
                rq.external_id = response['requisitionId']
                rq.save()
    def handle(self, *args, **options):
        self.stdout.write("Processing products...\n")

        relevant_ids = set([r['id'] for r in Product.get_db().view(
            'commtrack/products',
            reduce=False,
        ).all()])

        to_save = []

        for product in iter_docs(Product.get_db(), relevant_ids):
            if 'last_modified' not in product or not product['last_modified']:
                product['last_modified'] = datetime.now().isoformat()
                to_save.append(product)

                if len(to_save) > 500:
                    Product.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Product.get_db().bulk_save(to_save)

        self.stdout.write("Processing programs...\n")

        relevant_ids = set([r['id'] for r in Program.get_db().view(
            'commtrack/programs',
            reduce=False,
        ).all()])

        to_save = []

        for program in iter_docs(Program.get_db(), relevant_ids):
            if 'last_modified' not in program or not program['last_modified']:
                program['last_modified'] = datetime.now().isoformat()
                to_save.append(program)

                if len(to_save) > 500:
                    Program.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Program.get_db().bulk_save(to_save)
示例#30
0
 def program(self):
     try:
         return Program.get(self.program_id)
     except ResourceNotFound:
         raise Http404()