Example #1
0
def sync_facility_to_supply_point(domain, facility):
    supply_point = get_supply_point(domain, facility)
    facility_dict = {
        'domain': domain,
        'location_type': facility.type,
        'external_id': facility.code,
        'name': facility.name,
        'site_code': facility.code,  # todo: do they have a human readable code?
        'latitude': facility.latitude,
        'longitude': facility.longitude,
    }
    parent_sp = None
    if facility.parent_id:
        parent_sp = get_supply_point(domain, facility.parent_id)
        if not parent_sp:
            raise BadParentException('No matching supply point with code %s found' % facility.parent_id)

    if supply_point is None:
        if parent_sp:
            facility_dict['parent'] = parent_sp.location

        facility_loc = Location(**facility_dict)
        facility_loc.save()
        return make_supply_point(domain, facility_loc)
    else:
        facility_loc = supply_point.location
        if parent_sp and facility_loc.parent_id != parent_sp.location._id:
            raise BadParentException('You are trying to move a location. This is currently not supported.')

        should_save = apply_updates(facility_loc, facility_dict)
        if should_save:
            facility_loc.save()

        return supply_point
Example #2
0
def sync_requisition_from_openlmis(domain, requisition_id, openlmis_endpoint):
    cases = []
    send_notification = False
    lmis_requisition_details = openlmis_endpoint.get_requisition_details(requisition_id)
    if lmis_requisition_details:
        rec_cases = [c for c in RequisitionCase.get_by_external_id(domain, str(lmis_requisition_details.id)) if c.type == const.REQUISITION_CASE_TYPE]

        if len(rec_cases) == 0:
            products = [product for product in lmis_requisition_details.products if product.skipped == False]
            for product in products:
                pdt = Product.get_by_code(domain, product.code.lower())
                if pdt:
                    case = lmis_requisition_details.to_requisition_case(pdt._id)
                    case.save()
                    if case.requisition_status == 'AUTHORIZED':
                        send_notification = True
                    cases.append(case)
        else:
            for case in rec_cases:
                before_status = case.requisition_status
                if apply_updates(case, lmis_requisition_details.to_dict(case.product_id)):
                    after_status = case.requisition_status
                    case.save()
                    if before_status in ['INITIATED', 'SUBMITTED'] and after_status == 'AUTHORIZED':
                        send_notification = True
                cases.append(case)
        return cases, send_notification
    else:
        return None, False
Example #3
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
Example #4
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
Example #5
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)
Example #6
0
 def product_sync(self, ilsgateway_product):
     product = Product.get_by_code(self.domain, ilsgateway_product.sms_code)
     product_dict = {
         "domain": self.domain,
         "name": ilsgateway_product.name,
         "code": ilsgateway_product.sms_code,
         "unit": str(ilsgateway_product.units),
         "description": ilsgateway_product.description,
     }
     if product is None:
         product = Product(**product_dict)
         product.save()
     else:
         if apply_updates(product, product_dict):
             product.save()
     return product
Example #7
0
 def product_sync(self, ilsgateway_product):
     product = Product.get_by_code(self.domain, ilsgateway_product.sms_code)
     product_dict = {
         'domain': self.domain,
         'name': ilsgateway_product.name,
         'code': ilsgateway_product.sms_code,
         'unit': str(ilsgateway_product.units),
         'description': ilsgateway_product.description,
     }
     if product is None:
         product = Product(**product_dict)
         product.save()
     else:
         if apply_updates(product, product_dict):
             product.save()
     return product
Example #8
0
 def product_sync(self, ilsgateway_product):
     product = Product.get_by_code(self.domain, ilsgateway_product.sms_code)
     product_dict = {
         'domain': self.domain,
         'name': ilsgateway_product.name,
         'code': ilsgateway_product.sms_code,
         'unit': str(ilsgateway_product.units),
         'description': ilsgateway_product.description,
     }
     if product is None:
         product = Product(**product_dict)
         product.save()
     else:
         if apply_updates(product, product_dict):
             product.save()
     return product
