示例#1
0
 def get_location_id(self, facility):
     sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                               key=[self.domain, str(facility)],
                               reduce=False,
                               include_docs=True,
                               limit=1).first()
     return sp.location_id
示例#2
0
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
示例#3
0
    def sms_user_sync(self, ews_smsuser, **kwargs):
        sms_user = super(EWSApi, self).sms_user_sync(ews_smsuser, **kwargs)
        if not sms_user:
            return None
        sms_user.user_data['to'] = ews_smsuser.to
        sms_user.save()
        if ews_smsuser.supply_point:
            if ews_smsuser.supply_point.id:
                sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                                          key=[self.domain, str(ews_smsuser.supply_point.id)],
                                          reduce=False,
                                          include_docs=True,
                                          limit=1).first()
            else:
                sp = None

            if sp:
                couch_location = sp.location
            elif ews_smsuser.supply_point.location_id:
                try:
                    location = SQLLocation.objects.get(domain=self.domain,
                                                       external_id=ews_smsuser.supply_point.location_id)
                    couch_location = location.couch_location
                except SQLLocation.DoesNotExist:
                    couch_location = None
            else:
                couch_location = None
            if couch_location:
                sms_user.set_location(couch_location)
        return sms_user
def iter_location_join_supply_point(all_location_ids, chunksize=100):

    # this function was copy-paste-modified from iter_docs

    database = Location.get_db()
    for location_ids in chunked(all_location_ids, chunksize):
        # sync supply point id
        locations = [row.get('doc')
                     for row in get_docs(database, keys=location_ids)
                     if row.get('doc')
                     and row.get('doc')['domain'] not in EXCLUDE_DOMAINS]

        supply_points = SupplyPointCase.view(
            'commtrack/supply_point_by_loc',
            keys=[[location['domain'], location['_id']]
                  for location in locations],
            include_docs=True,
            classes={'CommCareCase': SupplyPointCase},
        ).all()

        supply_points_index = {}

        for supply_point in supply_points:
            key = (supply_point.domain, supply_point.location_id)
            if key in supply_points_index:
                raise Exception(
                    "Multiple supply points have "
                    "domain={!r}, location_id={!r}".format(*key))
            supply_points_index[key] = supply_point

        for location in locations:
            yield (
                location,
                supply_points_index.get((location['domain'], location['_id']))
            )
示例#5
0
def get_supply_point(domain, facility_or_code):
    facility_code = facility_or_code if isinstance(facility_or_code, basestring) else facility_or_code.code
    return SupplyPointCase.view('hqcase/by_domain_external_id',
        key=[domain, facility_code],
        reduce=False,
        include_docs=True,
    ).one()
示例#6
0
def get_supply_point_by_external_id(domain, external_id):
    return SupplyPointCase.view('hqcase/by_domain_external_id',
        key=[domain, str(external_id)],
        reduce=False,
        include_docs=True,
        limit=1
    ).first()
示例#7
0
def get_supply_point_case_by_location_id(domain, location_id):
    from corehq.apps.commtrack.models import SupplyPointCase
    return SupplyPointCase.view(
        'commtrack/supply_point_by_loc',
        key=[domain, location_id],
        include_docs=True,
        classes={'CommCareCase': SupplyPointCase},
    ).one()
示例#8
0
def get_supply_point(domain, facility_or_code):
    facility_code = facility_or_code if isinstance(facility_or_code, basestring) else facility_or_code.code
    return SupplyPointCase.view('hqcase/by_domain_external_id',
        key=[domain, facility_code],
        reduce=False,
        include_docs=True,
        limit=1
    ).first()
示例#9
0
 def get_closed_and_open_by_location_id_and_domain(domain, location_id):
     return SupplyPointCase.view(
         'supply_point_by_loc/view',
         key=[domain, location_id],
         include_docs=True,
         classes={'CommCareCase': SupplyPointCase},
         limit=1,
     ).one()
