Пример #1
0
    def validate_detail_columns(self, columns):
        from corehq.apps.app_manager.suite_xml.const import FIELD_TYPE_LOCATION
        from corehq.apps.locations.util import parent_child
        from corehq.apps.locations.fixtures import should_sync_hierarchical_fixture

        hierarchy = None
        for column in columns:
            if column.field_type == FIELD_TYPE_LOCATION:
                domain = self.module.get_app().domain
                domain_obj = Domain.get_by_name(domain)
                try:
                    if not should_sync_hierarchical_fixture(domain_obj, self.module.get_app()):
                        # discontinued feature on moving to flat fixture format
                        raise LocationXpathValidationError(
                            _('That format is no longer supported. To reference the location hierarchy you need to'
                              ' use the "Custom Calculations in Case List" feature preview. For more information '
                              'see: https://confluence.dimagi.com/pages/viewpage.action?pageId=38276915'))
                    hierarchy = hierarchy or parent_child(domain)
                    LocationXpath('').validate(column.field_property, hierarchy)
                except LocationXpathValidationError as e:
                    yield {
                        'type': 'invalid location xpath',
                        'details': six.text_type(e),
                        'module': self.get_module_info(),
                        'column': column,
                    }
Пример #2
0
    def validate_detail_columns(self, columns):
        from corehq.apps.app_manager.suite_xml.const import FIELD_TYPE_LOCATION
        from corehq.apps.locations.util import parent_child
        from corehq.apps.locations.fixtures import should_sync_hierarchical_fixture

        hierarchy = None
        for column in columns:
            if column.field_type == FIELD_TYPE_LOCATION:
                domain = self.module.get_app().domain
                domain_obj = Domain.get_by_name(domain)
                try:
                    if not should_sync_hierarchical_fixture(domain_obj, self.module.get_app()):
                        # discontinued feature on moving to flat fixture format
                        raise LocationXpathValidationError(
                            _('That format is no longer supported. To reference the location hierarchy you need to'
                              ' use the "Custom Calculations in Case List" feature preview. For more information '
                              'see: https://confluence.dimagi.com/pages/viewpage.action?pageId=38276915'))
                    hierarchy = hierarchy or parent_child(domain)
                    LocationXpath('').validate(column.field_property, hierarchy)
                except LocationXpathValidationError as e:
                    yield {
                        'type': 'invalid location xpath',
                        'details': str(e),
                        'module': self.get_module_info(),
                        'column': column,
                    }
Пример #3
0
    def __init__(self, domain, excel_importer):
        self.domain = domain
        self.excel_importer = excel_importer

        self.processed = 0
        self.results = []
        self.seen_site_codes = set()

        self.parent_child_map = parent_child(self.domain)

        self.total_rows = sum(
            ws.worksheet.get_highest_row() for ws in self.excel_importer.worksheets
        )
        self.types = [ws.worksheet.title for ws in self.excel_importer.worksheets]
        self.top_level_types = top_level_location_types(domain)
Пример #4
0
    def __init__(self, domain, excel_importer):
        self.domain = domain
        self.excel_importer = excel_importer

        self.processed = 0
        self.results = []
        self.seen_site_codes = set()

        self.parent_child_map = parent_child(self.domain)

        self.total_rows = sum(
            ws.worksheet.get_highest_row() for ws in self.excel_importer.worksheets
        )
        self.types = [ws.worksheet.title for ws in self.excel_importer.worksheets]
        self.top_level_types = top_level_location_types(domain)
Пример #5
0
def _check_parent_id(parent_id, domain, location_type):
    if parent_id:
        try:
            parent_obj = Location.get(parent_id)
        except ResourceNotFound:
            raise LocationImportError(_('Parent with id {0} does not exist').format(parent_id))

        if parent_obj.domain != domain:
            raise LocationImportError(
                _('Parent ID {0} references a location in another project').format(parent_id)
            )

        parent_relationships = parent_child(domain)
        if invalid_location_type(location_type, parent_obj, parent_relationships):
            raise LocationImportError(
                _('Invalid parent type of {0} for child type {1}').format(parent_obj.location_type, location_type)
            )
Пример #6
0
def check_parent_id(parent_id, domain, location_type):
    if parent_id:
        try:
            parent_obj = Location.get(parent_id)
        except ResourceNotFound:
            return {
                'id': None,
                'message': 'Parent with id {0} does not exist'.format(
                    parent_id
                )
            }
        parent_relationships = parent_child(domain)
        if invalid_location_type(location_type, parent_obj, parent_relationships):
            return {
                'id': None,
                'message': 'Invalid parent type of {0} for child type {1}'.format(
                    parent_obj.location_type, location_type)
            }
Пример #7
0
    def HIERARCHY(self, exclude_terminal=True):
        type_q = deque()
        type_q.append(None)
        bfs_ordered = []

        relationships = loc_util.parent_child(self.domain)

        while True:
            try:
                loc_type = type_q.popleft()
            except IndexError:
                break

            child_types = relationships.get(loc_type, [])
            if loc_type and (not exclude_terminal or child_types):
                bfs_ordered.append(loc_type)
            for child_type in child_types:
                if child_type not in bfs_ordered:
                    type_q.append(child_type)
                else:
                    del bfs_ordered[bfs_ordered.index(loc_type)]
                    bfs_ordered.insert(bfs_ordered.index(child_type), loc_type)

        return [{'key': k, 'caption': k} for k in bfs_ordered]
