def test_should_sync_locations_change_location_type(self):
        """
        When location_type gets changed, we should resync locations
        """
        yesterday = datetime.today() - timedelta(1)
        day_before_yesterday = yesterday - timedelta(1)
        LocationType.objects.all().update(last_modified=day_before_yesterday)  # Force update because of auto_now
        self.location_type = LocationType.objects.last()

        location = SQLLocation(
            location_id="unique-id",
            domain="test-domain",
            name="Meereen",
            location_type=self.location_type,
            metadata={'queen': "Daenerys Targaryen",
                      'rebels': "Sons of the Harpy"},
        )
        location.save()

        SQLLocation.objects.filter(pk=1).update(last_modified=day_before_yesterday)
        location = SQLLocation.objects.last()
        location_db = _location_footprint([location])

        self.assertFalse(should_sync_locations(SyncLog(date=yesterday), location_db, self.user))

        self.location_type.shares_cases = True
        self.location_type.save()

        location = SQLLocation.objects.last()
        location_db = _location_footprint([location])

        self.assertTrue(should_sync_locations(SyncLog(date=yesterday), location_db, self.user))
Beispiel #2
0
    def copy_locations(self, types_only=False):
        from corehq.apps.locations.models import LocationType, SQLLocation
        from corehq.apps.locations.views import LocationFieldsView

        self._copy_custom_data(LocationFieldsView.field_type)

        location_types = LocationType.objects.by_domain(self.existing_domain)
        location_types_map = {}
        for location_type in location_types:
            if location_type.parent_type_id:
                location_type.parent_type_id = location_types_map[location_type.parent_type_id]
            old_id, new_id = self.save_sql_copy(location_type, self.new_domain)
            location_types_map[old_id] = new_id

        if not types_only:
            # use get_descendants, which sorts locations hierarchically,
            # so we can save in the same order
            locs = SQLLocation.objects.get_queryset_descendants(
                Q(domain=self.existing_domain, parent_id__isnull=True)
            ).filter(is_archived=False)
            new_loc_pks_by_code = {}
            for loc in locs:
                # start with a new location so we don't inadvertently copy over a bunch of foreign keys
                new_loc = SQLLocation()
                for field in ["name", "site_code", "external_id", "metadata",
                              "is_archived", "latitude", "longitude"]:
                    setattr(new_loc, field, getattr(loc, field, None))
                new_loc.domain = self.new_domain
                new_loc.parent_id = new_loc_pks_by_code[loc.parent.site_code] if loc.parent_id else None
                new_loc.location_type_id = location_types_map[loc.location_type_id]
                _, new_pk = self.save_sql_copy(new_loc, self.new_domain)
                new_loc_pks_by_code[new_loc.site_code] = new_pk

            existing_fixture_config = LocationFixtureConfiguration.for_domain(self.existing_domain)
            self.save_sql_copy(existing_fixture_config, self.new_domain)
Beispiel #3
0
 def _make_loc(name, site_code, location_type, parent_code, location_id,
               do_delete, external_id, latitude, longitude, custom_data, uncategorized_data,
               index, parent=None):
     _type = lt_by_code.get(location_type)
     loc = SQLLocation(
         site_code=site_code, name=name, domain=self.domain.name, location_type=_type,
         parent=parent,
     )
     loc.save()
     return loc
Beispiel #4
0
def load_locs_json(domain, selected_loc_id=None, include_archived=False,
        user=None, only_administrative=False):
    """initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.

    what is initialized:
    * all top level locs
    * if a 'selected' loc is provided, that loc and its complete
      ancestry

    only_administrative - if False get all locations
                          if True get only administrative locations
    """
    def loc_to_json(loc):
        return {
            'name': loc.name,
            'location_type': loc.location_type.name,  # todo: remove when types aren't optional
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
            'can_edit': True
        }

    locations = SQLLocation.root_locations(
        domain, include_archive_ancestors=include_archived
    )

    if only_administrative:
        locations = locations.filter(location_type__administrative=True)

    loc_json = [loc_to_json(loc) for loc in locations]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        selected = SQLLocation.objects.get(
            domain=domain,
            location_id=selected_loc_id
        )

        lineage = selected.get_ancestors()

        parent = {'children': loc_json}
        for loc in lineage:
            children = loc.child_locations(include_archive_ancestors=include_archived)
            if only_administrative:
                children = children.filter(location_type__administrative=True)

            # find existing entry in the json tree that corresponds to this loc
            try:
                this_loc = [k for k in parent['children'] if k['uuid'] == loc.location_id][0]
            except IndexError:
                # if we couldn't find this location the view just break out of the loop.
                # there are some instances in viewing archived locations where we don't actually
                # support drilling all the way down.
                break
            this_loc['children'] = [loc_to_json(loc) for loc in children]
            parent = this_loc

    return loc_json
    def get_location(self, location_id):
        location = SQLLocation.by_location_id(location_id)

        if not location:
            raise CommandError("Could not find location %s" % location_id)

        return location
