def sync_ilsgateway_webuser(domain, ilsgateway_webuser):
    user = WebUser.get_by_username(ilsgateway_webuser.email.lower())
    user_dict = {
        'first_name': ilsgateway_webuser.first_name,
        'last_name': ilsgateway_webuser.last_name,
        'is_staff': ilsgateway_webuser.is_staff,
        'is_active': ilsgateway_webuser.is_active,
        'is_superuser': ilsgateway_webuser.is_superuser,
        'last_login': force_to_datetime(ilsgateway_webuser.last_login),
        'date_joined': force_to_datetime(ilsgateway_webuser.date_joined),
        'password_hashed': True,
    }
    sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                              key=[domain, str(ilsgateway_webuser.location)],
                              reduce=False,
                              include_docs=True,
                              limit=1).first()
    role_id = ilsgateway_webuser.role_id if hasattr(ilsgateway_webuser, 'role_id') else None
    location_id = sp.location_id if sp else None

    if user is None:
        try:
            user = WebUser.create(domain=None, username=ilsgateway_webuser.email.lower(),
                                  password=ilsgateway_webuser.password, email=ilsgateway_webuser.email, **user_dict)
            user.add_domain_membership(domain, role_id=role_id, location_id=location_id)
            user.save()
        except Exception as e:
            logging.error(e)
    else:
        if domain not in user.get_domains():
            user.add_domain_membership(domain, role_id=role_id, location_id=location_id)
            user.save()

    return user
Exemple #2
0
    def web_user_sync(self, ews_webuser):
        username = ews_webuser.email.lower()
        if not username:
            try:
                validate_email(ews_webuser.username)
                username = ews_webuser.username
            except ValidationError:
                return None
        user = WebUser.get_by_username(username)
        user_dict = {
            'first_name': ews_webuser.first_name,
            'last_name': ews_webuser.last_name,
            'is_active': ews_webuser.is_active,
            'last_login': force_to_datetime(ews_webuser.last_login),
            'date_joined': force_to_datetime(ews_webuser.date_joined),
            'password_hashed': True,
        }
        location_id = None
        if ews_webuser.location:
            try:
                location = SQLLocation.objects.get(domain=self.domain, external_id=ews_webuser.location)
                location_id = location.location_id
            except SQLLocation.DoesNotExist:
                pass

        if user is None:
            try:
                user = WebUser.create(domain=None, username=username,
                                      password=ews_webuser.password, email=ews_webuser.email,
                                      **user_dict)
                user.add_domain_membership(self.domain, location_id=location_id)
            except Exception as e:
                logging.error(e)
        else:
            if self.domain not in user.get_domains():
                user.add_domain_membership(self.domain, location_id=location_id)

        ews_webuser_extension(user, ews_webuser)
        dm = user.get_domain_membership(self.domain)

        if dm.location_id != location_id:
            dm.location_id = location_id

        if ews_webuser.is_superuser:
            dm.role_id = UserRole.by_domain_and_name(self.domain, 'Administrator')[0].get_id
        elif ews_webuser.groups and ews_webuser.groups[0].name == 'facility_manager':
            dm.role_id = UserRole.by_domain_and_name(self.domain, 'Facility manager')[0].get_id
        else:
            if ews_webuser.supply_point:
                supply_point = get_supply_point_case_by_domain_external_id(self.domain, ews_webuser.supply_point)
                if supply_point:
                    dm.location_id = supply_point.location_id
                    dm.role_id = UserRole.by_domain_and_name(self.domain, 'Web Reporter')[0].get_id
                else:
                    dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id
            else:
                dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id
        user.save()
        return user
Exemple #3
0
    def web_user_sync(self, ilsgateway_webuser):
        username = ilsgateway_webuser.email.lower()
        if not username:
            try:
                validate_email(ilsgateway_webuser.username)
                username = ilsgateway_webuser.username
            except ValidationError:
                return None
        user = WebUser.get_by_username(username)
        user_dict = {
            'first_name': ilsgateway_webuser.first_name,
            'last_name': ilsgateway_webuser.last_name,
            'is_active': ilsgateway_webuser.is_active,
            # I don't know why sample_webusers.json has these in a number o
            # different formats, w/ and w/o 'T', with timezone offset, etc
            # I do not know how timezones are meant to be handled, so I am
            # conservatively keeping the behavior the same by explicitly
            # stripping the timezone with no conversion
            # todo: this is almost certainly not what was intended
            'last_login': force_to_datetime(ilsgateway_webuser.last_login).replace(tzinfo=None),
            'date_joined': force_to_datetime(ilsgateway_webuser.date_joined).replace(tzinfo=None),
            'password_hashed': True,
        }
        location_id = None
        if ilsgateway_webuser.location:
            try:
                sql_location = SQLLocation.objects.get(domain=self.domain, external_id=ilsgateway_webuser.location)
                location_id = sql_location.location_id
            except SQLLocation.DoesNotExist:
                location_id = None

        if user is None:
            try:
                user = WebUser.create(domain=None, username=username,
                                      password=ilsgateway_webuser.password, email=ilsgateway_webuser.email.lower(),
                                      **user_dict)
                user.add_domain_membership(self.domain, location_id=location_id)
                user.save()
            except Exception as e:
                if settings.UNIT_TESTING:
                    raise
                logging.error(e)
        else:
            if self.domain not in user.get_domains():
                user.add_domain_membership(self.domain, location_id=location_id)
                user.save()
            else:
                dm = user.get_domain_membership(self.domain)
                if dm.location_id != location_id:
                    dm.location_id = location_id
                    user.save()
        return user
Exemple #4
0
def inventory_management(request, domain):

    inventory_management_ds = InventoryManagementData(
        config=dict(
            program=None, products=None, domain=domain,
            startdate=force_to_datetime(request.GET.get('startdate')),
            enddate=force_to_datetime(request.GET.get('enddate')), location_id=request.GET.get('location_id')
        )
    )
    return HttpResponse(
        json.dumps(inventory_management_ds.charts[0].data),
        mimetype='application/json'
    )
Exemple #5
0
def inventory_management(request, domain):

    inventory_management_ds = InventoryManagementData(
        config=dict(program=None,
                    products=None,
                    domain=domain,
                    startdate=force_to_datetime(request.GET.get('startdate')),
                    enddate=force_to_datetime(request.GET.get('enddate')),
                    location_id=request.GET.get('location_id'),
                    custom_date=True))
    return HttpResponse(json.dumps(inventory_management_ds.charts[0].data,
                                   default=json_handler),
                        content_type='application/json')
Exemple #6
0
def stockouts_product(request, domain):

    stockout_graph = StockoutsProduct(
        config=dict(program=None,
                    products=None,
                    domain=domain,
                    startdate=force_to_datetime(request.GET.get('startdate')),
                    enddate=force_to_datetime(request.GET.get('enddate')),
                    location_id=request.GET.get('location_id'),
                    custom_date=True))
    return HttpResponse(json.dumps(stockout_graph.charts[0].data,
                                   default=json_handler),
                        content_type='application/json')