示例#10
0
    def location_sync(self, ews_location):
        try:
            sql_loc = SQLLocation.objects.get(
                domain=self.domain,
                external_id=int(ews_location.id)
            )
            location = Loc.get(sql_loc.location_id)
        except SQLLocation.DoesNotExist:
            location = None

        if not location:
            if ews_location.parent_id:
                try:
                    loc_parent = SQLLocation.objects.get(
                        external_id=ews_location.parent_id,
                        domain=self.domain
                    )
                    loc_parent_id = loc_parent.location_id
                except SQLLocation.DoesNotExist:
                    parent = self.endpoint.get_location(ews_location.parent_id)
                    loc_parent = self.location_sync(Location(parent))
                    loc_parent_id = loc_parent._id

                location = Loc(parent=loc_parent_id)
            else:
                location = Loc()
                location.lineage = []

            self._set_location_properties(location, ews_location)
            location.save()
            self._set_up_supply_point(location, ews_location)
        else:
            location_dict = {
                'name': ews_location.name,
                'latitude': float(ews_location.latitude) if ews_location.latitude else None,
                'longitude': float(ews_location.longitude) if ews_location.longitude else None,
                'site_code': ews_location.code.lower(),
                'external_id': str(ews_location.id),
            }

            if apply_updates(location, location_dict):
                location.save()
        for supply_point in ews_location.supply_points:
            sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                                      key=[self.domain, str(supply_point.id)],
                                      reduce=False,
                                      include_docs=True,
                                      limit=1).first()
            if sp:
                sql_location = sp.location.sql_location
                sql_location.stocks_all_products = False
                if not sql_location.products:
                    sql_location.products = SQLProduct.objects.filter(
                        domain=self.domain,
                        code__in=supply_point.products
                    )
                    sql_location.save()
        return location
示例#11
0
def get_supply_point_case_in_domain_by_id(domain, supply_point_integer_id):
    from corehq.apps.commtrack.models import SupplyPointCase
    return SupplyPointCase.view(
        'cases_by_domain_external_id/view',
        key=[domain, str(supply_point_integer_id)],
        reduce=False,
        include_docs=True,
        limit=1,
    ).first()
示例#12
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]
示例#13
0
def get_supply_point_case_in_domain_by_id(
        domain, supply_point_integer_id):
    from corehq.apps.commtrack.models import SupplyPointCase
    return SupplyPointCase.view(
        'cases_by_domain_external_id/view',
        key=[domain, str(supply_point_integer_id)],
        reduce=False,
        include_docs=True,
        limit=1,
    ).first()
示例#14
0
def get_supply_point_case_by_location_id(domain, location_id):
    from corehq.apps.commtrack.models import SupplyPointCase
    return SupplyPointCase.view(
        'commtrack/supply_point_by_loc',
        key=[domain, location_id],
        include_docs=True,
        classes={
            'CommCareCase': SupplyPointCase
        },
    ).one()
示例#15
0
def get_supply_point_by_location_id(domain, location_id):
    """
    This also returns closed supply points.
    Please use location.linked_supply_point() instead.
    """
    from corehq.apps.commtrack.models import SupplyPointCase
    return SupplyPointCase.view(
        'supply_point_by_loc/view',
        key=[domain, location_id],
        include_docs=True,
        classes={'CommCareCase': SupplyPointCase},
    ).one()
示例#16
0
def sync_ilsgateway_location(domain, endpoint, ilsgateway_location):
    location = Location.view('commtrack/locations_by_code',
                             key=[domain, ilsgateway_location.code.lower()],
                             include_docs=True).first()
    if not location:
        if ilsgateway_location.parent:
            loc_parent = SupplyPointCase.view('hqcase/by_domain_external_id',
                                              key=[domain, str(ilsgateway_location.parent)],
                                              reduce=False,
                                              include_docs=True).first()
            if not loc_parent:
                parent = endpoint.get_location(ilsgateway_location.parent)
                loc_parent = sync_ilsgateway_location(domain, endpoint, Loc.from_json(parent))
            else:
                loc_parent = loc_parent.location
            location = Location(parent=loc_parent)
        else:
            location = Location()
            location.lineage = []
        location.domain = domain
        location.name = ilsgateway_location.name
        if ilsgateway_location.groups:
            location.metadata = {'groups': ilsgateway_location.groups}
        if ilsgateway_location.latitude:
            location.latitude = float(ilsgateway_location.latitude)
        if ilsgateway_location.longitude:
            location.longitude = float(ilsgateway_location.longitude)
        location.location_type = ilsgateway_location.type
        location.site_code = ilsgateway_location.code
        location.external_id = str(ilsgateway_location.id)
        location.save()
        if not SupplyPointCase.get_by_location(location):
            SupplyPointCase.create_from_location(domain, location)
    else:
        location_dict = {
            'name': ilsgateway_location.name,
            'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None,
            'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None,
            'type': ilsgateway_location.type,
            'site_code': ilsgateway_location.code.lower(),
            'external_id': str(ilsgateway_location.id),
        }
        case = SupplyPointCase.get_by_location(location)
        if apply_updates(location, location_dict):
            location.save()
            if case:
                case.update_from_location(location)
            else:
                SupplyPointCase.create_from_location(domain, location)
    return location