Beispiel #6
0
    def recipient(self):
        if self.recipient_type == self.RECIPIENT_TYPE_CASE:
            try:
                case = CaseAccessors(self.domain).get_case(self.recipient_id)
            except CaseNotFound:
                return None

            if case.domain != self.domain:
                return None

            return case
        elif self.recipient_type == self.RECIPIENT_TYPE_MOBILE_WORKER:
            user = CouchUser.get_by_user_id(self.recipient_id, domain=self.domain)
            if not isinstance(user, CommCareUser):
                return None

            return user
        elif self.recipient_type == self.RECIPIENT_TYPE_WEB_USER:
            user = CouchUser.get_by_user_id(self.recipient_id, domain=self.domain)
            if not isinstance(user, WebUser):
                return None

            return user
        elif self.recipient_type == self.RECIPIENT_TYPE_CASE_GROUP:
            try:
                group = CommCareCaseGroup.get(self.recipient_id)
            except ResourceNotFound:
                return None

            if group.domain != self.domain:
                return None

            return group
        elif self.recipient_type == self.RECIPIENT_TYPE_USER_GROUP:
            try:
                group = Group.get(self.recipient_id)
            except ResourceNotFound:
                return None

            if group.domain != self.domain:
                return None

            return group
        elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION:
            location = SQLLocation.by_location_id(self.recipient_id)

            if location is None:
                return None

            if location.domain != self.domain:
                return None

            return location
        else:
            raise UnknownRecipientType(self.recipient_type)
Beispiel #7
0
    def get_location(self, sms):
        location_id = self.get_location_id(sms)
        if not location_id:
            return None

        if location_id in self.location_id_to_location:
            return self.location_id_to_location[location_id]

        location = SQLLocation.by_location_id(location_id)
        self.location_id_to_location[location_id] = location
        return location
Beispiel #8
0
def _get_expand_from_level(domain, user_location, expand_from):
    """From the users current location, returns the highest location they want to start expanding from
    """
    if user_location.location_type.expand_from_root:
        return SQLLocation.root_locations(domain=domain)
    else:
        ancestors = (
            user_location
            .get_ancestors(include_self=True)
            .filter(location_type=expand_from, is_archived=False)
        )
        return ancestors
Beispiel #9
0
def _get_user_permissions(user, domain):
    '''
    Takes a couch_user and returns that users level in the heirarchy and the location name
    '''
    # TODO: This needs to handle the top level where someone can see everything
    try:
        user_location_id = user.get_domain_membership(domain).location_id
        loc = SQLLocation.by_location_id(user_location_id)
        location_type_name = loc.location_type.name
        location_name = loc.name
    except Exception as e:
        location_type_name = 'state'
        location_name = ''
    return location_type_name, location_name
Beispiel #10
0
    def obj_get_list(self, bundle, **kwargs):
        domain = kwargs['domain']
        project = bundle.request.project
        parent_id = bundle.request.GET.get('parent_id', None)
        include_inactive = json.loads(bundle.request.GET.get('include_inactive', 'false'))
        show_administrative = bundle.request.GET.get('show_administrative', False)
        viewable = _user_locations_ids(project, show_administrative)

        if not parent_id:
            locs = SQLLocation.root_locations(domain, include_inactive)
        else:
            parent = get_object_or_not_exist(Location, parent_id, domain)
            locs = parent.sql_location.child_locations(include_inactive)

        return [child for child in locs if child.location_id in viewable]
Beispiel #11
0
    def parse_group_location_params(self, param_ids):
        locationids_param = []
        groupids_param = []

        if param_ids:
            for id in param_ids:
                if id.startswith("g__"):
                    groupids_param.append(id[3:])
                elif id.startswith("l__"):
                    loc = SQLLocation.by_location_id(id[3:])
                    if loc.get_descendants():
                        locationids_param.extend(loc.get_descendants().location_ids())
                    locationids_param.append(id[3:])

        return locationids_param, groupids_param