Exemple #7
0
 def update_or_create_sql_object(self, doc):
     # Use get_or_create so that if sql model exists we don't bother saving it,
     # since these models are read-only
     model, created = self.sql_class().objects.get_or_create(
         id=doc['_id'],
         defaults={
             "domain": doc.get("domain"),
             "user_id": doc.get("user_id"),
             "valid": force_to_datetime(doc.get("valid")),
             "expires": force_to_datetime(doc.get("expires")),
             "type": doc.get("type"),
             "key": doc.get("key"),
         })
     return (model, created)
Exemple #8
0
def stockouts_product(request, domain):

    stockout_graph = StockoutsProduct(
        config=dict(
            program=None, products=None, domain=domain,
            startdate=force_to_datetime(request.GET.get('startdate')),
            enddate=force_to_datetime(request.GET.get('enddate')), location_id=request.GET.get('location_id'),
            custom_date=True
        )
    )
    return HttpResponse(
        json.dumps(stockout_graph.charts[0].data, default=json_handler),
        content_type='application/json'
    )
 def wrap_form_json(cls, obj, location_id):
     org_summary_id = obj['org_summary']['id']
     del obj['org_summary']['id']
     obj['org_summary']['external_id'] = org_summary_id
     obj['org_summary']['location_id'] = location_id
     obj['org_summary']['create_date'] = force_to_datetime(obj['org_summary']['create_date'])
     obj['org_summary']['update_date'] = force_to_datetime(obj['org_summary']['update_date'])
     obj['org_summary']['date'] = force_to_datetime(obj['org_summary']['date'])
     try:
         obj['org_summary'] = OrganizationSummary.objects.get(external_id=org_summary_id)
     except OrganizationSummary.DoesNotExist:
         obj['org_summary'] = OrganizationSummary.objects.create(**obj['org_summary'])
     obj['external_id'] = obj['id']
     del obj['id']
     return cls(**obj)
Exemple #10
0
 def wrap_form_json(cls, obj, location_id):
     org_summary_id = obj['org_summary']['id']
     del obj['org_summary']['id']
     obj['org_summary']['external_id'] = org_summary_id
     obj['org_summary']['location_id'] = location_id
     obj['org_summary']['create_date'] = force_to_datetime(obj['org_summary']['create_date'])
     obj['org_summary']['update_date'] = force_to_datetime(obj['org_summary']['update_date'])
     obj['org_summary']['date'] = force_to_datetime(obj['org_summary']['date'])
     try:
         obj['org_summary'] = OrganizationSummary.objects.get(external_id=org_summary_id)
     except OrganizationSummary.DoesNotExist:
         obj['org_summary'] = OrganizationSummary.objects.create(**obj['org_summary'])
     obj['external_id'] = obj['id']
     del obj['id']
     return cls(**obj)
Exemple #11
0
 def wrap_form_json(cls, obj, location_id):
     org_summary_id = obj["org_summary"]["id"]
     del obj["org_summary"]["id"]
     obj["org_summary"]["external_id"] = org_summary_id
     obj["org_summary"]["location_id"] = location_id
     obj["org_summary"]["create_date"] = force_to_datetime(obj["org_summary"]["create_date"])
     obj["org_summary"]["update_date"] = force_to_datetime(obj["org_summary"]["update_date"])
     obj["org_summary"]["date"] = force_to_datetime(obj["org_summary"]["date"])
     try:
         obj["org_summary"] = OrganizationSummary.objects.get(external_id=org_summary_id)
     except OrganizationSummary.DoesNotExist:
         obj["org_summary"] = OrganizationSummary.objects.create(**obj["org_summary"])
     obj["external_id"] = obj["id"]
     del obj["id"]
     return cls(**obj)
Exemple #12
0
 def get_date(cls, type=None, month_or_week=None, year=None, format=ISO_DATE_FORMAT,
              inclusive=True, timezone=pytz.utc):
     if month_or_week is None:
         month_or_week = datetime.datetime.date.today().month
     if year is None:
         year = datetime.datetime.date.today().year
     if type == 2:
         days = month_or_week.split('|')
         start = force_to_datetime(days[0])
         end = force_to_datetime(days[1])
     else:
         start = datetime(year, month_or_week, 1, 0, 0, 0)
         print start
         end = start + relativedelta(months=1) - relativedelta(days=1)
     return DateSpan(start, end, format, inclusive, timezone)
Exemple #13
0
    def web_user_sync(self, ews_webuser):
        if not ews_webuser.is_superuser and ews_webuser.groups:
            group = ews_webuser.groups[0]
            if group.name == 'facility_manager':
                return self.convert_web_user_to_sms_user(ews_webuser)

        username = ews_webuser.email.lower()
        if not username:
            try:
                validate_email(ews_webuser.username)
                username = ews_webuser.username
            except ValidationError:
                return None
        user = WebUser.get_by_username(username)
        user_dict = {
            'first_name': ews_webuser.first_name,
            'last_name': ews_webuser.last_name,
            'is_active': ews_webuser.is_active,
            'last_login': force_to_datetime(ews_webuser.last_login),
            'date_joined': force_to_datetime(ews_webuser.date_joined),
            'password_hashed': True,
        }
        sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                                  key=[self.domain, str(ews_webuser.location)],
                                  reduce=False,
                                  include_docs=True,
                                  limit=1).first()
        location_id = sp.location_id if sp else None

        if user is None:
            try:
                user = WebUser.create(domain=None, username=username,
                                      password=ews_webuser.password, email=ews_webuser.email,
                                      **user_dict)
                user.add_domain_membership(self.domain, location_id=location_id)
            except Exception as e:
                logging.error(e)
        else:
            if self.domain not in user.get_domains():
                user.add_domain_membership(self.domain, location_id=location_id)
        ews_webuser_extension(user, ews_webuser)
        dm = user.get_domain_membership(self.domain)
        if ews_webuser.is_superuser:
            dm.is_admin = True
        else:
            dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id
        user.save()
        return user
Exemple #14
0
def asha_af_report(request, domain):
    report = ASHAAFChecklistData(
        config=dict(doc_id=request.GET.get('doc_id'),
                    date=force_to_datetime(request.GET.get('date')),
                    domain=domain))
    return HttpResponse(json.dumps(report.rows),
                        content_type='application/json')
Exemple #15
0
def get_month(form, prop):
    value = form.form.get(prop, '')
    if value:
        with localize('fr'):
            return format(force_to_datetime(value), 'E')
    else:
        return value
Exemple #16
0
def _get_event_date(config, case_trigger_info=None, payload=None):
    event_date_spec = config.event_date
    event_date = event_date_spec.get_value(case_trigger_info)
    if not event_date:
        event_date = payload.get('received_on')
    event_date = force_to_datetime(event_date)
    return {'eventDate': event_date.strftime("%Y-%m-%d")}
Exemple #17
0
def get_month(form, prop):
    value = form.form.get(prop, "")
    if value:
        with localize("fr"):
            return format(force_to_datetime(value), "E")
    else:
        return value