示例#17
0
def save_stock_data_checkpoint(checkpoint, api, limit, offset, date, external_id, commit=True):
    save_checkpoint(checkpoint, api, limit, offset, date, False)
    if external_id:
        supply_point = SupplyPointCase.view('hqcase/by_domain_external_id',
                                            key=[checkpoint.domain, str(external_id)],
                                            reduce=False,
                                            include_docs=True).first()
        if not supply_point:
            return
        checkpoint.location = supply_point.location.sql_location
    else:
        checkpoint.location = None
    if commit:
        checkpoint.save()
示例#18
0
def sync_ilsgateway_location(domain, endpoint, ilsgateway_location):
    location = Location.view('commtrack/locations_by_code',
                             key=[domain, ilsgateway_location.code.lower()],
                             include_docs=True).first()
    if not location:
        if ilsgateway_location.parent:
            loc_parent = SupplyPointCase.view('hqcase/by_domain_external_id',
                                              key=[domain, str(ilsgateway_location.parent)],
                                              reduce=False,
                                              include_docs=True).first()
            if not loc_parent:
                parent = endpoint.get_location(ilsgateway_location.parent)
                loc_parent = sync_ilsgateway_location(domain, endpoint, Loc.from_json(parent))
            else:
                loc_parent = loc_parent.location
            location = Location(parent=loc_parent)
        else:
            location = Location()
            location.lineage = []
        location.domain = domain
        location.name = ilsgateway_location.name
        if ilsgateway_location.groups:
            location.metadata = {'groups': ilsgateway_location.groups}
        if ilsgateway_location.latitude:
            location.latitude = float(ilsgateway_location.latitude)
        if ilsgateway_location.longitude:
            location.longitude = float(ilsgateway_location.longitude)
        location.location_type = ilsgateway_location.type
        location.site_code = ilsgateway_location.code
        location.external_id = str(ilsgateway_location.id)
        location.save()
        if not SupplyPointCase.get_by_location(location):
            SupplyPointCase.create_from_location(domain, location)
    else:
        location_dict = {
            'name': ilsgateway_location.name,
            'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None,
            'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None,
            'type': ilsgateway_location.type,
            'site_code': ilsgateway_location.code.lower(),
            'external_id': str(ilsgateway_location.id),
        }
        case = SupplyPointCase.get_by_location(location)
        if apply_updates(location, location_dict):
            location.save()
            if case:
                case.update_from_location(location)
            else:
                SupplyPointCase.create_from_location(domain, location)
