def log_commcare_user_locations_changes(request, user, old_location_id, old_assigned_location_ids): change_messages = {} fields_changed = {} if old_location_id != user.location_id: location = None fields_changed['location_id'] = user.location_id if user.location_id: location = SQLLocation.objects.get(location_id=user.location_id) change_messages.update( UserChangeMessage.primary_location_info(location)) if old_assigned_location_ids != user.assigned_location_ids: locations = [] fields_changed['assigned_location_ids'] = user.assigned_location_ids if user.assigned_location_ids: locations = SQLLocation.objects.filter( location_id__in=user.assigned_location_ids) change_messages.update( UserChangeMessage.assigned_locations_info(locations)) if change_messages: log_user_change(by_domain=request.domain, for_domain=user.domain, couch_user=user, changed_by_user=request.couch_user, changed_via=USER_CHANGE_VIA_WEB, fields_changed=fields_changed, change_messages=change_messages)
def update_locations(self, location_codes, domain_info): from corehq.apps.user_importer.importer import ( check_modified_user_loc, find_location_id, get_location_from_site_code) location_ids = find_location_id(location_codes, domain_info.location_cache) user_current_primary_location_id = self.user.location_id locations_updated, primary_loc_removed = check_modified_user_loc( location_ids, self.user.location_id, self.user.assigned_location_ids) if primary_loc_removed: self.user.unset_location(commit=False) if locations_updated: self.user.reset_locations(location_ids, commit=False) self.logger.add_changes({'assigned_location_ids': location_ids}) if location_ids: locations = [ get_location_from_site_code(code, domain_info.location_cache) for code in location_codes ] self.logger.add_info( UserChangeMessage.assigned_locations_info(locations)) else: self.logger.add_info( UserChangeMessage.assigned_locations_info([])) # log this after assigned locations are updated, which can re-set primary location if self.user.location_id != user_current_primary_location_id: self.logger.add_changes({'location_id': self.user.location_id}) if self.user.location_id: self.logger.add_info( UserChangeMessage.primary_location_info( self.user.get_sql_location(self.user_domain))) else: self.logger.add_info( UserChangeMessage.primary_location_removed())
def _log_primary_location_info(self): primary_location = self.user.get_sql_location(self.user_domain) self.logger.add_info( UserChangeMessage.primary_location_info(primary_location))