Exemple #18
0
    def _runCreateUserFromRegistrationTest(self):
        """ 
        test creating of couch user from a registration xmlns.
        this is more of an integration test than a unit test.
        """

        couch_user, created = CommCareUser.create_or_update_from_xform(
            self.xform)
        self.assertEqual(couch_user.user_id, self.uuid)
        # czue: removed lxml reference
        #uuid = ET.fromstring(xml).findtext(".//{http://openrosa.org/user/registration}uuid")
        couch_user = CommCareUser.get_by_user_id(self.xform.form['uuid'])

        self.assertNotEqual(couch_user, None)
        self.assertEqual(couch_user.username,
                         format_username(self.username, self.domain))
        self.assertEqual(couch_user.domain, self.domain)
        self.assertEqual(couch_user.user_id, self.uuid)
        date = datetime.date(datetime.strptime(self.date_string, '%Y-%m-%d'))
        self.assertEqual(couch_user.created_on, force_to_datetime(date))
        self.assertEqual(couch_user.device_ids[0], self.registering_device_id)

        django_user = couch_user.get_django_user()
        self.assertEqual(couch_user.user_id,
                         CouchUser.from_django_user(django_user).user_id)
Exemple #19
0
def _get_event_date(config, case_trigger_info=None, payload=None):
    event_date_spec = config.event_date
    event_date = event_date_spec.get_value(case_trigger_info)
    if not event_date:
        event_date = payload.get('received_on')
    event_date = force_to_datetime(event_date)
    return {'eventDate': event_date.strftime("%Y-%m-%d")}
 def update_or_create_sql_object(self, doc):
     model, created = self.sql_class().objects.update_or_create(
         couch_id=doc['_id'],
         defaults={
             "activation_guid": doc.get('activation_guid'),
             "tos_confirmed": doc.get("tos_confirmed"),
             "request_time": force_to_datetime(doc.get("request_time")),
             "request_ip": doc.get("request_ip"),
             "confirm_time": force_to_datetime(doc.get("confirm_time")),
             "confirm_ip": doc.get("confirm_ip"),
             "domain": doc.get("domain"),
             "new_user_username": doc.get("new_user_username"),
             "requesting_user_username":
             doc.get("requesting_user_username"),
         })
     return (model, created)
    def obj_get_list(self, bundle, **kwargs):
        if kwargs.get('domain'):
            return [self.obj_get(bundle, **kwargs)]
        else:
            filters = {}
            if hasattr(bundle.request, 'GET'):
                filters = bundle.request.GET

            params = {}
            if 'last_modified__lte' in filters:
                params['lte'] = force_to_datetime(filters['last_modified__lte'])

            if 'last_modified__gte' in filters:
                params['gte'] = force_to_datetime(filters['last_modified__gte'])

            return DomainQuerySetAdapter(DomainES().last_modified(**params).sort('last_modified'))
    def obj_get_list(self, bundle, **kwargs):
        if kwargs.get('domain'):
            return [self.obj_get(bundle, **kwargs)]
        else:
            filters = {}
            if hasattr(bundle.request, 'GET'):
                filters = bundle.request.GET

            params = {}
            if 'last_modified__lte' in filters:
                params['lte'] = force_to_datetime(filters['last_modified__lte'])

            if 'last_modified__gte' in filters:
                params['gte'] = force_to_datetime(filters['last_modified__gte'])

            return DomainQuerySetAdapter(DomainES().last_modified(**params).sort('last_modified'))
Exemple #23
0
def get_month(form, prop):
    value = form.form.get(prop, '')
    if value:
        with localize('fr'):
            return format(force_to_datetime(value), 'E')
    else:
        return value
Exemple #24
0
 def __call__(self, item, context=None):
     try:
         date = force_to_datetime(self._month_expression(item, context))
     except ValueError:
         return ''
     if not date:
         return ''
     return str(date.month)
    def rows(self):
        default_row_data = [
            ['', 'Date when cheklist was filled'],
            [1, 'Newborn visits within first day of birth in case of home deliveries'],
            [2, 'Set of home visits for newborn care as specified in the HBNC guidelines (six '
                'visits in case of Institutional delivery and seven in case of a home delivery)'],
            [3, 'Attending VHNDs/Promoting immunization'],
            [4, 'Supporting institutional delivery'],
            [5, 'Management of childhood illness - especially diarrhea and pneumonia'],
            [6, 'Household visits with nutrition counseling'],
            [7, 'Fever cases seen/malaria slides made in malaria endemic area'],
            [8, 'Acting as DOTS provider'],
            [9, 'Holding or attending village/VHSNC meeting'],
            [10, 'Successful referral of the IUD, female sterilization or male '
                 'sterilization cases and/or providing OCPs/Condoms'],
            ['', 'Total of number of tasks on which ASHA reported being functional'],
            ['', 'Total number of ASHAs who are functional on at least 6/10 tasks'],
            ['', 'Remark']
        ]

        properties = ['completed_on', 'hv_fx_home_birth_visits', 'hv_fx_newborns_visited', 'hv_fx_vhnd',
                      'hv_fx_support_inst_delivery', 'hv_fx_child_illness_mgmt', 'hv_fx_nut_counseling',
                      'hv_fx_malaria', 'hv_fx_dots', 'hv_fx_vhsnc', 'hv_fx_fp']

        ttotal = [0] * len(default_row_data)
        total_of_functional = 0
        for asha in self.ashas:
            data = ASHAAFChecklistData(config=dict(
                doc_id=asha['doc_id'],
                date=force_to_datetime(self.request.GET.get('date')),
                domain=self.domain
            )).data
            total = 0
            for idx, p in enumerate(properties):
                if data[p] == 1:
                    ttotal[idx] += 1
                    total += 1
                if p == 'completed_on':
                    default_row_data[idx].append(data[p].strftime('%Y-%m-%d %H:%M'))
                else:
                    default_row_data[idx].append(data[p] if data[p] != 88 else 'NA')
            if total >= 6:
                total_of_functional += 1
            default_row_data[-3].append(total)
            default_row_data[-2].append('{0}/{1}'.format(total, 10))
            default_row_data[-1].append('')

        for idx, row in enumerate(default_row_data):
            if idx == 0:
                row.append('Total no. of ASHAs functional on each tasks')
            elif 0 < idx < 11:
                row.append(ttotal[idx])
            elif idx == 12:
                row.append(total_of_functional)
            else:
                row.append('')

        return default_row_data
Exemple #26
0
def xml_to_stock_report_helper(form, elem):
    tag = elem.tag
    tag = tag[tag.find('}')+1:] # strip out ns
    timestamp = force_to_datetime(elem.attrib.get('date') or form.received_on).replace(tzinfo=None)
    products = elem.findall('./{%s}entry' % stockconst.COMMTRACK_REPORT_XMLNS)
    transactions = [t for prod_entry in products for t in
                    _xml_to_stock_transaction_helper(form.domain, timestamp, tag, elem, prod_entry)]

    return StockReportHelper(form, timestamp, tag, transactions)
Exemple #27
0
    def from_xml(cls, form, config, elem):
        tag = elem.tag
        tag = tag[tag.find('}')+1:] # strip out ns
        timestamp = force_to_datetime(elem.attrib.get('date') or form.received_on).replace(tzinfo=None)
        products = elem.findall('./{%s}entry' % stockconst.COMMTRACK_REPORT_XMLNS)
        transactions = [t for prod_entry in products for t in
                        StockTransaction.from_xml(config, timestamp, tag, elem, prod_entry)]

        return cls(form, timestamp, tag, transactions)
