示例#1
0
    def clean_assigned_locations(self):
        from corehq.apps.locations.models import SQLLocation
        from corehq.apps.locations.util import get_locations_from_ids

        location_ids = self.data.getlist('assigned_locations')
        try:
            locations = get_locations_from_ids(location_ids, self.domain)
        except SQLLocation.DoesNotExist:
            raise ValidationError(_('One or more of the locations was not found.'))

        return [location.location_id for location in locations]
示例#2
0
def child_locations_for_select2(request, domain):
    id = request.GET.get('id')
    ids = request.GET.get('ids')
    query = request.GET.get('name', '').lower()
    user = request.couch_user

    base_queryset = SQLLocation.objects.accessible_to_user(domain, user)

    def loc_to_payload(loc):
        return {'id': loc.location_id, 'name': loc.get_path_display()}

    if id:
        try:
            loc = base_queryset.get(location_id=id)
            if loc.domain != domain:
                raise SQLLocation.DoesNotExist()
        except SQLLocation.DoesNotExist:
            return json_response(
                {'message': 'no location with id %s found' % id},
                status_code=404,
            )
        else:
            return json_response(loc_to_payload(loc))
    elif ids:
        from corehq.apps.locations.util import get_locations_from_ids
        ids = json.loads(ids)
        try:
            locations = get_locations_from_ids(ids,
                                               domain,
                                               base_queryset=base_queryset)
        except SQLLocation.DoesNotExist:
            return json_response(
                {'message': 'one or more locations not found'},
                status_code=404,
            )
        return json_response([loc_to_payload(loc) for loc in locations])
    else:
        locs = []
        user_loc = user.get_sql_location(domain)

        if user_can_edit_any_location(user, request.project):
            locs = base_queryset.filter(domain=domain, is_archived=False)
        elif user_loc:
            locs = user_loc.get_descendants(include_self=True)

        if locs != [] and query:
            locs = locs.filter(name__icontains=query)

        return json_response(map(loc_to_payload, locs[:10]))
示例#3
0
def child_locations_for_select2(request, domain):
    id = request.GET.get('id')
    ids = request.GET.get('ids')
    query = request.GET.get('name', '').lower()
    user = request.couch_user

    base_queryset = SQLLocation.objects.accessible_to_user(domain, user)

    def loc_to_payload(loc):
        return {'id': loc.location_id, 'name': loc.display_name}

    if id:
        try:
            loc = base_queryset.get(location_id=id)
            if loc.domain != domain:
                raise SQLLocation.DoesNotExist()
        except SQLLocation.DoesNotExist:
            return json_response(
                {'message': 'no location with id %s found' % id},
                status_code=404,
            )
        else:
            return json_response(loc_to_payload(loc))
    elif ids:
        from corehq.apps.locations.util import get_locations_from_ids
        ids = json.loads(ids)
        try:
            locations = get_locations_from_ids(ids, domain, base_queryset=base_queryset)
        except SQLLocation.DoesNotExist:
            return json_response(
                {'message': 'one or more locations not found'},
                status_code=404,
            )
        return json_response([loc_to_payload(loc) for loc in locations])
    else:
        locs = []
        user_loc = user.get_sql_location(domain)

        if user_can_edit_any_location(user, request.project):
            locs = base_queryset.filter(domain=domain, is_archived=False)
        elif user_loc:
            locs = user_loc.get_descendants(include_self=True)

        if locs != [] and query:
            locs = locs.filter(name__icontains=query)

        return json_response(map(loc_to_payload, locs[:10]))
示例#4
0
    def clean_assigned_locations(self):
        # select2 (< 4.0) doesn't format multiselect for remote data as an array
        #   but formats it as comma-seperated list, so we need to clean the returned data
        from corehq.apps.locations.models import SQLLocation
        from corehq.apps.locations.util import get_locations_from_ids

        value = self.cleaned_data.get('assigned_locations')
        if not isinstance(value, basestring) or value.strip() == '':
            return []

        location_ids = [location_id.strip() for location_id in value.split(',')]
        try:
            locations = get_locations_from_ids(location_ids, self.domain)
        except SQLLocation.DoesNotExist:
            raise ValidationError(_('One or more of the locations was not found.'))

        return [location.location_id for location in locations]
示例#5
0
    def render(self, name, value, attrs=None):
        location_ids = value.split(',') if value else []
        from corehq.apps.locations.util import get_locations_from_ids
        try:
            locations = get_locations_from_ids(location_ids, self.domain)
        except SQLLocation.DoesNotExist:
            locations = []
        initial_data = [{'id': loc.location_id, 'name': loc.display_name} for loc in locations]

        return get_template('locations/manage/partials/autocomplete_select_widget.html').render(Context({
            'id': self.id,
            'name': name,
            'value': value or '',
            'query_url': self.query_url,
            'multiselect': self.multiselect,
            'initial_data': initial_data,
        }))
示例#6
0
    def clean_assigned_locations(self):
        # select2 (< 4.0) doesn't format multiselect for remote data as an array
        #   but formats it as comma-seperated list, so we need to clean the returned data
        from corehq.apps.locations.models import SQLLocation
        from corehq.apps.locations.util import get_locations_from_ids

        value = self.cleaned_data.get('assigned_locations')
        if not isinstance(value, basestring) or value.strip() == '':
            return []

        location_ids = [location_id.strip() for location_id in value.split(',')]
        try:
            locations = get_locations_from_ids(location_ids, self.domain)
        except SQLLocation.DoesNotExist:
            raise ValidationError(_('One or more of the locations was not found.'))

        return [location.location_id for location in locations]