Exemple #1
0
 def update_primary_location(self, location_id):
     current_primary_location_id = get_user_primary_location_id(
         self.user, self.user_domain)
     if location_id:
         self.user.set_location(self.user_domain, location_id)
         if current_primary_location_id != location_id:
             self._log_primary_location_info()
     else:
         self.user.unset_location(self.user_domain)
         # if there was a location before, log that it was cleared
         if current_primary_location_id:
             self.logger.add_info(
                 UserChangeMessage.primary_location_removed())
Exemple #2
0
    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())