Exemple #28
0
def get_stock_transaction(domain, endpoint):
    # Faking xform
    try:
        xform = XFormInstance.get(docid='ilsgateway-xform')
    except ResourceNotFound:
        xform = XFormInstance(_id='ilsgateway-xform')
        xform.save()

    for facility in FACILITIES:
        has_next = True
        next_url = ""

        while has_next:
            meta, stocktransactions = endpoint.get_stocktransactions(next_url_params=next_url,
                                                                     filters=(dict(supply_point=facility,
                                                                                   order_by='date')))
            for stocktransaction in stocktransactions:
                case = SupplyPointCase.view('hqcase/by_domain_external_id',
                                            key=[domain, str(stocktransaction.supply_point_id)],
                                            reduce=False,
                                            include_docs=True,
                                            limit=1).first()
                product = Product.get_by_code(domain, stocktransaction.product_code)
                try:
                    StockTransaction.objects.get(case_id=case._id,
                                                 product_id=product._id,
                                                 report__date=force_to_datetime(stocktransaction.date),
                                                 stock_on_hand=Decimal(stocktransaction.ending_balance),
                                                 type='stockonhand', report__domain=domain)
                except StockTransaction.DoesNotExist:
                    r = StockReport.objects.create(form_id=xform._id,
                                                   date=force_to_datetime(stocktransaction.date),
                                                   type='balance',
                                                   domain=domain)
                    StockTransaction.objects.create(report=r,
                                                    section_id='stock',
                                                    case_id=case._id,
                                                    product_id=product._id,
                                                    type='stockonhand',
                                                    stock_on_hand=Decimal(stocktransaction.ending_balance))
            if not meta.get('next', False):
                has_next = False
            else:
                next_url = meta['next'].split('?')[1]
Exemple #29
0
def sync_stock_transaction(domain, endpoint, facility, xform, checkpoint,
                           date, limit=100, offset=0):
    has_next = True
    next_url = ""
    while has_next:
        supply_point = facility
        case = SupplyPointCase.view('hqcase/by_domain_external_id',
                                    key=[domain, str(supply_point)],
                                    reduce=False,
                                    include_docs=True,
                                    limit=1).first()
        if not case:
            break
        meta, stocktransactions = endpoint.get_stocktransactions(next_url_params=next_url,
                                                                 limit=limit,
                                                                 offset=offset,
                                                                 filters=(dict(supply_point=supply_point,
                                                                               date__gte=date,
                                                                               order_by='date')))
        save_stock_data_checkpoint(checkpoint,
                                   'stock_transaction',
                                   meta.get('limit') or limit,
                                   meta.get('offset') or offset,
                                   date, facility, True)
        transactions_to_add = []
        with transaction.commit_on_success():
            for stocktransaction in stocktransactions:
                if case:
                    product = Product.get_by_code(domain, stocktransaction.product)
                    report = StockReport(
                        form_id=xform._id,
                        date=force_to_datetime(stocktransaction.date),
                        type='balance',
                        domain=domain
                    )
                    report.save()
                    try:
                        sql_product = SQLProduct.objects.get(product_id=product._id)
                    except SQLProduct.DoesNotExist:
                        continue

                    transactions_to_add.append(StockTransaction(
                        case_id=case._id,
                        product_id=product._id,
                        sql_product=sql_product,
                        section_id='stock',
                        type='stockonhand',
                        stock_on_hand=Decimal(stocktransaction.ending_balance),
                        report=report
                    ))
        # Doesn't send signal
        StockTransaction.objects.bulk_create(transactions_to_add)
        if not meta.get('next', False):
            has_next = False
        else:
            next_url = meta['next'].split('?')[1]
Exemple #30
0
 def date_to_json(self, date):
     if date:
         try:
             date = force_to_datetime(date)
             return (PhoneTime(date, self.timezone).user_time(self.timezone)
                     .ui_string('%d-%m-%Y'))
         except ValueError:
             return ''
     else:
         return ''
Exemple #31
0
 def date_to_json(self, date):
     if date:
         try:
             date = force_to_datetime(date)
             return (PhoneTime(date, self.timezone).user_time(self.timezone)
                     .ui_string('%d-%m-%Y'))
         except ValueError:
             return ''
     else:
         return ''
Exemple #32
0
def sync_ilsgateway_webuser(domain, ilsgateway_webuser):
    username = ilsgateway_webuser.email.lower()
    if not username:
        try:
            validate_email(ilsgateway_webuser.username)
            username = ilsgateway_webuser.username
        except ValidationError:
            return None
    user = WebUser.get_by_username(username)
    user_dict = {
        'first_name': ilsgateway_webuser.first_name,
        'last_name': ilsgateway_webuser.last_name,
        'is_active': ilsgateway_webuser.is_active,
        'last_login': force_to_datetime(ilsgateway_webuser.last_login),
        'date_joined': force_to_datetime(ilsgateway_webuser.date_joined),
        'password_hashed': True,
    }
    sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                              key=[domain, str(ilsgateway_webuser.location)],
                              reduce=False,
                              include_docs=True,
                              limit=1).first()
    role_id = UserRole.get_read_only_role_by_domain(domain).get_id
    location_id = sp.location_id if sp else None

    if user is None:
        try:
            user = WebUser.create(domain=None, username=username,
                                  password=ilsgateway_webuser.password, email=ilsgateway_webuser.email,
                                  **user_dict)
            user.add_domain_membership(domain, is_admin=False, role_id=role_id, location_id=location_id)
            user.save()
            check_hashes(user, user.get_django_user(), ilsgateway_webuser.password)
        except Exception as e:
            logging.error(e)
    else:
        if domain not in user.get_domains():
            user.add_domain_membership(domain, role_id=role_id, location_id=location_id,
                                       is_admin=False)
            user.save()
    return user
Exemple #33
0
def asha_af_report(request, domain):
    report = ASHAAFChecklistData(
        config=dict(
            doc_id=request.GET.get('doc_id'),
            date=force_to_datetime(request.GET.get('date')),
            domain=domain
        )
    )
    return HttpResponse(
        json.dumps(report.rows),
        content_type='application/json'
    )
Exemple #34
0
def sync_ilsgateway_webuser(domain, ilsgateway_webuser):
    username = ilsgateway_webuser.email.lower()
    if not username:
        try:
            validate_email(ilsgateway_webuser.username)
            username = ilsgateway_webuser.username
        except ValidationError:
            return None
    user = WebUser.get_by_username(username)
    user_dict = {
        'first_name': ilsgateway_webuser.first_name,
        'last_name': ilsgateway_webuser.last_name,
        'is_active': ilsgateway_webuser.is_active,
        'last_login': force_to_datetime(ilsgateway_webuser.last_login),
        'date_joined': force_to_datetime(ilsgateway_webuser.date_joined),
        'password_hashed': True,
    }
    sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                              key=[domain, str(ilsgateway_webuser.location)],
                              reduce=False,
                              include_docs=True,
                              limit=1).first()
    role_id = UserRole.get_read_only_role_by_domain(domain).get_id
    location_id = sp.location_id if sp else None

    if user is None:
        try:
            user = WebUser.create(domain=None, username=username,
                                  password=ilsgateway_webuser.password, email=ilsgateway_webuser.email, **user_dict)
            user.add_domain_membership(domain, is_admin=False, role_id=role_id, location_id=location_id)
            user.save()
        except Exception as e:
            logging.error(e)
    else:
        if domain not in user.get_domains():
            user.add_domain_membership(domain, role_id=role_id, location_id=location_id,
                                       is_admin=False)
            user.save()
    return user
 def update_or_create_sql_object(self, doc):
     model, created = self.sql_class().objects.update_or_create(
         couch_id=doc['_id'],
         defaults={
             "email": doc.get("email"),
             "invited_by": doc.get("invited_by"),
             "invited_on": force_to_datetime(doc.get("invited_on")),
             "is_accepted": doc.get("is_accepted", False),
             "domain": doc.get("domain"),
             "role": doc.get("role"),
             "program": doc.get("program"),
             "supply_point": doc.get("supply_point"),
         })
     return (model, created)