Beispiel #12
0
def load_locs_json(domain, selected_loc_id=None, user=None, show_test=False):

    def loc_to_json(loc, project):
        return {
            'name': loc.name,
            'location_type': loc.location_type.name,  # todo: remove when types aren't optional
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
            'can_edit': True
        }

    project = Domain.get_by_name(domain)

    locations = SQLLocation.root_locations(domain)
    if not show_test:
        locations = [
            loc for loc in locations if loc.metadata.get('is_test_location', 'real') != 'test'
        ]

    loc_json = [loc_to_json(loc, project) for loc in locations]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        selected = SQLLocation.objects.get(
            domain=domain,
            location_id=selected_loc_id
        )

        lineage = selected.get_ancestors()

        parent = {'children': loc_json}
        for loc in lineage:
            children = loc.child_locations()
            # find existing entry in the json tree that corresponds to this loc
            try:
                this_loc = [k for k in parent['children'] if k['uuid'] == loc.location_id][0]
            except IndexError:
                # if we couldn't find this location the view just break out of the loop.
                # there are some instances in viewing archived locations where we don't actually
                # support drilling all the way down.
                break
            this_loc['children'] = [loc_to_json(loc, project) for loc in children]
            parent = this_loc

    return loc_json
Beispiel #13
0
def load_locs_json(domain, selected_loc_id=None, include_archived=False):
    """initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.

    what is initialized:
    * all top level locs
    * if a 'selected' loc is provided, that loc and its complete
      ancestry
    """
    def loc_to_json(loc):
        return {
            'name': loc.name,
            'location_type': loc.location_type,
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
        }

    loc_json = [
        loc_to_json(loc) for loc in
        SQLLocation.root_locations(
            domain, include_archive_ancestors=include_archived
        )
    ]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        selected = SQLLocation.objects.get(
            domain=domain,
            location_id=selected_loc_id
        )

        lineage = selected.get_ancestors()

        parent = {'children': loc_json}
        for loc in lineage:
            # find existing entry in the json tree that corresponds to this loc
            this_loc = [k for k in parent['children'] if k['uuid'] == loc.location_id][0]
            this_loc['children'] = [
                loc_to_json(loc) for loc in
                loc.child_locations(include_archive_ancestors=include_archived)
            ]
            parent = this_loc

    return loc_json
    def get_location_details(self, location_id):
        result = defaultdict(dict)

        if not location_id:
            return result

        if location_id in self.location_details:
            return self.location_details[location_id]

        location = SQLLocation.by_location_id(location_id)
        if location:
            locs = location.get_ancestors(include_self=True).select_related('location_type')
            for loc in locs:
                result[loc.location_type.code] = {
                    'location_id': loc.location_id,
                    'name': loc.name,
                    'site_code': loc.site_code,
                }

        self.location_details[location_id] = result
        return result
Beispiel #15
0
def host_case_owner_location_from_case(domain, case):
    if not case:
        return None

    try:
        host = case.host
    except (CaseNotFound, ResourceNotFound):
        return None

    if not host:
        return None

    location_id = host.owner_id
    if not location_id:
        return None

    location = SQLLocation.by_location_id(location_id)
    if not location or location.is_archived or location.domain != domain:
        return None

    return location
