def setUp(self, mock_pk): mock_pk.__get__ = Mock(return_value=7) super(TestCreateCasesByBeneficiary, self).setUp() self.pcp.site_code = str(self.agency.agencyId) self.pcp.save() self.virtual_location_user = make_location_user(self.pcp) set_issuer_id(self.domain, self.virtual_location_user) self.virtual_location_user.save() self.pcp.user_id = self.virtual_location_user._id self.pcp.save() self.default_location = SQLLocation.objects.create( domain=self.domain, location_type=self.pcp.location_type, site_code='default', ) self.default_location_user = make_location_user(self.default_location) set_issuer_id(self.domain, self.default_location_user) self.default_location_user.save() self.default_location.user_id = self.default_location_user._id self.default_location.save()
def _sync_location_user(self): if not self.location.location_id: return if self.location.location_type.has_user and not self.location.user_id: # make sure there's a location user res = list(UserES().domain(self.domain).show_inactive().term( 'user_location_id', self.location.location_id).values_list('_id', flat=True)) user_id = res[0] if res else None if user_id: user = CommCareUser.get(user_id) else: user = make_location_user(self.location) user.is_active = True user.user_location_id = self.location.location_id user.set_location(self.location, commit=False) user.save() self.location.user_id = user._id self.location.save() elif self.location.user_id and not self.location.location_type.has_user: # archive the location user user = CommCareUser.get_by_user_id(self.location.user_id, self.domain) if user: user.is_active = False user.save() self.location.user_id = '' self.location.save()
def _sync_location_user(self): if not self.location.location_id: return if self.location.location_type.has_user and not self.location.user_id: # make sure there's a location user res = list(UserES() .domain(self.domain) .show_inactive() .term('user_location_id', self.location.location_id) .values_list('_id', flat=True)) user_id = res[0] if res else None if user_id: user = CommCareUser.get(user_id) else: user = make_location_user(self.location) user.is_active = True user.user_location_id = self.location.location_id user.set_location(self.location, commit=False) user.save() self.location.user_id = user._id self.location.save() elif self.location.user_id and not self.location.location_type.has_user: # archive the location user user = CommCareUser.get_by_user_id(self.location.user_id, self.domain) if user: user.is_active = False user.save() self.location.user_id = '' self.location.save()
def create_user(self, agency, agency_loc, user_level): assert agency_loc.location_type.has_user agency_loc_id = agency_loc.location_id domain = agency_loc.domain user = CommCareUser.get_by_username( '%s@%s.commcarehq.org' % (agency_loc.site_code, agency_loc.domain)) if user is None: user = make_location_user(agency_loc) user.user_location_id = agency_loc_id user.set_location(agency_loc, commit=False) user.user_data['agency_id_legacy'] = agency_loc.metadata[ 'private_sector_agency_id'] user.set_role( domain, UserRole.by_domain_and_name( domain, 'Default Mobile Worker')[0].get_qualified_id()) user.user_data['user_level'] = user_level user.user_data['usertype'] = agency.usertype user.save() agency_loc.user_id = user._id agency_loc.save()