Example #9
0
def sync_openlmis_product(domain, program, lmis_product):
    product = get_product(domain, lmis_product)
    product_dict = {
        'domain': domain,
        'name': lmis_product.name,
        'code': lmis_product.code,
        'unit': str(lmis_product.unit),
        'description': lmis_product.description,
        'category': lmis_product.category,
        'program_id': program._id,
    }
    if product is None:
        product = Product(**product_dict)
        product.save()
    else:
        if apply_updates(product, product_dict):
            product.save()
    return product
Example #10
0
def sync_openlmis_product(domain, program, lmis_product):
    product = get_product(domain, lmis_product)
    product_dict = {
        'domain': domain,
        'name': lmis_product.name,
        'code': lmis_product.code,
        'unit': str(lmis_product.unit),
        'description': lmis_product.description,
        'category': lmis_product.category,
        'program_id': program._id,

    }
    if product is None:
        product = Product(**product_dict)
        product.save()
    else:
        if apply_updates(product, product_dict):
            product.save()
    return product
Example #11
0
    def sms_user_sync(self, ilsgateway_smsuser, username_part=None, password=None,
                      first_name='', last_name=''):
        domain_part = "%s.commcarehq.org" % self.domain
        if not username_part:
            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)
        if not first_name:
            first_name = splitted_value[0][:30] if splitted_value else ''

        if not last_name:
            last_name = splitted_value[1][:30] if len(splitted_value) > 1 else ''

        language = ilsgateway_smsuser.language

        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:
            cleaned_number = apply_leniency(ilsgateway_smsuser.phone_numbers[0])
            if cleaned_number:
                user_dict['phone_numbers'] = [cleaned_number]
                user_dict['user_data']['backend'] = ilsgateway_smsuser.backend

        if user is None and username_part:
            try:
                user_password = password or User.objects.make_random_password()
                user = CommCareUser.create(domain=self.domain, username=username, password=user_password,
                                           email=ilsgateway_smsuser.email, commit=False,
                                           password_hashed=bool(password))
                user.first_name = first_name
                user.last_name = last_name
                user.language = language
                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(self.domain, user_dict["phone_numbers"][0], True)
                    except PhoneNumberInUseException as e:
                        self._reassign_number(user, user_dict["phone_numbers"][0])
                    except InvalidFormatException:
                        pass
            except Exception as e:
                logging.error(e)
        else:
            verified_number = user.get_verified_number()
            phone_number = verified_number.phone_number if verified_number else None
            if apply_updates(user, user_dict):
                if user_dict.get('phone_numbers'):
                    new_phone_number = user_dict['phone_numbers'][0]
                    if new_phone_number != phone_number:
                        if phone_number:
                            user.delete_verified_number(phone_number)
                        self._save_verified_number(user, new_phone_number)
                elif phone_number:
                    user.phone_numbers = []
                    user.delete_verified_number(phone_number)
                user.save()
        return user
Example #12
0
    def location_sync(self, ilsgateway_location):
        def get_or_create_msd_zone(region):
            msd_name = _get_msd_name(region.name)
            msd_code = MSDZONE_MAP[msd_name][0]
            try:
                sql_msd_loc = SQLLocation.objects.get(
                    domain=self.domain,
                    site_code=msd_code
                )
                msd_location = Loc.get(sql_msd_loc.location_id)
            except SQLLocation.DoesNotExist:
                msd_location = Loc(parent=loc_parent)

            msd_location.domain = self.domain
            msd_location.name = msd_name
            msd_location.location_type = 'MSDZONE'
            msd_location.site_code = MSDZONE_MAP[msd_name][0]
            msd_location.save()
            return msd_location

        try:
            sql_loc = SQLLocation.objects.get(
                domain=self.domain,
                external_id=int(ilsgateway_location.id)
            )
            location = Loc.get(sql_loc.location_id)
        except SQLLocation.DoesNotExist:
            location = None
        except SQLLocation.MultipleObjectsReturned:
            return

        if not location:
            if ilsgateway_location.parent_id:
                try:
                    sql_loc_parent = SQLLocation.objects.get(
                        domain=self.domain,
                        external_id=ilsgateway_location.parent_id
                    )
                    loc_parent = sql_loc_parent.couch_location
                except SQLLocation.DoesNotExist:
                    parent = self.endpoint.get_location(ilsgateway_location.parent_id)
                    loc_parent = self.location_sync(Location(parent))

                if ilsgateway_location.type == 'REGION':
                    location = Loc(parent=get_or_create_msd_zone(ilsgateway_location))
                else:
                    location = Loc(parent=loc_parent)
            else:
                location = Loc()
                location.lineage = []
            location.domain = self.domain
            location.name = ilsgateway_location.name
            if ilsgateway_location.groups:
                location.metadata = {'group': ilsgateway_location.groups[0]}
            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 = unicode(ilsgateway_location.id)
            location.save()

            if ilsgateway_location.type == 'FACILITY' and not SupplyPointCase.get_by_location(location):
                SupplyPointCase.create_from_location(self.domain, location)
                location.save()
        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']['group'] = ilsgateway_location.groups[0]
            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(self.domain, location)
        return location