Beispiel #16
0
def load_restricted_locs(domain, selected_loc_id=None, user=None, show_test=False):
    """
    Returns a dict of a set of locations based on location selected and accessible locations.
    1. User with full organisation access:
       a) with no location selected: use root locations.
       b) with a location selected: along with root locations
         include the hierarchal path uptil the selected location from the root. For example :
           if loc2 is selected and hierarchy is loc1>loc2>loc3 then it will return
            [{ name:loc1,
               children: [{ name: loc2
                          }]
             },
             { name: loc5 }
            ]
    2. User with limited access(only locations under user access are included)
       a) with no location selected: By default selected location is user's primary location and
                                     rest is same as point 1.b
       b) with a location selected: Same as Point 1.b

    Basic logic is: Start with all root locations, put all children of location
                    which is drilled(based on user permission) but drill only the  location
                    which lies in hierarchy of selected location.
    """
    def loc_to_json(loc):
        return {
            'name': loc.name,
            'location_type': loc.location_type.name,  # todo: remove when types aren't optional
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
            'can_edit': True
        }

    def _get_ancestor_loc_dict(location_list, location_id):
        for parent_location in location_list:
            if parent_location['uuid'] == location_id:
                return parent_location
        return None

    def location_transform(locations):
        """
        It returns a dict of locations mapped with their parents.
        :param locations: list of locations(list of SQLLocation objects)
        :return: {
        None: [loc1, loc2, loc3],  # None signifies No parent. i.e locs are root locations
        loc1.id: [loc5, loc6, loc7],
        }
        """
        loc_dict = defaultdict(list)
        for loc in locations:
            # do not include test locations
            if not show_test and loc.metadata.get('is_test_location', 'real') == 'test':
                continue
            parent_id = loc.parent_id
            loc_dict[parent_id].append(loc)

        return loc_dict

    accessible_location = SQLLocation.objects.accessible_to_user(domain, user)
    location_level = 0
    lineage = []
    if selected_loc_id:
        location = SQLLocation.by_location_id(selected_loc_id)
        location_level = LOCATION_TYPES.index(location.location_type.name)
        lineage = location.get_ancestors()
        all_ancestors = [loc.id for loc in lineage]
        # No need to pull the locations whose parents or they themselves do not present in the
        # hierarchy of the location selected. Also check for parent_id null in case root location
        # is selected
        accessible_location = accessible_location.filter(Q(parent_id__in=all_ancestors) | Q(parent_id__isnull=True))

    # Only pull locations which are above or at level of location selected
    accessible_location = accessible_location.filter(
        location_type__name__in=LOCATION_TYPES[:location_level + 1])

    parent_child_dict = location_transform(set(accessible_location).union(set(lineage)))
    locations_list = [loc_to_json(loc) for loc in parent_child_dict[None]]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        json_at_level = locations_list
        for loc in lineage:
            # Get the ancestor_dict out of all present at the level
            # which needs to be drilled down
            ancestor_loc_dict = _get_ancestor_loc_dict(json_at_level, loc.location_id)

            # could not find the ancestor at the level,
            # user should not have reached at this point to try and access an ancestor that is not permitted
            if ancestor_loc_dict is None:
                break

            children = parent_child_dict.get(loc.id, [])
            ancestor_loc_dict['children'] = [loc_to_json(loc) for loc in children]

            # reset level to one level down to find ancestor in next iteration
            json_at_level = ancestor_loc_dict['children']

    return locations_list
    if not case:
        return None

    try:
        host = case.host
    except (CaseNotFound, ResourceNotFound):
        return None

    if not host:
        return None

    location_id = host.owner_id
    if not location_id:
        return None

    location = SQLLocation.by_location_id(location_id)
    if not location or location.is_archived or location.domain != reminder.domain:
        return None

    return [location]


def host_case_owner_location_parent(handler, reminder):
    result = host_case_owner_location(handler, reminder)
    if not result:
        return None

    parent_location = result[0].parent
    if not parent_location:
        return None
Beispiel #18
0
def get_test_district_locations_id(domain):
    return [
        sql_location.location_id
        for sql_location in SQLLocation.by_domain(domain).filter(location_type__code=const.LocationTypes.DISTRICT)
        if sql_location.metadata.get('is_test_location', 'real') == 'test'
    ]
Beispiel #19
0
def get_test_district_locations_id(domain):
    return [
        sql_location.location_id
        for sql_location in SQLLocation.by_domain(domain).filter(location_type__code=const.LocationTypes.DISTRICT)
        if sql_location.metadata.get('is_test_location', 'real') == 'test'
    ]