Пример #8
0
def HIERARCHY(domain, exclude_terminal=True):
    type_q = deque()
    type_q.append(None)
    bfs_ordered = []

    relationships = loc_util.parent_child(domain)

    while True:
        try:
            loc_type = type_q.popleft()
        except IndexError:
            break

        child_types = relationships.get(loc_type, [])
        if loc_type and (not exclude_terminal or child_types):
            bfs_ordered.append(loc_type)
        for child_type in child_types:
            if child_type not in bfs_ordered:
                type_q.append(child_type)
            else:
                del bfs_ordered[bfs_ordered.index(loc_type)]
                bfs_ordered.insert(bfs_ordered.index(child_type), loc_type)

    return [{'key': k, 'caption': k} for k in bfs_ordered]
Пример #9
0
 def xpath(self):
     from corehq.apps.locations.util import parent_child
     hierarchy = parent_child(self.app.domain)
     return LocationXpath('commtrack:locations').location(
         self.column.field_property, hierarchy)
Пример #10
0
 def get_terminal(self):
     relationships = loc_util.parent_child(self.domain)
     loc_types = loc_util.defined_location_types(self.domain)
     for loc_type in loc_types:
         if not relationships.get(loc_type):
             return loc_type
Пример #11
0
def get_terminal(domain):
    relationships = loc_util.parent_child(domain)
    loc_types = loc_util.defined_location_types(domain)
    for loc_type in loc_types:
        if not relationships.get(loc_type):
            return loc_type
Пример #12
0
    def xpath(self):
        from corehq.apps.locations.util import parent_child

        hierarchy = parent_child(self.app.domain)
        return LocationXpath("commtrack:locations").location(self.column.field_property, hierarchy)
Пример #13
0
def import_location(domain, location_type, location_data, parent_child_map=None):
    data = dict(location_data)

    provided_code = data.pop('site_code', None)

    parent_site_code = data.pop('parent_site_code', None)

    if not parent_child_map:
        parent_child_map = parent_child(domain)

    form_data = {}

    try:
        parent_id = _process_parent_site_code(
            parent_site_code, domain, location_type, parent_child_map
        )
    except LocationImportError as e:
        return {
            'id': None,
            'message': _('Unable to import location {0}: {1}').format(
                data.pop('name'), e
            )
        }

    existing = None
    parent = parent_id
    if provided_code:
        existing = Location.by_site_code(domain, provided_code)
        if existing:
            if existing.location_type != location_type:
                return {
                    'id': None,
                    'message': _("Existing location type error, type of {0} is not {1}").format(
                        existing.name, location_type
                    )
                }

            parent = parent_id or existing.parent_id

    form_data['site_code'] = provided_code

    form_data['parent_id'] = parent
    form_data['name'] = data.pop('name')
    form_data['location_type'] = location_type

    lat, lon = data.pop('latitude', None), data.pop('longitude', None)
    if lat and lon:
        form_data['coordinates'] = '%s, %s' % (lat, lon)

    properties = {}
    consumption = []
    for k, v in data.iteritems():
        if k.startswith('default_'):
            consumption.append((k[8:], v))
        else:
            properties[(location_type, k)] = v

    return submit_form(
        domain,
        parent,
        form_data,
        properties,
        existing,
        location_type,
        consumption
    )
Пример #14
0
def import_location(domain, location_type, location_data, parent_child_map=None):
    data = dict(location_data)

    provided_code = data.pop('site_code', None)

    parent_site_code = data.pop('parent_site_code', None)

    if not parent_child_map:
        parent_child_map = parent_child(domain)

    form_data = {}

    try:
        parent_id = _process_parent_site_code(
            parent_site_code, domain, location_type, parent_child_map
        )
    except LocationImportError as e:
        return {
            'id': None,
            'message': _('Unable to import location {0}: {1}').format(
                data.pop('name'), e
            )
        }

    existing = None
    parent = parent_id
    if provided_code:
        existing = Location.by_site_code(domain, provided_code)
        if existing:
            if existing.location_type != location_type:
                return {
                    'id': None,
                    'message': _("Existing location type error, type of {0} is not {1}").format(
                        existing.name, location_type
                    )
                }

            parent = parent_id or existing.parent_id

    form_data['site_code'] = provided_code

    form_data['parent_id'] = parent
    form_data['name'] = data.pop('name')
    form_data['location_type'] = location_type

    lat, lon = data.pop('latitude', None), data.pop('longitude', None)
    if lat and lon:
        form_data['coordinates'] = '%s, %s' % (lat, lon)

    properties = {}
    consumption = []
    for k, v in data.iteritems():
        if k.startswith('default_'):
            consumption.append((k[8:], v))
        else:
            properties[(location_type, k)] = v

    return submit_form(
        domain,
        parent,
        form_data,
        properties,
        existing,
        location_type,
        consumption
    )