Example #13
0
    def sms_user_sync(self,
                      ilsgateway_smsuser,
                      username_part=None,
                      password=None,
                      first_name='',
                      last_name=''):
        domain_part = "%s.commcarehq.org" % self.domain
        if not username_part:
            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)
        if not first_name:
            first_name = splitted_value[0][:30] if splitted_value else ''

        if not last_name:
            last_name = splitted_value[1][:30] if len(
                splitted_value) > 1 else ''

        language = ilsgateway_smsuser.language

        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:
            cleaned_number = apply_leniency(
                ilsgateway_smsuser.phone_numbers[0])
            if cleaned_number:
                user_dict['phone_numbers'] = [cleaned_number]
                user_dict['user_data']['backend'] = ilsgateway_smsuser.backend

        if user is None and username_part:
            try:
                user_password = password or User.objects.make_random_password()
                user = CommCareUser.create(domain=self.domain,
                                           username=username,
                                           password=user_password,
                                           email=ilsgateway_smsuser.email,
                                           commit=False,
                                           password_hashed=bool(password))
                user.first_name = first_name
                user.last_name = last_name
                user.language = language
                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(
                            self.domain, user_dict["phone_numbers"][0], True)
                    except PhoneNumberInUseException as e:
                        self._reassign_number(user,
                                              user_dict["phone_numbers"][0])
                    except InvalidFormatException:
                        pass
            except Exception as e:
                logging.error(e)
        else:
            verified_number = user.get_verified_number()
            phone_number = verified_number.phone_number if verified_number else None
            if apply_updates(user, user_dict):
                if user_dict.get('phone_numbers'):
                    new_phone_number = user_dict['phone_numbers'][0]
                    if new_phone_number != phone_number:
                        if phone_number:
                            user.delete_verified_number(phone_number)
                        self._save_verified_number(user, new_phone_number)
                elif phone_number:
                    user.phone_numbers = []
                    user.delete_verified_number(phone_number)
                user.save()
        return user