Exemple #36
0
    def web_user_sync(self, ilsgateway_webuser):
        username = ilsgateway_webuser.email.lower()
        if not username:
            try:
                validate_email(ilsgateway_webuser.username)
                username = ilsgateway_webuser.username
            except ValidationError:
                return None
        user = WebUser.get_by_username(username)
        user_dict = {
            'first_name': ilsgateway_webuser.first_name,
            'last_name': ilsgateway_webuser.last_name,
            'is_active': ilsgateway_webuser.is_active,
            'last_login': force_to_datetime(ilsgateway_webuser.last_login),
            'date_joined': force_to_datetime(ilsgateway_webuser.date_joined),
            'password_hashed': True,
        }
        try:
            sql_location = SQLLocation.objects.get(domain=self.domain, external_id=ilsgateway_webuser.location)
            location_id = sql_location.location_id
        except SQLLocation.DoesNotExist:
            location_id = None

        if user is None:
            try:
                user = WebUser.create(domain=None, username=username,
                                      password=ilsgateway_webuser.password, email=ilsgateway_webuser.email,
                                      **user_dict)
                user.add_domain_membership(self.domain, location_id=location_id)
                user.save()
            except Exception as e:
                logging.error(e)
        else:
            if self.domain not in user.get_domains():
                user.add_domain_membership(self.domain, location_id=location_id)
                user.save()
        return user
    def update_list(cls, data, month_end):
        for beneficiary in data:
            history = WomanHistory.objects.filter(person_case_id=beneficiary['id']).first()
            safe_history = {}
            if history:
                safe_history = history.date_filter(month_end)

            planning_methods = safe_history.get('family_planning_method')
            if planning_methods:
                beneficiary['currentFamilyPlanningMethod'] = planning_methods[-1][1].replace("\'", '')
                beneficiary['adoptionDateOfFamilyPlaning'] = force_to_datetime(
                    planning_methods[-1][0].replace("\'", '')
                ).date()

        return data
Exemple #38
0
    def create(cls, domain, username, password, email=None, uuid='', date='', **kwargs):
        django_user = create_user(username, password=password, email=email)
        if uuid:
            if not re.match(r'[\w-]+', uuid):
                raise cls.InvalidID('invalid id %r' % uuid)
            couch_user = cls(_id=uuid)
        else:
            couch_user = cls()

        if date:
            couch_user.created_on = force_to_datetime(date)
        else:
            couch_user.created_on = datetime.utcnow()
        couch_user.sync_from_django_user(django_user)
        return couch_user
Exemple #39
0
    def eligible_couple_details(self):
        woman = Woman.objects.get(domain=self.domain, person_case_id=self.person_case_id)
        children_by_sex = Child.objects.filter(mother_case_id=self.person_case_id).aggregate(
            male=Sum(Case(When(sex='M', then=1), output_field=IntegerField())),
            female=Sum(Case(When(sex='F', then=1), output_field=IntegerField())),
        )
        try:
            male_children_died = int(woman.num_male_children_died)
        except TypeError:
            male_children_died = 0
        try:
            female_children_died = int(woman.num_female_children_died)
        except TypeError:
            female_children_died = 0

        data = {
            'maleChildrenBorn': (children_by_sex['male'] or 0) + (male_children_died or 0),
            'femaleChildrenBorn': (children_by_sex['female'] or 0) + (female_children_died or 0),
            'maleChildrenAlive': (children_by_sex['male'] or 0),
            'femaleChildrenAlive': (children_by_sex['female'] or 0),
            'familyPlaningMethod': 'N/A',
            'familyPlanningMethodDate': 'N/A',
            'ashaVisit': 'N/A',
            'previousFamilyPlanningMethod': 'N/A',
            'preferredFamilyPlaningMethod': 'N/A',
        }

        ec_details = woman.eligible_couple_form_details(self.month_end)

        planning_methods = ec_details.get('fp_current_method_history')
        if planning_methods:
            current_method = planning_methods[-1]
            data['familyPlaningMethod'] = current_method[1].replace("\'", '') if current_method[1] else 'N/A'
            data['familyPlanningMethodDate'] = force_to_datetime(
                current_method[0].replace("\'", '')
            ).date() if current_method[0] else 'N/A'
            if len(planning_methods) > 1:
                previous_method = planning_methods[-2][1].replace("\'", '') if planning_methods[-2][1] else 'N/A'
                data['previousFamilyPlanningMethod'] = previous_method

        preferred_methods = ec_details.get('fp_preferred_method_history')
        if preferred_methods:
            data['preferredFamilyPlaningMethod'] = preferred_methods[-1][1] or 'N/A'

        if ec_details.get('family_planning_form_history'):
            data['ashaVisit'] = ec_details.get('family_planning_form_history')[-1]

        return data
Exemple #40
0
    def _runCreateUserFromRegistrationTest(self):
        """
        test creating of couch user from a registration xmlns.
        this is more of an integration test than a unit test.
        """
        couch_user, created = CommCareUser.create_or_update_from_xform(self.xform)
        self.assertEqual(couch_user.user_id, self.uuid)
        # czue: removed lxml reference
        # uuid = ET.fromstring(xml).findtext(".//{http://openrosa.org/user/registration}uuid")
        couch_user = CommCareUser.get_by_user_id(self.xform.form['uuid'])

        self.assertNotEqual(couch_user, None)
        self.assertEqual(couch_user.username, format_username(self.username, self.domain))
        self.assertEqual(couch_user.domain, self.domain)
        self.assertEqual(couch_user.user_id, self.uuid)
        date = iso_string_to_date(self.date_string)
        self.assertEqual(couch_user.created_on, force_to_datetime(date))
        self.assertEqual(couch_user.device_ids[0], self.registering_device_id)

        django_user = couch_user.get_django_user()
        self.assertEqual(couch_user.user_id, CouchUser.from_django_user(django_user).user_id)
