예제 #1
0
 def handle(self, *args, **options):
     save = options['save']
     # since we need locations removed, filter for update logs
     records = UserHistory.objects.filter(
         Q(changes__has_key='location_id')
         | Q(changes__has_key='assigned_location_ids'),
         user_type='CommCareUser',
         action=UserHistory.UPDATE,
     )
     with open("add_location_change_message.csv", "w") as _file:
         for record in records.order_by('pk').iterator():
             updated = False
             if 'location_id' in record.changes and record.changes[
                     'location_id'] is None:
                 if 'location' not in record.change_messages:
                     record.change_messages.update(
                         UserChangeMessage.primary_location_removed())
                     updated = True
             if record.changes.get('assigned_location_ids') == []:
                 if 'assigned_locations' not in record.change_messages:
                     record.change_messages.update(
                         UserChangeMessage.assigned_locations_info([]))
                     updated = True
             if updated:
                 _file.write(
                     f"{record.pk},{record.user_id},{record.changes},{record.change_messages}\n"
                 )
                 if save:
                     record.save()
예제 #2
0
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)
예제 #3
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())
예제 #4
0
    def update_locations(self, location_codes, membership, 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 = membership.location_id
        locations_updated, primary_loc_removed = check_modified_user_loc(
            location_ids, membership.location_id,
            membership.assigned_location_ids)
        if primary_loc_removed:
            self.user.unset_location(self.user_domain, commit=False)
        if locations_updated:
            self.user.reset_locations(self.user_domain,
                                      location_ids,
                                      commit=False)
            if location_ids:
                locations = [
                    get_location_from_site_code(code,
                                                domain_info.location_cache)
                    for code in location_codes
                ]
            else:
                locations = []
            self.logger.add_info(
                UserChangeMessage.assigned_locations_info(locations))

        # log this after assigned locations are updated, which can re-set primary location
        user_updated_primary_location_id = get_user_primary_location_id(
            self.user, self.user_domain)
        if user_updated_primary_location_id != user_current_primary_location_id:
            if user_updated_primary_location_id:
                self._log_primary_location_info()
            else:
                self.logger.add_info(
                    UserChangeMessage.primary_location_removed())