Example #14
0
    def location_sync(self, ilsgateway_location, fetch_groups=False):
        try:
            sql_loc = SQLLocation.objects.get(
                domain=self.domain,
                external_id=int(ilsgateway_location.id)
            )
            location = Loc.get(sql_loc.location_id)
        except SQLLocation.DoesNotExist:
            location = None
        except SQLLocation.MultipleObjectsReturned:
            return

        if not location:
            if ilsgateway_location.parent_id:
                try:
                    sql_loc_parent = SQLLocation.objects.get(
                        domain=self.domain,
                        external_id=ilsgateway_location.parent_id
                    )
                    loc_parent = sql_loc_parent.couch_location
                except SQLLocation.DoesNotExist:
                    parent = self.endpoint.get_location(ilsgateway_location.parent_id)
                    loc_parent = self.location_sync(Location(parent))
                location = Loc(parent=loc_parent)
            else:
                location = Loc()
                location.lineage = []
            location.domain = self.domain
            location.name = ilsgateway_location.name
            if ilsgateway_location.groups:
                location.metadata = {'group': ilsgateway_location.groups[0]}
            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 = unicode(ilsgateway_location.id)
            location.save()

            if ilsgateway_location.type == 'FACILITY' and not SupplyPointCase.get_by_location(location):
                SupplyPointCase.create_from_location(self.domain, location)
                location.save()
        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']['group'] = ilsgateway_location.groups[0]
            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(self.domain, location)

        if ilsgateway_location.historical_groups:
            historical_groups = ilsgateway_location.historical_groups
        else:
            counter = 0
            historical_groups = {}
            while counter != 5:
                try:
                    # todo: we may be able to avoid this call by passing the groups in as part of the original
                    # location dict, though that may introduce slowness/timeouts
                    location_object = self.endpoint.get_location(
                        ilsgateway_location.id,
                        params=dict(with_historical_groups=1)
                    )
                    historical_groups = Location(**location_object).historical_groups
                    break
                except ConnectionError as e:
                    logging.error(e)
                    counter += 1

        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
Example #15
0
    def location_sync(self, ilsgateway_location):
        def get_or_create_msd_zone(region):
            msd_name = _get_msd_name(region.name)
            msd_code = MSDZONE_MAP[msd_name][0]
            try:
                sql_msd_loc = SQLLocation.objects.get(domain=self.domain,
                                                      site_code=msd_code)
                msd_location = Loc.get(sql_msd_loc.location_id)
            except SQLLocation.DoesNotExist:
                msd_location = Loc(parent=loc_parent)

            msd_location.domain = self.domain
            msd_location.name = msd_name
            msd_location.location_type = 'MSDZONE'
            msd_location.site_code = MSDZONE_MAP[msd_name][0]
            msd_location.save()
            return msd_location

        try:
            sql_loc = SQLLocation.objects.get(domain=self.domain,
                                              external_id=int(
                                                  ilsgateway_location.id))
            location = Loc.get(sql_loc.location_id)
        except SQLLocation.DoesNotExist:
            location = None
        except SQLLocation.MultipleObjectsReturned:
            return

        if not location:
            if ilsgateway_location.id in EXCLUDED_REGIONS:
                return

            if ilsgateway_location.parent_id:
                try:
                    sql_loc_parent = SQLLocation.objects.get(
                        domain=self.domain,
                        external_id=ilsgateway_location.parent_id)
                    loc_parent = sql_loc_parent.couch_location
                except SQLLocation.DoesNotExist:
                    parent = self.endpoint.get_location(
                        ilsgateway_location.parent_id)
                    loc_parent = self.location_sync(Location(parent))
                    if not loc_parent:
                        return

                if ilsgateway_location.type == 'REGION':
                    location = Loc(
                        parent=get_or_create_msd_zone(ilsgateway_location))
                else:
                    location = Loc(parent=loc_parent)
            else:
                location = Loc()
                location.lineage = []
            location.domain = self.domain
            location.name = ilsgateway_location.name
            if ilsgateway_location.groups:
                location.metadata = {'group': ilsgateway_location.groups[0]}
            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 = unicode(ilsgateway_location.id)
            location.save()

            if ilsgateway_location.type == 'FACILITY' and not SupplyPointCase.get_by_location(
                    location):
                SupplyPointCase.create_from_location(self.domain, location)
                location.save()
        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'][
                    'group'] = ilsgateway_location.groups[0]
            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(self.domain, location)
