Beispiel #1
0
def import_locations_async(domain, file_ref_id, user_id):
    importer = MultiExcelImporter(import_locations_async, file_ref_id)
    results = new_locations_import(domain, importer, CouchUser.get_by_user_id(user_id))
    importer.mark_complete()

    if LOCATIONS_IN_UCR.enabled(domain):
        # We must rebuild datasources once the location import is complete in
        # case child locations were not updated, but a parent location was.
        # For example if a state was updated, the county may reference the state
        # and need to have its row updated
        datasources = get_datasources_for_domain(domain, "Location", include_static=True)
        for datasource in datasources:
            rebuild_indicators_in_place.delay(datasource.get_id)

    if getattr(settings, 'CELERY_ALWAYS_EAGER', False):
        # Log results because they are not sent to the view when
        # CELERY_ALWAYS_EAGER is true
        logging.getLogger(__name__).info(
            "import_locations_async %s results: %s -> success=%s",
            file_ref_id,
            " ".join(
                "%s=%r" % (name, getattr(results, name))
                for name in ["messages", "warnings", "errors"]
                if getattr(results, name)
            ),
            results.success,
        )

    return {
        'messages': results
    }
Beispiel #2
0
    def bootstrap(self, domain):
        self.all_sources = get_app_sources(domain)
        self.application_field.choices = sorted(
            [(app_id, source['name']) for app_id, source in self.all_sources.items()],
            key=lambda id_name_tuple: (id_name_tuple[1] or '').lower()
        )
        self.source_field.choices = []

        def _add_choices(field, choices):
            field.choices.extend(choices)
            # it's weird/annoying that you have to manually sync these
            field.widget.choices.extend(choices)

        if self.enable_cases:
            _add_choices(
                self.source_field,
                [(ct['value'], ct['text']) for app in self.all_sources.values() for ct in app['case']]
            )
        if self.enable_forms:
            _add_choices(
                self.source_field,
                [(ct['value'], ct['text']) for app in self.all_sources.values() for ct in app['form']]
            )
        if self.enable_raw:
            available_data_sources = get_datasources_for_domain(domain, include_static=True,
                                                                include_aggregate=AGGREGATE_UCRS.enabled(domain))
            _add_choices(
                self.source_field,
                [(ds.data_source_id, ds.display_name) for ds in available_data_sources]
            )
            # also shove these into app sources for every app for now to avoid substantially
            # messing with this code for this widget
            # (this is not the best ux / should probably be cleaned up later)
            for app_data in self.all_sources.values():
                app_data['data_source'] = [{"text": ds.display_name, "value": ds.data_source_id}
                                           for ds in available_data_sources]

        # NOTE: This corresponds to a view-model that must be initialized in your template.
        # See the doc string of this class for more information.
        self.application_field.widget.attrs = {'data-bind': 'value: application'}
        self.source_type_field.widget.attrs = {'data-bind': 'value: sourceType'}
        self.source_field.widget.attrs = {'data-bind': '''
            options: sourcesMap[application()][sourceType()],
            optionsText: function(item){return item.text},
            optionsValue: function(item){return item.value}
        '''}
Beispiel #3
0
    def copy_ucr_datasources(self):
        datasource_map = {}
        datasources = get_datasources_for_domain(self.existing_domain)
        for datasource in datasources:
            datasource.meta.build.finished = False
            datasource.meta.build.initiated = None

            old_id, new_id = self.save_couch_copy(datasource, self.new_domain)
            datasource_map[old_id] = new_id
        for static_datasource in StaticDataSourceConfiguration.by_domain(self.existing_domain):
            table_id = static_datasource.get_id.replace(
                StaticDataSourceConfiguration._datasource_id_prefix + self.existing_domain + "-", ""
            )
            new_id = StaticDataSourceConfiguration.get_doc_id(self.new_domain, table_id)
            # check that new datasource is in new domain's list of static datasources
            StaticDataSourceConfiguration.by_id(new_id)
            datasource_map[static_datasource.get_id] = new_id
        return datasource_map
Beispiel #4
0
    def copy_ucr_datasources(self):
        datasource_map = {}
        datasources = get_datasources_for_domain(self.existing_domain)
        for datasource in datasources:
            datasource.meta.build.finished = False
            datasource.meta.build.initiated = None

            old_id, new_id = self.save_couch_copy(datasource, self.new_domain)
            datasource_map[old_id] = new_id
        for static_datasource in StaticDataSourceConfiguration.by_domain(
                self.existing_domain):
            table_id = static_datasource.get_id.replace(
                StaticDataSourceConfiguration._datasource_id_prefix +
                self.existing_domain + '-', '')
            new_id = StaticDataSourceConfiguration.get_doc_id(
                self.new_domain, table_id)
            # check that new datasource is in new domain's list of static datasources
            StaticDataSourceConfiguration.by_id(new_id)
            datasource_map[static_datasource.get_id] = new_id
        return datasource_map