示例#19
0
def sync_product_stock(domain, endpoint, facility, 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()
        meta, product_stocks = endpoint.get_productstocks(
            next_url_params=next_url,
            limit=limit,
            offset=offset,
            filters=dict(supply_point=supply_point, last_modified__gte=date)
        )
        save_stock_data_checkpoint(checkpoint,
                                   'product_stock',
                                   meta.get('limit') or limit,
                                   meta.get('offset') or offset,
                                   date, facility, True)
        for product_stock in product_stocks:
            if case:
                product = Product.get_by_code(domain, product_stock.product)
                try:
                    stock_state = StockState.objects.get(section_id='stock',
                                                         case_id=case._id,
                                                         product_id=product._id)
                    stock_state.last_modified_date = product_stock.last_modified
                    stock_state.stock_on_hand = product_stock.quantity or 0
                except StockState.DoesNotExist:
                    stock_state = StockState(section_id='stock',
                                             case_id=case._id,
                                             product_id=product._id,
                                             stock_on_hand=product_stock.quantity or 0,
                                             last_modified_date=product_stock.last_modified,
                                             sql_product=SQLProduct.objects.get(product_id=product._id))

                if product_stock.auto_monthly_consumption:
                    stock_state.daily_consumption = product_stock.auto_monthly_consumption / DAYS_IN_MONTH
                else:
                    stock_state.daily_consumption = None
                stock_state.save()

        if not meta.get('next', False):
            has_next = False
        else:
            next_url = meta['next'].split('?')[1]
示例#20
0
文件: api.py 项目: jmaina/commcare-hq
    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
示例#21
0
文件: api.py 项目: jmaina/commcare-hq
 def sms_user_sync(self, ilsgateway_smsuser, **kwargs):
     from custom.logistics.commtrack import add_location
     sms_user = super(ILSGatewayAPI, self).sms_user_sync(ilsgateway_smsuser, **kwargs)
     if not sms_user:
         return None
     sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                               key=[self.domain, str(ilsgateway_smsuser.supply_point)],
                               reduce=False,
                               include_docs=True,
                               limit=1).first()
     location_id = sp.location_id if sp else None
     dm = sms_user.get_domain_membership(self.domain)
     dm.location_id = location_id
     sms_user.save()
     add_location(sms_user, location_id)
     return sms_user
示例#22
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]
示例#23
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
示例#24
0
文件: api.py 项目: jmaina/commcare-hq
    def sms_user_sync(self, ews_smsuser, **kwargs):
        sms_user = super(EWSApi, self).sms_user_sync(ews_smsuser, **kwargs)
        if not sms_user:
            return None
        sms_user.user_data['to'] = ews_smsuser.to

        if ews_smsuser.supply_point:
            if ews_smsuser.supply_point.id:
                sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                                          key=[self.domain, str(ews_smsuser.supply_point.id)],
                                          reduce=False,
                                          include_docs=True,
                                          limit=1).first()
            else:
                sp = None

            if sp:
                couch_location_id = sp.location_id
            elif ews_smsuser.supply_point.location_id:
                try:
                    location = SQLLocation.objects.get(domain=self.domain,
                                                       external_id=ews_smsuser.supply_point.location_id)
                    couch_location_id = location.location_id
                except SQLLocation.DoesNotExist:
                    couch_location_id = None
            else:
                couch_location_id = None
            if couch_location_id:
                dm = sms_user.get_domain_membership(self.domain)
                dm.location_id = couch_location_id
                sms_user.save()
                add_location(sms_user, couch_location_id)

        if ews_smsuser.role == 'facility_manager':
            role = UserRole.by_domain_and_name(self.domain, 'Facility manager')
            if role:
                dm = sms_user.get_domain_membership(self.domain)
                dm.role_id = role[0].get_id

        sms_user.save()
        return sms_user
示例#25
0
    def rows(self):
        rows = []
        supply_points = []
        offset = 1000
        meta, chunk = self.endpoint.get_supply_points(limit=1000)
        supply_points.extend(chunk)
        while meta.get('next', False):
            meta, chunk = self.endpoint.get_supply_points(offset=offset, limit=1000)
            offset += 1000
            supply_points.extend(chunk)

        for supply_point in supply_points:
            couch_sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                                            key=[self.domain, str(supply_point.id)],
                                            reduce=False,
                                            include_docs=True).first()
            if not couch_sp:
                rows.append([supply_point.name, supply_point.type,
                            supply_point.code, supply_point.active, supply_point.last_reported, False])

        return rows