Example #16
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:
                    loc_parent_id = self._sync_parent(ews_location.parent_id)

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

            self._set_location_properties(location, ews_location)
            self._set_up_supply_point(location, ews_location)
        else:
            location_dict = {}
            if location.sql_location.location_type.administrative:
                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,
                }
                try:
                    SQLLocation.objects.get(
                        domain=self.domain,
                        site_code=ews_location.code.lower())
                except SQLLocation.DoesNotExist:
                    location_dict['site_code'] = ews_location.code.lower()
            else:
                supply_point_with_stock_data = filter(
                    lambda x: x.last_reported and x.active,
                    ews_location.supply_points)
                supply_point = None
                if supply_point_with_stock_data:
                    supply_point = supply_point_with_stock_data[0]
                elif ews_location.supply_points:
                    supply_point = ews_location.supply_points[0]

                if supply_point:
                    location_dict = {
                        'name':
                        supply_point.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':
                        supply_point.code,
                    }

            if location_dict and apply_updates(location, location_dict):
                location.save()
        for supply_point in ews_location.supply_points:
            sp = get_supply_point_case_by_domain_external_id(
                self.domain, supply_point.id)
            if sp:
                sql_location = sp.sql_location
                if set(sql_location.products.values_list(
                        'code', flat=True)) != supply_point.products:
                    sql_location.products = SQLProduct.objects.filter(
                        domain=self.domain, code__in=supply_point.products)
                    sql_location.save()

                for in_charge in supply_point.incharges:
                    self._set_in_charges(in_charge, sql_location)
        return location
Example #17
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
Example #18
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
Example #19
0
    def location_sync(self, ilsgateway_location):
        def get_or_create_msd_zone(region):
            msd_name = _get_msd_name(region.name)
            msd_code = MSDZONE_MAP[msd_name][0]
            try:
                sql_msd_loc = SQLLocation.objects.get(domain=self.domain, site_code=msd_code)
                msd_location = Loc.get(sql_msd_loc.location_id)
            except SQLLocation.DoesNotExist:
                msd_location = Loc(parent=loc_parent)

            msd_location.domain = self.domain
            msd_location.name = msd_name
            msd_location.location_type = "MSDZONE"
            msd_location.site_code = MSDZONE_MAP[msd_name][0]
            msd_location.save()
            return msd_location

        try:
            sql_loc = SQLLocation.objects.get(domain=self.domain, external_id=int(ilsgateway_location.id))
            location = Loc.get(sql_loc.location_id)
        except SQLLocation.DoesNotExist:
            location = None
        except SQLLocation.MultipleObjectsReturned:
            return

        if not location:
            if ilsgateway_location.id in EXCLUDED_REGIONS:
                return

            if ilsgateway_location.parent_id:
                try:
                    sql_loc_parent = SQLLocation.objects.get(
                        domain=self.domain, external_id=ilsgateway_location.parent_id
                    )
                    loc_parent = sql_loc_parent.couch_location
                except SQLLocation.DoesNotExist:
                    parent = self.endpoint.get_location(ilsgateway_location.parent_id)
                    loc_parent = self.location_sync(Location(parent))
                    if not loc_parent:
                        return

                if ilsgateway_location.type == "REGION":
                    location = Loc(parent=get_or_create_msd_zone(ilsgateway_location))
                else:
                    location = Loc(parent=loc_parent)
            else:
                location = Loc()
                location.lineage = []
            location.domain = self.domain
            location.name = ilsgateway_location.name
            if ilsgateway_location.groups:
                location.metadata = {"group": ilsgateway_location.groups[0]}
            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 = unicode(ilsgateway_location.id)
            location.save()

            if ilsgateway_location.type == "FACILITY" and not SupplyPointCase.get_by_location(location):
                SupplyPointCase.create_from_location(self.domain, location)
                location.save()
        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"]["group"] = ilsgateway_location.groups[0]
            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(self.domain, location)
        return location
