def save(self): """ Calculate which locations need added or removed, then submit one caseblock to handle this """ user = CommTrackUser.get_by_username(self.username) if not user: raise UserUploadError( _('no username with {} found!'.format(self.username))) # have to rewrap since we need to force it to a commtrack user user = CommTrackUser.wrap(user.to_json()) current_locations = user.locations current_location_codes = [loc.site_code for loc in current_locations] commit_list = {} messages = [] def _add_loc(loc, clear=False): sp = self.get_supply_point_from_location(loc) if sp is None: messages.append( _("No supply point found for location '{}'. " "Make sure the location type is not set to administrative only " "and that the location has a valid sms code.").format( loc or '')) else: commit_list.update(user.supply_point_index_mapping(sp, clear)) for loc in self.to_add: if loc not in current_location_codes: _add_loc(loc) for loc in self.to_remove: if loc in current_location_codes: _add_loc(loc, clear=True) if commit_list: submit_mapping_case_block(user, commit_list) return messages
def save(self): """ Calculate which locations need added or removed, then submit one caseblock to handle this """ user = CommTrackUser.wrap(CommTrackUser.get_by_username(self.username).to_json()) current_locations = user.locations current_location_codes = [loc.site_code for loc in current_locations] commit_list = {} for loc in self.to_add: if loc not in current_location_codes: sp = self.get_supply_point_from_location(loc) commit_list.update(user.supply_point_index_mapping(sp)) for loc in self.to_remove: if loc in current_location_codes: sp = self.get_supply_point_from_location(loc) commit_list.update(user.supply_point_index_mapping(sp, True)) if commit_list: submit_mapping_case_block(user, commit_list)
def save(self): """ Calculate which locations need added or removed, then submit one caseblock to handle this """ user = CommTrackUser.get_by_username(self.username) if not user: raise UserUploadError(_('no username with {} found!'.format(self.username))) # have to rewrap since we need to force it to a commtrack user user = CommTrackUser.wrap(user.to_json()) current_locations = user.locations current_location_codes = [loc.site_code for loc in current_locations] commit_list = {} messages = [] def _add_loc(loc, clear=False): sp = self.get_supply_point_from_location(loc) if sp is None: messages.append(_("No supply point found for location '{}'. " "Make sure the location type is not set to administrative only " "and that the location has a valid sms code." ).format(loc or '')) else: commit_list.update(user.supply_point_index_mapping(sp, clear)) for loc in self.to_add: if loc not in current_location_codes: _add_loc(loc) for loc in self.to_remove: if loc in current_location_codes: _add_loc(loc, clear=True) if commit_list: submit_mapping_case_block(user, commit_list) return messages