示例#26
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
示例#27
0
def get_product_stock(domain, endpoint, facilities):
    for facility in facilities:
        has_next = True
        next_url = ""
        while has_next:
            meta, product_stocks = endpoint.get_productstocks(next_url_params=next_url,
                                                              filters=dict(supply_point=facility))
            for product_stock in product_stocks:
                case = SupplyPointCase.view('hqcase/by_domain_external_id',
                                            key=[domain, str(product_stock.supply_point)],
                                            reduce=False,
                                            include_docs=True,
                                            limit=1).first()
                product = Product.get_by_code(domain, product_stock.product)
                try:
                    stock_state = StockState.objects.get(section_id='stock',
                                                         case_id=case._id,
                                                         product_id=product._id)
                except StockState.DoesNotExist:
                    stock_state = StockState(section_id='stock',
                                             case_id=case._id,
                                             product_id=product._id,
                                             stock_on_hand=product_stock.quantity or 0,
                                             last_modified_date=product_stock.last_modified,
                                             sql_product=SQLProduct.objects.get(product_id=product._id))

                if product_stock.auto_monthly_consumption:
                    stock_state.daily_consumption = product_stock.auto_monthly_consumption / DAYS_IN_MONTH
                elif product_stock.use_auto_consumption is False:
                    stock_state.daily_consumption = product_stock.manual_monthly_consumption / DAYS_IN_MONTH
                else:
                    stock_state.daily_consumption = None

                stock_state.save()

            if not meta.get('next', False):
                has_next = False
            else:
                next_url = meta['next'].split('?')[1]
示例#28
0
def sync_ilsgateway_smsuser(domain, ilsgateway_smsuser):
    domain_part = "%s.commcarehq.org" % domain
    username_part = "%s%d" % (ilsgateway_smsuser.name.strip().replace(' ', '.').lower(), ilsgateway_smsuser.id)
    username = "******" % (username_part[:(128 - len(domain_part))], domain_part)
    #sanity check
    assert len(username) <= 128
    user = CouchUser.get_by_username(username)
    splitted_value = ilsgateway_smsuser.name.split(' ', 1)
    first_name = last_name = ''
    if splitted_value:
        first_name = splitted_value[0][:30]
        last_name = splitted_value[1][:30] if len(splitted_value) > 1 else ''

    user_dict = {
        'first_name': first_name,
        'last_name': last_name,
        'is_active': bool(ilsgateway_smsuser.is_active),
        'email': ilsgateway_smsuser.email,
        'user_data': {}
    }

    if ilsgateway_smsuser.role:
        user_dict['user_data']['role'] = ilsgateway_smsuser.role

    if ilsgateway_smsuser.phone_numbers:
        user_dict['phone_numbers'] = [ilsgateway_smsuser.phone_numbers[0].replace('+', '')]
        user_dict['user_data']['backend'] = ilsgateway_smsuser.backend

    sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                              key=[domain, str(ilsgateway_smsuser.supply_point)],
                              reduce=False,
                              include_docs=True,
                              limit=1).first()
    location_id = sp.location_id if sp else None

    if user is None and username_part:
        try:
            password = User.objects.make_random_password()
            user = CommCareUser.create(domain=domain, username=username, password=password,
                                       email=ilsgateway_smsuser.email, commit=False)
            user.first_name = first_name
            user.last_name = last_name
            user.is_active = bool(ilsgateway_smsuser.is_active)
            user.user_data = user_dict["user_data"]
            if "phone_numbers" in user_dict:
                user.set_default_phone_number(user_dict["phone_numbers"][0])
                try:
                    user.save_verified_number(domain, user_dict["phone_numbers"][0], True, ilsgateway_smsuser.backend)
                except PhoneNumberInUseException as e:
                    v = VerifiedNumber.by_phone(user_dict["phone_numbers"][0], include_pending=True)
                    v.delete()
                    user.save_verified_number(domain, user_dict["phone_numbers"][0], True, ilsgateway_smsuser.backend)
            dm = user.get_domain_membership(domain)
            dm.location_id = location_id
            user.save()
            add_location(user, location_id)

        except Exception as e:
            logging.error(e)
    else:
        dm = user.get_domain_membership(domain)
        current_location_id = dm.location_id if dm else None
        save = False

        if current_location_id != location_id:
            dm.location_id = location_id
            add_location(user, location_id)
            save = True

        if apply_updates(user, user_dict) or save:
            user.save()
    return user