Example #20
0
    def location_sync(self, ilsgateway_location):
        def get_or_create_msd_zone(region):
            msd_name = _get_msd_name(region.name)
            msd_code = MSDZONE_MAP[msd_name][0]
            try:
                sql_msd_loc = SQLLocation.objects.get(
                    domain=self.domain,
                    site_code=msd_code
                )
                msd_location = Loc.get(sql_msd_loc.location_id)
            except SQLLocation.DoesNotExist:
                msd_location = Loc(parent=loc_parent)

            msd_location.domain = self.domain
            msd_location.name = msd_name
            msd_location.location_type = 'MSDZONE'
            msd_location.site_code = MSDZONE_MAP[msd_name][0]
            msd_location.save()
            return msd_location

        try:
            sql_loc = SQLLocation.objects.get(
                domain=self.domain,
                external_id=int(ilsgateway_location.id)
            )
            location = Loc.get(sql_loc.location_id)
        except SQLLocation.DoesNotExist:
            location = None
        except SQLLocation.MultipleObjectsReturned:
            return

        if not location:
            if ilsgateway_location.id in EXCLUDED_REGIONS:
                return

            if ilsgateway_location.parent_id:
                try:
                    sql_loc_parent = SQLLocation.objects.get(
                        domain=self.domain,
                        external_id=ilsgateway_location.parent_id
                    )
                    loc_parent = sql_loc_parent.couch_location
                except SQLLocation.DoesNotExist:
                    new_parent = self.endpoint.get_location(ilsgateway_location.parent_id)
                    loc_parent = self.location_sync(Location(new_parent))
                    if not loc_parent:
                        return

                if ilsgateway_location.type == 'REGION':
                    location = Loc(parent=get_or_create_msd_zone(ilsgateway_location))
                else:
                    location = Loc(parent=loc_parent)
            else:
                location = Loc()
                location.lineage = []
            location.domain = self.domain
            location.name = ilsgateway_location.name
            if ilsgateway_location.groups:
                location.metadata = {'group': ilsgateway_location.groups[0]}
            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 = unicode(ilsgateway_location.id)
            location.save()

            interface = SupplyInterface(self.domain)
            if ilsgateway_location.type == 'FACILITY':
                if not interface.get_by_location(location):
                    interface.create_from_location(self.domain, location)
                    location.save()
                else:
                    sql_location = location.sql_location
                    if not sql_location.supply_point_id:
                        location.save()
        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']['group'] = ilsgateway_location.groups[0]
            case = SupplyInterface(self.domain).get_by_location(location)
            if apply_updates(location, location_dict):
                location.save()
                if case:
                    update_supply_point_from_location(case, location)
                else:
                    SupplyInterface.create_from_location(self.domain, location)
            location_parent = location.parent
            if ilsgateway_location.type == 'FACILITY' and ilsgateway_location.parent_id and location_parent \
                    and location_parent.external_id != str(ilsgateway_location.parent_id):
                new_parent = self.endpoint.get_location(ilsgateway_location.parent_id)
                new_parent = self.location_sync(Location(new_parent))
                location.lineage = get_lineage_from_location_id(new_parent.get_id)
                location.save()
                location.previous_parents = [location_parent.get_id]
                location_edited_receiver(None, location, moved=True)
        return location
Example #21
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:
                    loc_parent_id = self._sync_parent(ews_location.parent_id)

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

            self._set_location_properties(location, ews_location)
            self._set_up_supply_point(location, ews_location)
        else:
            location_dict = {}
            if location.sql_location.location_type.administrative:
                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,
                }
                try:
                    SQLLocation.objects.get(domain=self.domain, site_code=ews_location.code.lower())
                except SQLLocation.DoesNotExist:
                    location_dict['site_code'] = ews_location.code.lower()
            else:
                supply_point_with_stock_data = filter(
                    lambda x: x.last_reported and x.active, ews_location.supply_points
                )
                supply_point = None
                if supply_point_with_stock_data:
                    supply_point = supply_point_with_stock_data[0]
                elif ews_location.supply_points:
                    supply_point = ews_location.supply_points[0]

                if supply_point:
                    location_dict = {
                        'name': supply_point.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': supply_point.code,
                    }

            if location_dict and apply_updates(location, location_dict):
                location.save()
        for supply_point in ews_location.supply_points:
            sp = get_supply_point_case_by_domain_external_id(self.domain, supply_point.id)
            if sp:
                sql_location = sp.sql_location
                if set(sql_location.products.values_list('code', flat=True)) != supply_point.products:
                    sql_location.products = SQLProduct.objects.filter(
                        domain=self.domain,
                        code__in=supply_point.products
                    )
                    sql_location.save()

                for in_charge in supply_point.incharges:
                    self._set_in_charges(in_charge, sql_location)
        return location
Example #22
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