Beispiel #5
0
def import_locations_async(domain, file_ref_id, user_id):
    importer = MultiExcelImporter(import_locations_async, file_ref_id)
    user = CouchUser.get_by_user_id(user_id)
    results = new_locations_import(domain, importer, user)
    importer.mark_complete()

    if LOCATIONS_IN_UCR.enabled(domain):
        # We must rebuild datasources once the location import is complete in
        # case child locations were not updated, but a parent location was.
        # For example if a state was updated, the county may reference the state
        # and need to have its row updated
        datasources = get_datasources_for_domain(domain,
                                                 "Location",
                                                 include_static=True)
        for datasource in datasources:
            rebuild_indicators_in_place.delay(datasource.get_id,
                                              initiated_by=user.username,
                                              source='import_locations')

    if getattr(settings, 'CELERY_TASK_ALWAYS_EAGER', False):
        # Log results because they are not sent to the view when
        # CELERY_TASK_ALWAYS_EAGER is true
        logging.getLogger(__name__).info(
            "import_locations_async %s results: %s -> success=%s",
            file_ref_id,
            " ".join("%s=%r" % (name, getattr(results, name))
                     for name in ["messages", "warnings", "errors"]
                     if getattr(results, name)),
            results.success,
        )

    return {
        'messages': {
            'messages': results.messages,
            'errors': results.errors,
            'warnings': results.warnings,
            'success': results.success,
        }
    }
Beispiel #6
0
 def by_domain(cls, domain):
     return get_datasources_for_domain(domain)
Beispiel #7
0
 def by_domain(cls, domain):
     return get_datasources_for_domain(domain)
Beispiel #8
0
 def obj_get_list(self, bundle, **kwargs):
     self._ensure_toggle_enabled(bundle.request)
     domain = kwargs['domain']
     return get_datasources_for_domain(domain)
Beispiel #9
0
    def bootstrap(self, domain):
        self.all_sources = get_app_sources(domain)
        self.application_field.choices = sorted(
            [(app_id, source['name'])
             for app_id, source in self.all_sources.items()],
            key=lambda id_name_tuple: (id_name_tuple[1] or '').lower())
        if self.enable_registry:
            self.application_field.choices += [('', '--------')]
            self.all_sources.update({'': {"name": '', "case": [], "form": []}})
            self.app_and_registry_sources = get_dropdown_options(
                domain, self.all_sources, self.registry_permission_checker)
            self.all_sources.update(get_registry_case_sources(domain))

        self.source_field.choices = []

        def _add_choices(field, choices):
            field.choices.extend(choices)
            # it's weird/annoying that you have to manually sync these
            field.widget.choices.extend(choices)

        _add_choices(self.source_field, [(ct['value'], ct['text'])
                                         for app in self.all_sources.values()
                                         for ct in app['case']])
        _add_choices(self.source_field, [(ct['value'], ct['text'])
                                         for app in self.all_sources.values()
                                         for ct in app['form']])
        if self.enable_raw:
            available_data_sources = get_datasources_for_domain(
                domain,
                include_static=True,
                include_aggregate=AGGREGATE_UCRS.enabled(domain))
            _add_choices(self.source_field,
                         [(ds.data_source_id, ds.display_name)
                          for ds in available_data_sources])
            # also shove these into app sources for every app for now to avoid substantially
            # messing with this code for this widget
            # (this is not the best ux / should probably be cleaned up later)
            for app_data in self.all_sources.values():
                app_data['data_source'] = [{
                    "text": ds.display_name,
                    "value": ds.data_source_id
                } for ds in available_data_sources]
        self.registry_slug_field.choices = sort_tuple_field_choices_by_name([
            (registry["slug"], registry["name"])
            for registry in get_data_registry_dropdown_options(
                domain, permission_checker=self.registry_permission_checker)
        ], ) + [('', '--------')]

        # NOTE: This corresponds to a view-model that must be initialized in your template.
        # See the doc string of this class for more information.
        self.source_type_field.widget.attrs = {
            'data-bind': 'value: sourceType'
        }

        if self.enable_registry:
            self.application_field.widget.attrs = {
                'data-bind':
                '''
                value: application,
                disable: isDataFromOneProject() != 'true',
                optionsText: function(item){return item.text},
                optionsValue: function(item){return item.value},
                options: dropdownMap['app'][isDataFromOneProject()]
            '''
            }
            self.registry_slug_field.widget.attrs = {
                'data-bind':
                '''
                optionsText: function(item){return item.text},
                optionsValue: function(item){return item.value},
                value: registrySlug,
                disable: sourceType() != 'case' || isDataFromOneProject() != 'false',
                options: dropdownMap['registry'][isDataFromOneProject()]
                '''
            }
            self.source_field.widget.attrs = {
                'data-bind':
                '''
                optionsText: function(item){return item.text},
                optionsValue: function(item){return item.value},
                value: sourceId,
                options: _.union(sourcesMap[application()][sourceType()], sourcesMap[registrySlug()][sourceType()])
            '''
            }
        else:
            self.application_field.widget.attrs = {
                'data-bind': 'value: application'
            }
            self.registry_slug_field.widget.attrs = {
                'data-bind':
                '''
                optionsText: function(item){return item.text},
                optionsValue: function(item){return item.value},
                value: registrySlug
                '''
            }
            self.source_field.widget.attrs = {
                'data-bind':
                '''
                optionsText: function(item){return item.text},
                optionsValue: function(item){return item.value},
                value: sourceId,
                options: sourcesMap[application()][sourceType()]
            '''
            }