示例#29
0
def stock_data_task(domain, endpoint, apis, test_facilities=None):
    start_date = datetime.today()
    try:
        checkpoint = StockDataCheckpoint.objects.get(domain=domain)
        api = checkpoint.api
        date = checkpoint.date
        limit = checkpoint.limit
        offset = checkpoint.offset
        location = checkpoint.location
        if not checkpoint.start_date:
            checkpoint.start_date = start_date
            checkpoint.save()
        else:
            start_date = checkpoint.start_date
    except StockDataCheckpoint.DoesNotExist:
        checkpoint = StockDataCheckpoint()
        checkpoint.domain = domain
        checkpoint.start_date = start_date
        api = 'product_stock'
        date = None
        limit = 100
        offset = 0
        location = None

    if TEST:
        facilities = test_facilities
    else:
        facilities = SQLLocation.objects.filter(
            domain=domain,
            location_type__iexact='FACILITY'
        ).order_by('created_at').values_list('external_id', flat=True)
    apis_from_checkpoint = itertools.dropwhile(lambda x: x[0] != api, apis)
    facilities_copy = list(facilities)
    if location:
        supply_point = SupplyPointCase.view(
            'commtrack/supply_point_by_loc',
            key=[location.domain, location.location_id],
            include_docs=True,
            classes={'CommCareCase': SupplyPointCase},
        ).one()
        external_id = supply_point.external_id if supply_point else None
        if external_id:
            facilities = itertools.dropwhile(lambda x: int(x) != int(external_id), facilities)

    for idx, api in enumerate(apis_from_checkpoint):
        api[1](
            domain=domain,
            checkpoint=checkpoint,
            date=date,
            limit=limit,
            offset=offset,
            endpoint=endpoint,
            facilities=facilities
        )
        limit = 100
        offset = 0
        if idx == 0:
            facilities = facilities_copy
    save_stock_data_checkpoint(checkpoint, 'product_stock', 100, 0, start_date, None, False)
    checkpoint.start_date = None
    checkpoint.save()
示例#30
0
def sync_ilsgateway_smsuser(domain, ilsgateway_smsuser):
    domain_part = "%s.commcarehq.org" % domain
    username_part = "%s%d" % (ilsgateway_smsuser.name.strip().replace(' ', '.').lower(), ilsgateway_smsuser.id)
    username = "******" % (username_part[:(128 - (len(domain_part) + 1))], domain_part)
    # sanity check
    assert len(username) <= 128
    user = CouchUser.get_by_username(username)
    splitted_value = ilsgateway_smsuser.name.split(' ', 1)
    first_name = last_name = ''
    if splitted_value:
        first_name = splitted_value[0][:30]
        last_name = splitted_value[1][:30] if len(splitted_value) > 1 else ''

    user_dict = {
        'first_name': first_name,
        'last_name': last_name,
        'is_active': bool(ilsgateway_smsuser.is_active),
        'email': ilsgateway_smsuser.email,
        'user_data': {}
    }

    if ilsgateway_smsuser.role:
        user_dict['user_data']['role'] = ilsgateway_smsuser.role

    if ilsgateway_smsuser.phone_numbers:
        user_dict['phone_numbers'] = [ilsgateway_smsuser.phone_numbers[0].replace('+', '')]
        user_dict['user_data']['backend'] = ilsgateway_smsuser.backend

    sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                              key=[domain, str(ilsgateway_smsuser.supply_point)],
                              reduce=False,
                              include_docs=True,
                              limit=1).first()
    location_id = sp.location_id if sp else None

    if user is None and username_part:
        try:
            password = User.objects.make_random_password()
            user = CommCareUser.create(domain=domain, username=username, password=password,
                                       email=ilsgateway_smsuser.email, commit=False)
            user.first_name = first_name
            user.last_name = last_name
            user.is_active = bool(ilsgateway_smsuser.is_active)
            user.user_data = user_dict["user_data"]
            if "phone_numbers" in user_dict:
                user.set_default_phone_number(user_dict["phone_numbers"][0])
                try:
                    user.save_verified_number(domain, user_dict["phone_numbers"][0], True,
                                              ilsgateway_smsuser.backend)
                except PhoneNumberInUseException as e:
                    v = VerifiedNumber.by_phone(user_dict["phone_numbers"][0], include_pending=True)
                    v.delete()
                    user.save_verified_number(domain, user_dict["phone_numbers"][0], True,
                                              ilsgateway_smsuser.backend)
            dm = user.get_domain_membership(domain)
            dm.location_id = location_id
            user.save()
            add_location(user, location_id)

        except Exception as e:
            logging.error(e)
    else:
        dm = user.get_domain_membership(domain)
        current_location_id = dm.location_id if dm else None
        save = False

        if current_location_id != location_id:
            dm.location_id = location_id
            add_location(user, location_id)
            save = True

        if apply_updates(user, user_dict) or save:
            user.save()
    return user
