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.role: sms_user.user_data['role'] = [ews_smsuser.role] sms_user.save() if ews_smsuser.supply_point: if ews_smsuser.supply_point.id: sp = get_supply_point_case_by_domain_external_id( self.domain, ews_smsuser.supply_point.id) 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 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.role: sms_user.user_data['role'] = [ews_smsuser.role] sms_user.save() if ews_smsuser.supply_point: if ews_smsuser.supply_point.id: sp = get_supply_point_case_by_domain_external_id(self.domain, ews_smsuser.supply_point.id) 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 web_user_sync(self, ews_webuser): username = ews_webuser.email.lower() if not username: try: validate_email(ews_webuser.username) username = ews_webuser.username except ValidationError: return None user = WebUser.get_by_username(username) user_dict = { 'first_name': ews_webuser.first_name, 'last_name': ews_webuser.last_name, 'is_active': ews_webuser.is_active, 'last_login': force_to_datetime(ews_webuser.last_login), 'date_joined': force_to_datetime(ews_webuser.date_joined), 'password_hashed': True, } location_id = None if ews_webuser.location: try: location = SQLLocation.objects.get(domain=self.domain, external_id=ews_webuser.location) location_id = location.location_id except SQLLocation.DoesNotExist: pass if user is None: try: user = WebUser.create(domain=None, username=username, password=ews_webuser.password, email=ews_webuser.email, **user_dict) user.add_domain_membership(self.domain, location_id=location_id) except Exception as e: logging.error(e) else: if self.domain not in user.get_domains(): user.add_domain_membership(self.domain, location_id=location_id) ews_webuser_extension(user, ews_webuser) dm = user.get_domain_membership(self.domain) if dm.location_id != location_id: dm.location_id = location_id if ews_webuser.is_superuser: dm.role_id = UserRole.by_domain_and_name(self.domain, 'Administrator')[0].get_id elif ews_webuser.groups and ews_webuser.groups[0].name == 'facility_manager': dm.role_id = UserRole.by_domain_and_name(self.domain, 'Facility manager')[0].get_id else: if ews_webuser.supply_point: supply_point = get_supply_point_case_by_domain_external_id(self.domain, ews_webuser.supply_point) if supply_point: dm.location_id = supply_point.location_id dm.role_id = UserRole.by_domain_and_name(self.domain, 'Web Reporter')[0].get_id else: dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id else: dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id user.save() return user
def sms_user_sync(self, ews_smsuser, **kwargs): sms_user = super(EWSApi, self).sms_user_sync(ews_smsuser, **kwargs) if not sms_user: return changed = False saved = False if ews_smsuser.role and sms_user.user_data.get('role') != [ews_smsuser.role]: sms_user.user_data['role'] = [ews_smsuser.role] changed = True if ews_smsuser.phone_numbers: connections = [] for connection in ews_smsuser.phone_numbers: connections.append({ 'phone_number': connection.phone_number, 'backend': connection.backend, 'default': connection.default }) if connections != sms_user.user_data.get('connections'): sms_user.user_data['connections'] = connections changed = True if ews_smsuser.supply_point: if ews_smsuser.supply_point.id: sp = get_supply_point_case_by_domain_external_id(self.domain, ews_smsuser.supply_point.id) 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 and couch_location.get_id != sms_user.location_id: if not sms_user.get_id: sms_user.save() sms_user.set_location(couch_location) saved = True if not sms_user.get_id or (changed and not saved): sms_user.save() return sms_user
def _set_extension(self, web_user, supply_point_id, sms_notifications): extension, _ = EWSExtension.objects.get_or_create( domain=self.domain, user_id=web_user.get_id, ) supply_point = None location_id = None if supply_point_id: supply_point = get_supply_point_case_by_domain_external_id(self.domain, supply_point_id) if supply_point: location_id = supply_point.location_id if location_id != extension.location_id or sms_notifications != extension.sms_notifications: extension.location_id = location_id extension.sms_notifications = sms_notifications extension.save()
def validate_supply_points(self, date): for location in iterate_over_api_objects( self.endpoint.get_locations, filters={'is_active': True, 'date_updated__gte': date} ): for supply_point in location.supply_points: sp = get_supply_point_case_by_domain_external_id(self.domain, supply_point.id) if sp: EWSMigrationProblem.objects.filter( domain=self.domain, external_id=supply_point.id, object_type='supply_point' ).delete() sql_location = sp.sql_location ids = sql_location.facilityincharge_set.all().values_list('user_id', flat=True) usernames = [user['username'].split('@')[0] for user in iter_docs(CouchUser.get_db(), ids)] if not all([self._check_username(usernames, incharge) for incharge in supply_point.incharges]): migration_problem, _ = EWSMigrationProblem.objects.get_or_create( domain=self.domain, object_id=sql_location.location_id, object_type='location' ) migration_problem.object_type = 'location' migration_problem.external_id = sql_location.external_id migration_problem.description = 'Invalid in charges' migration_problem.save() else: EWSMigrationProblem.objects.filter( domain=self.domain, external_id=sql_location.external_id, object_type='location' ).delete() elif supply_point.active and supply_point.last_reported: migration_problem, _ = EWSMigrationProblem.objects.get_or_create( domain=self.domain, external_id=supply_point.id, ) migration_problem.object_type = 'supply_point' migration_problem.external_id = supply_point.id migration_problem.description = 'Not exists' migration_problem.save()
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 if ews_smsuser.role: sms_user.user_data['role'] = [ews_smsuser.role] if ews_smsuser.phone_numbers: sms_user.user_data['connections'] = [] for connection in ews_smsuser.phone_numbers: sms_user.user_data['connections'].append({ 'phone_number': connection.phone_number, 'backend': connection.backend, 'default': connection.default }) sms_user.save() if ews_smsuser.supply_point: if ews_smsuser.supply_point.id: sp = get_supply_point_case_by_domain_external_id(self.domain, ews_smsuser.supply_point.id) 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 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
def web_user_sync(self, ews_webuser): username = ews_webuser.email.lower() if not username: try: validate_email(ews_webuser.username) username = ews_webuser.username except ValidationError: return None user = WebUser.get_by_username(username) user_dict = { 'first_name': ews_webuser.first_name, 'last_name': ews_webuser.last_name, 'is_active': ews_webuser.is_active, 'last_login': force_to_datetime(ews_webuser.last_login), 'date_joined': force_to_datetime(ews_webuser.date_joined), 'password_hashed': True, } location_id = None if ews_webuser.location: try: location = SQLLocation.objects.get( domain=self.domain, external_id=ews_webuser.location) location_id = location.location_id except SQLLocation.DoesNotExist: pass if user is None: try: user = WebUser.create(domain=None, username=username, password=ews_webuser.password, email=ews_webuser.email, **user_dict) user.add_domain_membership(self.domain, location_id=location_id) except Exception as e: logging.error(e) else: if self.domain not in user.get_domains(): user.add_domain_membership(self.domain, location_id=location_id) ews_webuser_extension(user, ews_webuser) dm = user.get_domain_membership(self.domain) if dm.location_id != location_id: dm.location_id = location_id if ews_webuser.is_superuser: dm.role_id = UserRole.by_domain_and_name(self.domain, 'Administrator')[0].get_id elif ews_webuser.groups and ews_webuser.groups[ 0].name == 'facility_manager': dm.role_id = UserRole.by_domain_and_name( self.domain, 'Facility manager')[0].get_id else: if ews_webuser.supply_point: supply_point = get_supply_point_case_by_domain_external_id( self.domain, ews_webuser.supply_point) if supply_point: dm.location_id = supply_point.location_id dm.role_id = UserRole.by_domain_and_name( self.domain, 'Web Reporter')[0].get_id else: dm.role_id = UserRole.get_read_only_role_by_domain( self.domain).get_id else: dm.role_id = UserRole.get_read_only_role_by_domain( self.domain).get_id user.save() return user
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
def web_user_sync(self, ews_webuser): username = ews_webuser.email.lower() if not username: try: validate_email(ews_webuser.username) username = ews_webuser.username except ValidationError: return None user = WebUser.get_by_username(username) user_dict = { 'first_name': ews_webuser.first_name, 'last_name': ews_webuser.last_name, 'is_active': ews_webuser.is_active, 'last_login': force_to_datetime(ews_webuser.last_login), 'date_joined': force_to_datetime(ews_webuser.date_joined), 'password_hashed': True, } location_id = None if ews_webuser.location: try: sql_location = SQLLocation.objects.get(domain=self.domain, external_id=ews_webuser.location) location_id = sql_location.location_id except SQLLocation.DoesNotExist: pass if user is None: try: user = WebUser.create(domain=None, username=username, password=ews_webuser.password, email=ews_webuser.email.lower(), **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) # We are migrating only active users user.is_active = True dm = user.get_domain_membership(self.domain) if dm.location_id != location_id: dm.location_id = location_id if ews_webuser.program: self._set_program(user, ews_webuser.program) self._set_extension(user, ews_webuser.supply_point, ews_webuser.sms_notifications) if ews_webuser.is_superuser: dm.role_id = UserRole.by_domain_and_name(self.domain, 'Administrator')[0].get_id elif ews_webuser.groups and ews_webuser.groups[0].name == 'facility_manager': dm.role_id = UserRole.by_domain_and_name(self.domain, 'Facility manager')[0].get_id else: if ews_webuser.supply_point: supply_point = get_supply_point_case_by_domain_external_id(self.domain, ews_webuser.supply_point) if supply_point: dm.role_id = UserRole.by_domain_and_name(self.domain, 'Web Reporter')[0].get_id else: dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id else: dm.role_id = UserRole.get_read_only_role_by_domain(self.domain).get_id if ews_webuser.contact: user.phone_numbers = [] connections = [] for connection in ews_webuser.contact.phone_numbers: phone_number = apply_leniency(connection.phone_number) if connection.default: user.phone_numbers = [phone_number] + user.phone_numbers else: user.phone_numbers.append(phone_number) connections.append({ 'phone_number': connection.phone_number, 'backend': connection.backend, 'default': connection.default }) if connections != user.user_data.get('connections'): user.user_data['connections'] = connections user.save() return user