def get_transactions(case_id, product_id, section_id, window_start, window_end):
    """
    Given a case/product pair, get transactions in a format ready for consumption calc
    """
    # todo: get rid of this middle layer once the consumption calc has
    # been updated to deal with the regular transaction objects
    SimpleTransaction = collections.namedtuple('SimpleTransaction', ['action', 'value', 'received_on'])

    def _to_consumption_tx(txn):
        if txn.type in (const.TRANSACTION_TYPE_STOCKONHAND, const.TRANSACTION_TYPE_STOCKOUT,
                        const.TRANSACTION_TYPE_LA):
            value = txn.stock_on_hand
        else:
            assert txn.type in (const.TRANSACTION_TYPE_RECEIPTS, const.TRANSACTION_TYPE_CONSUMPTION)
            value = math.fabs(txn.quantity)
        return SimpleTransaction(
            action=txn.type,
            value=value,
            received_on=txn.report.date,
        )

    # todo: beginning of window date filtering
    db_transactions = StockTransaction.objects.filter(
        case_id=case_id, product_id=product_id,
        report__date__gt=window_start,
        report__date__lte=force_to_datetime(window_end),
        section_id=section_id,
    ).order_by('report__date', 'pk')

    first = True
    for db_tx in db_transactions:
        # for the very first transaction, include the previous one if there as well
        # to capture the data on the edge of the window
        if first:
            previous = db_tx.get_previous_transaction()
            if previous:
                yield _to_consumption_tx(db_tx)
            first = False

        yield _to_consumption_tx(db_tx)
Exemple #42
0
def get_transactions(case_id, product_id, section_id, window_start, window_end):
    """
    Given a case/product pair, get transactions in a format ready for consumption calc
    """
    # todo: get rid of this middle layer once the consumption calc has
    # been updated to deal with the regular transaction objects
    SimpleTransaction = collections.namedtuple('SimpleTransaction', ['action', 'value', 'received_on'])

    def _to_consumption_tx(txn):
        if txn.type in (const.TRANSACTION_TYPE_STOCKONHAND, const.TRANSACTION_TYPE_STOCKOUT,
                        const.TRANSACTION_TYPE_LA):
            value = txn.stock_on_hand
        else:
            assert txn.type in (const.TRANSACTION_TYPE_RECEIPTS, const.TRANSACTION_TYPE_CONSUMPTION)
            value = math.fabs(txn.quantity)
        return SimpleTransaction(
            action=txn.type,
            value=value,
            received_on=txn.report.date,
        )

    # todo: beginning of window date filtering
    db_transactions = StockTransaction.objects.filter(
        case_id=case_id, product_id=product_id,
        report__date__gt=window_start,
        report__date__lte=force_to_datetime(window_end),
        section_id=section_id,
    ).order_by('report__date', 'pk')

    first = True
    for db_tx in db_transactions:
        # for the very first transaction, include the previous one if there as well
        # to capture the data on the edge of the window
        if first:
            previous = db_tx.get_previous_transaction()
            if previous:
                yield _to_consumption_tx(db_tx)
            first = False

        yield _to_consumption_tx(db_tx)
Exemple #43
0
def resync_password(config, user):
    email = user.email
    password = user.password
    webuser = WebUser.get_by_username(email.lower())
    if not webuser:
        return

    django_user = webuser.get_django_user()
    domains = webuser.get_domains()
    if not domains:
        return None

    # Make sure that user is migrated and didn't exist before migration
    if all([config.for_domain(domain) is not None for domain in domains]):
        if force_to_datetime(user.date_joined).replace(microsecond=0) != webuser.date_joined:
            return None
        if not check_hashes(webuser, django_user, password):
            logging.info("Logistics: resyncing password...")
            webuser.password = password
            django_user.password = password
            webuser.save()
            django_user.save()
Exemple #44
0
def get_datetimes_from_key(key):
    start, end = key.split("_")
    return [force_to_datetime(start), force_to_datetime(end)]
Exemple #45
0
    def web_user_sync(self, ews_webuser):
        username = ews_webuser.email.lower()
        if not username:
            try:
                validate_email(ews_webuser.username)
                username = ews_webuser.username
            except ValidationError:
                return None
        user = WebUser.get_by_username(username)
        user_dict = {
            'first_name': ews_webuser.first_name,
            'last_name': ews_webuser.last_name,
            'is_active': ews_webuser.is_active,
            'last_login': force_to_datetime(ews_webuser.last_login),
            'date_joined': force_to_datetime(ews_webuser.date_joined),
            'password_hashed': True,
        }
        location_id = None
        if ews_webuser.location:
            try:
                location = SQLLocation.objects.get(
                    domain=self.domain, external_id=ews_webuser.location)
                location_id = location.location_id
            except SQLLocation.DoesNotExist:
                pass

        if user is None:
            try:
                user = WebUser.create(domain=None,
                                      username=username,
                                      password=ews_webuser.password,
                                      email=ews_webuser.email,
                                      **user_dict)
                user.add_domain_membership(self.domain,
                                           location_id=location_id)
            except Exception as e:
                logging.error(e)
        else:
            if self.domain not in user.get_domains():
                user.add_domain_membership(self.domain,
                                           location_id=location_id)

        ews_webuser_extension(user, ews_webuser)
        dm = user.get_domain_membership(self.domain)

        if dm.location_id != location_id:
            dm.location_id = location_id

        if ews_webuser.is_superuser:
            dm.role_id = UserRole.by_domain_and_name(self.domain,
                                                     'Administrator')[0].get_id
        elif ews_webuser.groups and ews_webuser.groups[
                0].name == 'facility_manager':
            dm.role_id = UserRole.by_domain_and_name(
                self.domain, 'Facility manager')[0].get_id
        else:
            if ews_webuser.supply_point:
                supply_point = get_supply_point_case_by_domain_external_id(
                    self.domain, ews_webuser.supply_point)
                if supply_point:
                    dm.location_id = supply_point.location_id
                    dm.role_id = UserRole.by_domain_and_name(
                        self.domain, 'Web Reporter')[0].get_id
                else:
                    dm.role_id = UserRole.get_read_only_role_by_domain(
                        self.domain).get_id
            else:
                dm.role_id = UserRole.get_read_only_role_by_domain(
                    self.domain).get_id
        user.save()
        return user