示例#31
0
def sync_ilsgateway_location(domain, endpoint, ilsgateway_location, fetch_groups=False):
    try:
        sql_loc = SQLLocation.objects.get(
            domain=domain,
            external_id=int(ilsgateway_location.id)
        )
        location = Location.get(sql_loc.location_id)
    except SQLLocation.DoesNotExist:
        location = None
    except SQLLocation.MultipleObjectsReturned:
        return

    if not location:
        if ilsgateway_location.parent_id:
            loc_parent = SupplyPointCase.view('hqcase/by_domain_external_id',
                                              key=[domain, str(ilsgateway_location.parent_id)],
                                              reduce=False,
                                              include_docs=True).first()
            if not loc_parent:
                parent = endpoint.get_location(ilsgateway_location.parent_id)
                loc_parent = sync_ilsgateway_location(domain, endpoint, Loc(parent))
            else:
                loc_parent = loc_parent.location
            location = Location(parent=loc_parent)
        else:
            location = Location()
            location.lineage = []
        location.domain = domain
        location.name = ilsgateway_location.name
        if ilsgateway_location.groups:
            location.metadata = {'groups': ilsgateway_location.groups}
        if ilsgateway_location.latitude:
            location.latitude = float(ilsgateway_location.latitude)
        if ilsgateway_location.longitude:
            location.longitude = float(ilsgateway_location.longitude)
        location.location_type = ilsgateway_location.type
        location.site_code = ilsgateway_location.code
        location.external_id = str(ilsgateway_location.id)
        location.save()
        if not SupplyPointCase.get_by_location(location):
            SupplyPointCase.create_from_location(domain, location)
    else:
        location_dict = {
            'name': ilsgateway_location.name,
            'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None,
            'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None,
            'location_type': ilsgateway_location.type,
            'site_code': ilsgateway_location.code.lower(),
            'external_id': str(ilsgateway_location.id),
            'metadata': {}
        }
        if ilsgateway_location.groups:
            location_dict['metadata']['groups'] = ilsgateway_location.groups
        case = SupplyPointCase.get_by_location(location)
        if apply_updates(location, location_dict):
            location.save()
            if case:
                case.update_from_location(location)
            else:
                SupplyPointCase.create_from_location(domain, location)
    if ilsgateway_location.historical_groups:
        historical_groups = ilsgateway_location.historical_groups
    elif fetch_groups:
        location_object = endpoint.get_location(
            ilsgateway_location.id,
            params=dict(with_historical_groups=1)
        )

        historical_groups = Loc(**location_object).historical_groups
    else:
        historical_groups = {}
    for date, groups in historical_groups.iteritems():
        for group in groups:
            HistoricalLocationGroup.objects.get_or_create(date=date, group=group,
                                                          location_id=location.sql_location)

    return location
示例#32
0
def _get_location_id(facility, domain):
        sp = SupplyPointCase.view('hqcase/by_domain_external_id',
                                  key=[domain, str(facility)],
                                  reduce=False,
                                  include_docs=True).first()
        return sp.location_id
def get_supply_point_case_by_domain_external_id(domain, external_id):
    return SupplyPointCase.view('hqcase/by_domain_external_id',
                                key=[domain, str(external_id)],
                                reduce=False,
                                include_docs=True,
                                limit=1).first()