Beispiel #20
0
def load_locs_json(domain,
                   selected_loc_id=None,
                   include_archived=False,
                   user=None,
                   only_administrative=False):
    """initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.

    what is initialized:
    * all top level locs
    * if a 'selected' loc is provided, that loc and its complete
      ancestry

    only_administrative - if False get all locations
                          if True get only administrative locations
    """
    from .permissions import (user_can_edit_location, user_can_view_location,
                              user_can_access_location_id)

    def loc_to_json(loc, project):
        ret = {
            'name': loc.name,
            'location_type':
            loc.location_type.name,  # todo: remove when types aren't optional
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
            'can_edit': True
        }
        if user:
            if user.has_permission(domain, 'access_all_locations'):
                ret['can_edit'] = user_can_edit_location(user, loc, project)
            else:
                ret['can_edit'] = user_can_access_location_id(
                    domain, user, loc.location_id)
        return ret

    project = Domain.get_by_name(domain)

    locations = SQLLocation.root_locations(
        domain, include_archive_ancestors=include_archived)

    if only_administrative:
        locations = locations.filter(location_type__administrative=True)

    loc_json = [
        loc_to_json(loc, project) for loc in locations
        if user is None or user_can_view_location(user, loc, project)
    ]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        selected = SQLLocation.objects.get(domain=domain,
                                           location_id=selected_loc_id)

        lineage = selected.get_ancestors()

        parent = {'children': loc_json}
        for loc in lineage:
            children = loc.child_locations(
                include_archive_ancestors=include_archived)
            if only_administrative:
                children = children.filter(location_type__administrative=True)

            # find existing entry in the json tree that corresponds to this loc
            try:
                this_loc = [
                    k for k in parent['children']
                    if k['uuid'] == loc.location_id
                ][0]
            except IndexError:
                # if we couldn't find this location the view just break out of the loop.
                # there are some instances in viewing archived locations where we don't actually
                # support drilling all the way down.
                break
            this_loc['children'] = [
                loc_to_json(loc, project) for loc in children
                if user is None or user_can_view_location(user, loc, project)
            ]
            parent = this_loc

    return loc_json
Beispiel #21
0
def _user_locations_ids(project, show_administrative):
    locations = SQLLocation.by_domain(project.name)
    if show_administrative == 'False':
        locations = locations.filter(location_type__administrative=True)
    # admins and users not assigned to a location can see and edit everything
    return locations.values_list('location_id', flat=True)
    def setUpClass(cls):
        cls.domain = Domain(name=DOMAIN)
        cls.domain.save()

        cls.country = LocationType(domain=DOMAIN, name='country')
        cls.country.save()
        cls.state = LocationType(
            domain=DOMAIN,
            name='state',
            parent_type=cls.country,
        )
        cls.state.save()
        cls.city = LocationType(
            domain=DOMAIN,
            name='city',
            parent_type=cls.state,
            shares_cases=True,
        )
        cls.city.save()

        cls.usa = SQLLocation(
            domain=DOMAIN,
            name='The United States of America',
            site_code='usa',
            location_type=cls.country,
        )
        cls.usa.save()
        cls.massachusetts = SQLLocation(
            domain=DOMAIN,
            name='Massachusetts',
            site_code='massachusetts',
            location_type=cls.state,
            parent=cls.usa,
        )
        cls.massachusetts.save()
        cls.new_york = SQLLocation(
            domain=DOMAIN,
            name='New York',
            site_code='new_york',
            location_type=cls.state,
            parent=cls.usa,
        )
        cls.new_york.save()

        cls.cambridge = SQLLocation(
            domain=DOMAIN,
            name='Cambridge',
            site_code='cambridge',
            location_type=cls.city,
            parent=cls.massachusetts,
        )
        cls.cambridge.save()
        cls.somerville = SQLLocation(
            domain=DOMAIN,
            name='Somerville',
            site_code='somerville',
            location_type=cls.city,
            parent=cls.massachusetts,
        )
        cls.somerville.save()
        cls.nyc = SQLLocation(
            domain=DOMAIN,
            name='New York City',
            site_code='nyc',
            location_type=cls.city,
            parent=cls.new_york,
        )
        cls.nyc.save()

        cls.drew = CommCareUser(
            domain=DOMAIN,
            username='******',
            location_id=cls.nyc.location_id,
            assigned_location_ids=[cls.nyc.location_id],
        )
        cls.jon = CommCareUser(
            domain=DOMAIN,
            username='******',
            location_id=cls.cambridge.location_id,
            assigned_location_ids=[cls.cambridge.location_id],
        )
        cls.nate = CommCareUser(
            domain=DOMAIN,
            username='******',
            location_id=cls.somerville.location_id,
            assigned_location_ids=[cls.somerville.location_id],
        )
        cls.sheel = CommCareUser(
            domain=DOMAIN,
            username='******',
            location_id=cls.somerville.location_id,
            assigned_location_ids=[cls.somerville.location_id],
            last_login=datetime.datetime.now(),
            date_joined=datetime.datetime.now(),
        )
        cls.sheel.save()