Exemple #46
0
def sync_stock_transactions_for_facility(domain, endpoint, facility, xform, checkpoint,
                                         date, limit=1000, offset=0):
    """
    Syncs stock data from StockTransaction objects in ILSGateway to StockTransaction objects in HQ
    """
    has_next = True
    next_url = ""
    section_id = 'stock'
    supply_point = facility
    case = get_supply_point_by_external_id(domain, supply_point)
    if not case:
        return

    save_stock_data_checkpoint(checkpoint, 'stock_transaction', limit, offset, date, facility, True)

    products_saved = set()
    while has_next:
        meta, stocktransactions = endpoint.get_stocktransactions(next_url_params=next_url,
                                                                 limit=limit,
                                                                 offset=offset,
                                                                 filters=(dict(supply_point=supply_point,
                                                                               date__gte=date,
                                                                               order_by='date')))

        # set the checkpoint right before the data we are about to process
        meta_limit = meta.get('limit') or limit
        meta_offset = meta.get('offset') or offset
        save_stock_data_checkpoint(checkpoint, 'stock_transaction', meta_limit, meta_offset, date, facility, True)
        transactions_to_add = []
        with transaction.commit_on_success():
            for stocktransaction in stocktransactions:
                params = dict(
                    form_id=xform._id,
                    date=force_to_datetime(stocktransaction.date),
                    type='balance',
                    domain=domain,
                )
                try:
                    report, _ = StockReport.objects.get_or_create(**params)
                except StockReport.MultipleObjectsReturned:
                    # legacy
                    report = StockReport.objects.filter(**params)[0]

                sql_product = SQLProduct.objects.get(code=stocktransaction.product, domain=domain)
                if stocktransaction.quantity != 0:
                    transactions_to_add.append(StockTransaction(
                        case_id=case._id,
                        product_id=sql_product.product_id,
                        sql_product=sql_product,
                        section_id=section_id,
                        type='receipts' if stocktransaction.quantity > 0 else 'consumption',
                        stock_on_hand=Decimal(stocktransaction.ending_balance),
                        quantity=Decimal(stocktransaction.quantity),
                        report=report
                    ))
                transactions_to_add.append(StockTransaction(
                    case_id=case._id,
                    product_id=sql_product.product_id,
                    sql_product=sql_product,
                    section_id=section_id,
                    type='stockonhand',
                    stock_on_hand=Decimal(stocktransaction.ending_balance),
                    report=report
                ))
                products_saved.add(sql_product.product_id)

        if transactions_to_add:
            # Doesn't send signal
            StockTransaction.objects.bulk_create(transactions_to_add)

        if not meta.get('next', False):
            has_next = False
        else:
            next_url = meta['next'].split('?')[1]

    for product in products_saved:
        # if we saved anything rebuild the stock state object by firing the signal
        # on the last transaction for each product
        last_st = StockTransaction.latest(case._id, section_id, product)
        update_stock_state_for_transaction(last_st)
Exemple #47
0
@celery.task(ignore_result=True)
def add_products_to_loc(api):
    endpoint = api.endpoint
    synchronization(None, endpoint.get_locations, api.location_sync, None, None, 100, 0,
                    filters={"is_active": True})