Beispiel #23
0
            if group.domain != self.domain:
                return None

            return group
        elif self.recipient_type == self.RECIPIENT_TYPE_USER_GROUP:
            try:
                group = Group.get(self.recipient_id)
            except ResourceNotFound:
                return None

            if group.domain != self.domain:
                return None

            return group
        elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION:
            location = SQLLocation.by_location_id(self.recipient_id)

            if location is None:
                return None

            if location.domain != self.domain:
                return None

            return location
        else:
            raise UnknownRecipientType(self.recipient_type)

    @staticmethod
    def recipient_is_an_individual_contact(recipient):
        return (isinstance(recipient, (CommCareUser, WebUser))
                or is_commcarecase(recipient))
Beispiel #24
0
def _user_locations_ids(project, show_administrative):
    locations = SQLLocation.by_domain(project.name)
    if show_administrative == 'False':
        locations = locations.filter(location_type__administrative=True)
    # admins and users not assigned to a location can see and edit everything
    return locations.values_list('location_id', flat=True)
Beispiel #25
0
def load_locs_json(domain, selected_loc_id=None, include_archived=False,
        user=None, only_administrative=False):
    """initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.

    what is initialized:
    * all top level locs
    * if a 'selected' loc is provided, that loc and its complete
      ancestry

    only_administrative - if False get all locations
                          if True get only administrative locations
    """
    from .permissions import user_can_edit_location, user_can_view_location
    def loc_to_json(loc, project):
        ret = {
            'name': loc.name,
            'location_type': loc.location_type.name,  # todo: remove when types aren't optional
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
            'can_edit': True
        }
        if user:
            ret['can_edit'] = user_can_edit_location(user, loc, project)
        return ret

    project = Domain.get_by_name(domain)

    locations = SQLLocation.root_locations(
        domain, include_archive_ancestors=include_archived
    )

    if only_administrative:
        locations = locations.filter(location_type__administrative=True)

    loc_json = [
        loc_to_json(loc, project) for loc in locations
        if user is None or user_can_view_location(user, loc, project)
    ]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        selected = SQLLocation.objects.get(
            domain=domain,
            location_id=selected_loc_id
        )

        lineage = selected.get_ancestors()

        parent = {'children': loc_json}
        for loc in lineage:
            children = loc.child_locations(include_archive_ancestors=include_archived)
            if only_administrative:
                children = children.filter(location_type__administrative=True)
            # find existing entry in the json tree that corresponds to this loc
            this_loc = [k for k in parent['children'] if k['uuid'] == loc.location_id][0]
            this_loc['children'] = [
                loc_to_json(loc, project) for loc in children
                if user is None or user_can_view_location(user, loc, project)
            ]
            parent = this_loc

    return loc_json
    def setUpClass(cls):
        super(LocationHierarchyTest, cls).setUpClass()
        domain = "a-song-of-ice-and-fire"
        domain_obj = create_domain(domain)
        continent_location_type = LocationType(
            domain=domain,
            name="continent",
            code="continent",
        )
        continent_location_type.save()
        kingdom_location_type = LocationType(
            domain=domain,
            name="kingdom",
            code="kingdom",
            parent_type=continent_location_type,
        )
        kingdom_location_type.save()
        city_location_type = LocationType(
            domain=domain,
            name="city",
            code="city",
            parent_type=kingdom_location_type,
        )
        city_location_type.save()

        continent = SQLLocation(
            domain=domain,
            name="Westeros",
            location_type=continent_location_type,
            site_code="westeros",
        )
        continent.save()
        kingdom = SQLLocation(
            domain=domain,
            name="The North",
            location_type=kingdom_location_type,
            parent=continent,
            site_code="the_north",
        )
        kingdom.save()
        city = SQLLocation(
            domain=domain,
            name="Winterfell",
            location_type=city_location_type,
            parent=kingdom,
            site_code="winterfell",
        )
        city.save()

        cls.domain_obj = domain_obj
        cls.domain = domain
        cls.continent = continent
        cls.kingdom = kingdom
        cls.city = city
Beispiel #27
0
 def sql_location(self):
     from corehq.apps.locations.models import SQLLocation
     if self.location_id:
         return SQLLocation.by_location_id(self.location_id)