def sync_stock_transaction(stocktransaction, domain, case=None, bulk=False):
    case = case or get_supply_point_case_in_domain_by_id(domain, stocktransaction.supply_point)
    if not case:
        return
    transactions = []
    section_id = 'stock'
    params = dict(
        form_id='logistics-xform',
        date=force_to_datetime(stocktransaction.date),
        type='balance',
        domain=domain
    )
    try:
        report, _ = StockReport.objects.get_or_create(**params)
    except StockReport.MultipleObjectsReturned:
        # legacy
        report = StockReport.objects.filter(**params)[0]

    sql_product = SQLProduct.objects.get(code=stocktransaction.product, domain=domain)
    if stocktransaction.report_type.lower() == 'stock received':
        transactions.append(StockTransaction(
            case_id=case.get_id,
            product_id=sql_product.product_id,
            sql_product=sql_product,
Exemple #48
0
def days_elapsed_from_date(date):
    date = force_to_datetime(date)
    now = datetime.utcnow()
    return (now - date).days
Exemple #49
0
    def web_user_sync(self, ilsgateway_webuser):
        username = ilsgateway_webuser.email.lower()
        if not username:
            try:
                validate_email(ilsgateway_webuser.username)
                username = ilsgateway_webuser.username
            except ValidationError:
                return None
        user = WebUser.get_by_username(username)
        user_dict = {
            'first_name':
            ilsgateway_webuser.first_name,
            'last_name':
            ilsgateway_webuser.last_name,
            'is_active':
            ilsgateway_webuser.is_active,
            # I don't know why sample_webusers.json has these in a number o
            # different formats, w/ and w/o 'T', with timezone offset, etc
            # I do not know how timezones are meant to be handled, so I am
            # conservatively keeping the behavior the same by explicitly
            # stripping the timezone with no conversion
            # todo: this is almost certainly not what was intended
            'last_login':
            force_to_datetime(
                ilsgateway_webuser.last_login).replace(tzinfo=None),
            'date_joined':
            force_to_datetime(
                ilsgateway_webuser.date_joined).replace(tzinfo=None),
            'password_hashed':
            True,
        }
        location_id = None
        if ilsgateway_webuser.location:
            try:
                sql_location = SQLLocation.objects.get(
                    domain=self.domain,
                    external_id=ilsgateway_webuser.location)
                location_id = sql_location.location_id
            except SQLLocation.DoesNotExist:
                location_id = None

        if user is None:
            try:
                user = WebUser.create(domain=None,
                                      username=username,
                                      password=ilsgateway_webuser.password,
                                      email=ilsgateway_webuser.email,
                                      **user_dict)
                user.add_domain_membership(self.domain,
                                           location_id=location_id)
                user.save()
            except Exception as e:
                if settings.UNIT_TESTING:
                    raise
                logging.error(e)
        else:
            if self.domain not in user.get_domains():
                user.add_domain_membership(self.domain,
                                           location_id=location_id)
                user.save()
        return user
    def rows(self):
        default_row_data = [
            ['', _('Date when cheklist was filled')],
            [
                1,
                _('Newborn visits within first day of birth in case of home deliveries'
                  )
            ],
            [
                2,
                _('Set of home visits for newborn care as specified in the HBNC guidelines '
                  '(six visits in case of Institutional delivery and seven in case of a home delivery)'
                  )
            ], [3, _('Attending VHNDs/Promoting immunization')],
            [4, _('Supporting institutional delivery')],
            [
                5,
                _('Management of childhood illness - especially diarrhea and pneumonia'
                  )
            ], [6, _('Household visits with nutrition counseling')],
            [
                7,
                _('Fever cases seen/malaria slides made in malaria endemic area'
                  )
            ], [8, _('Acting as DOTS provider')],
            [9, _('Holding or attending village/VHSNC meeting')],
            [
                10,
                _('Successful referral of the IUD, female sterilization or male '
                  'sterilization cases and/or providing OCPs/Condoms')
            ],
            [
                '',
                _('Total of number of tasks on which ASHA reported being functional'
                  )
            ],
            [
                '',
                _('Total number of ASHAs who are functional on at least %s of the tasks'
                  % '60%')
            ], ['', _('Remark')]
        ]

        properties = [
            'completed_on', 'hv_fx_home_birth_visits',
            'hv_fx_newborns_visited', 'hv_fx_vhnd',
            'hv_fx_support_inst_delivery', 'hv_fx_child_illness_mgmt',
            'hv_fx_nut_counseling', 'hv_fx_malaria', 'hv_fx_dots',
            'hv_fx_vhsnc', 'hv_fx_fp'
        ]

        ttotal = [0] * len(default_row_data)
        total_of_functional = 0
        for asha in self.ashas:
            data = ASHAAFChecklistData(config=dict(
                doc_id=asha['doc_id'],
                date=force_to_datetime(self.request.GET.get('date')),
                domain=self.domain,
                is_checklist=1)).data
            total = 0
            denominator = 0
            for idx, p in enumerate(properties):
                if data[p] == 1:
                    ttotal[idx] += 1
                    total += 1
                if p != 'completed_on' and data[p] != 88:
                    denominator += 1
                if p == 'completed_on':
                    default_row_data[idx].append(
                        data[p].strftime('%Y-%m-%d %H:%M'))
                else:
                    default_row_data[idx].append(
                        data[p] if data[p] != 88 else 'NA')
            try:
                percent = total * 100 / denominator
            except ZeroDivisionError:
                percent = 0
            if percent >= 60:
                total_of_functional += 1
            default_row_data[-3].append(total)
            default_row_data[-2].append('{0}/{1} ({2}%)'.format(
                total, denominator, percent))
            default_row_data[-1].append('')

        for idx, row in enumerate(default_row_data):
            if idx == 0:
                row.append(_('Total no. of ASHAs functional on each tasks'))
            elif 0 < idx < 11:
                row.append(ttotal[idx])
            elif idx == 12:
                row.append(total_of_functional)
            else:
                row.append('')

        return default_row_data
Exemple #51
0
def format_date_string(value):
    if not value:
        return value
    with localize('fr'):
        return format(force_to_datetime(value), 'd E')
Exemple #52
0
 def __call__(self, item, context=None):
     try:
         date = force_to_datetime(item[self.property])
     except ValueError:
         return -1
     return date.isocalendar()[0]
Exemple #53
0
def get_events_from_couch(batch_start_key, start_key, end_key, batch_size, start_doc_id=None):
    navigation_objects = []
    access_objects = []
    records_returned = 0
    next_start_key = None
    nav_couch_ids = []
    access_couch_ids = []
    other_doc_type_count = 0
    processed_doc_id = start_doc_id
    couch_docs = _get_couch_docs(start_key, end_key, batch_size, start_doc_id)
    for result in couch_docs:
        next_start_key = result['key']
        records_returned += 1
        doc = result["doc"]
        kwargs = _pick(doc, ["user", "domain", "ip_address", "session_key",
                            "status_code", "user_agent"])
        kwargs.update({
            "event_date": force_to_datetime(doc.get("event_date")),
            "couch_id": doc["_id"],
        })
        processed_doc_id = doc["_id"]
        if doc["doc_type"] == "NavigationEventAudit":
            nav_couch_ids.append(doc['_id'])
            kwargs.update(_pick(doc, ["headers", "status_code", "view", "view_kwargs"]))
            # Postgres does not play well with control characters in strings
            # Some crafted URLs can contain these charachters, so replacing them with '' in request_path
            # https://stackoverflow.com/a/14946355/3537212
            request_path = re.sub(
                r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]',
                lambda match: repr(match.group(0)),
                doc.get("request_path", "")
            )
            path, _, params = request_path.partition("?")
            kwargs.update({
                "path": path,
                "params": params,
            })
            navigation_objects.append(NavigationEventAudit(**kwargs))
        elif doc["doc_type"] == "AccessAudit":
            access_couch_ids.append(doc['_id'])
            kwargs.update(_pick(doc, ["http_accept", "trace_id"]))
            access_type = doc.get('access_type')
            kwargs.update({
                "access_type": ACCESS_LOOKUP.get(doc.get("access_type")),
                "path": doc.get("path_info"),
            })
            if access_type == "logout":
                kwargs.update({"path": "accounts/logout"})
            access_objects.append(AccessAudit(**kwargs))
        else:
            assert doc["doc_type"] in IGNORED_DOC_TYPES, doc
            other_doc_type_count += 1
    res_obj = get_unsaved_events(
        navigation_objects,
        access_objects,
        nav_couch_ids,
        access_couch_ids,
        batch_start_key,
        end_key
    )

    res_obj.update({
        "break_query": records_returned < batch_size or not next_start_key,
        "next_start_key": next_start_key,
        "last_doc_id": processed_doc_id,
        "other_doc_type_count": other_doc_type_count
    })
    return res_obj
Exemple #54
0
def get_seconds(d):
    import time
    return time.mktime(force_to_datetime(d).utctimetuple())
Exemple #55
0
    def eligible_couple_details(self):
        woman = Woman.objects.get(domain=self.domain,
                                  person_case_id=self.person_case_id)
        children_by_sex = Child.objects.filter(
            mother_case_id=self.person_case_id).aggregate(
                male=Sum(
                    Case(When(sex='M', then=1), output_field=IntegerField())),
                female=Sum(
                    Case(When(sex='F', then=1), output_field=IntegerField())),
            )
        try:
            male_children_died = int(woman.num_male_children_died)
        except TypeError:
            male_children_died = 0
        try:
            female_children_died = int(woman.num_female_children_died)
        except TypeError:
            female_children_died = 0

        data = {
            'maleChildrenBorn':
            (children_by_sex['male'] or 0) + (male_children_died or 0),
            'femaleChildrenBorn':
            (children_by_sex['female'] or 0) + (female_children_died or 0),
            'maleChildrenAlive': (children_by_sex['male'] or 0),
            'femaleChildrenAlive': (children_by_sex['female'] or 0),
            'familyPlaningMethod':
            'N/A',
            'familyPlanningMethodDate':
            'N/A',
            'ashaVisit':
            'N/A',
            'previousFamilyPlanningMethod':
            'N/A',
            'preferredFamilyPlaningMethod':
            'N/A',
        }

        ec_details = woman.eligible_couple_form_details(self.month_end)

        planning_methods = ec_details.get('fp_current_method_history')
        if planning_methods:
            # filter forms that don't update the current planning method (method[1] = '')
            planning_methods = [
                method for method in planning_methods if method[1]
            ]

        if planning_methods:
            current_method = planning_methods[-1]
            data['familyPlaningMethod'] = current_method[1].replace(
                "\'", '') if current_method[1] else 'N/A'
            data['familyPlanningMethodDate'] = force_to_datetime(
                current_method[0].replace(
                    "\'", '')).date() if current_method[0] else 'N/A'
            if len(planning_methods) > 1:
                previous_method = planning_methods[-2][1].replace(
                    "\'", '') if planning_methods[-2][1] else 'N/A'
                data['previousFamilyPlanningMethod'] = previous_method

        preferred_methods = ec_details.get('fp_preferred_method_history')
        if preferred_methods:
            # filter forms that don't update the current preferred method (method[1] = '')
            preferred_methods = [
                method for method in preferred_methods if method[1]
            ]

        if preferred_methods:
            data['preferredFamilyPlaningMethod'] = preferred_methods[-1][
                1] or 'N/A'

        if ec_details.get('family_planning_form_history'):
            data['ashaVisit'] = ec_details.get(
                'family_planning_form_history')[-1]

        return data
Exemple #56
0

def referenced_case_attribute(case, field_name):
    if not case.indices[0]['referenced_id']:
        return ""
    referenced_case = CommCareCase.get(case.indices[0]['referenced_id'])
    if hasattr(referenced_case, field_name):
        return getattr(referenced_case, field_name)
    else:
        return ""


def get_datepart(case, t='n'):
    child_date_of_death = case.get_case_property('child_date_of_death')
    if child_date_of_death:
        return format(force_to_datetime(child_date_of_death), t)
    else:
        return ""


def calculate_weight(case):
    weight_birth = case.get_case_property('weight_birth')
    if weight_birth:
        #Probably measured in grams. Should be converted to kilograms
        if float(weight_birth) > 10:
            return str(float(weight_birth) / 1000.0)
        else:
            return weight_birth
    return ""