Beispiel #1
0
def _restore_collection(in_zip, mission, file_type):
    collection, created = models.Collection.objects.get_or_create(
        mission=mission, file_type=file_type
    )

    location_descs = json.loads(in_zip.read(
        "config/collections/%s/locations.json" % collection)
    )
    for location_desc in location_descs:
        models.Location.objects.get_or_create(
            collection=collection, **location_desc
        )

    zip_config_path = "config/collections/%s/collection.conf" % collection
    errors = check_collection_configuration(
        CollectionConfigurationReader.from_fileobject(
            in_zip.open(zip_config_path)
        )
    )
    if not errors:
        backup_config(collection.config_path)
        _restore_file(in_zip, zip_config_path, collection.config_path)
        logger.info("Restored configuration for collection %s" % collection)
    else:
        logger.warn(
            "Could not restore collection configuration due to errors:\n%s"
            % ("\n".join(errors))
        )
Beispiel #2
0
    def _check_errors(self, path, old_path, collection):
        # check for errors
        if collection:
            reader = CollectionConfigurationReader(path)
            errors = check_collection_configuration(reader)
        else:
            reader = GlobalReader(path)
            errors = check_global_configuration(reader)

        if errors:
            self.error("Configuration contains errors:\n   - %s" %
                       ("\n   - ".join(errors)))
        return errors
Beispiel #3
0
def _restore_collection(in_zip, mission, file_type):
    collection, created = models.Collection.objects.get_or_create(
        mission=mission, file_type=file_type)

    location_descs = json.loads(
        in_zip.read("config/collections/%s/locations.json" % collection))
    for location_desc in location_descs:
        models.Location.objects.get_or_create(collection=collection,
                                              **location_desc)

    zip_config_path = "config/collections/%s/collection.conf" % collection
    errors = check_collection_configuration(
        CollectionConfigurationReader.from_fileobject(
            in_zip.open(zip_config_path)))
    if not errors:
        backup_config(collection.config_path)
        _restore_file(in_zip, zip_config_path, collection.config_path)
        logger.info("Restored configuration for collection %s" % collection)
    else:
        logger.warn(
            "Could not restore collection configuration due to errors:\n%s" %
            ("\n".join(errors)))
Beispiel #4
0
def configuration_view(request, mission, file_type):
    """ View function to provide a change form for the collections configuration.
    For the metadata mapping a seperate formset is used.
    """
    collection = models.Collection.objects.get(mission=mission,
                                               file_type=file_type)

    config = collection.configuration

    if request.method == "POST":
        configuration_form = forms.CollectionConfigurationForm(request.POST)
        mapping_formsets = _generate_mapping_formsets(collection,
                                                      POST=request.POST)

        mapping_formsets_valid = all(
            [f[1].is_valid() for f in mapping_formsets])

        if configuration_form.is_valid() and mapping_formsets_valid:
            for key, value in configuration_form.cleaned_data.items():
                setattr(config, key, value)

            for location, mapping_formset in mapping_formsets:
                mapping = SortedDict()
                for form in mapping_formset:
                    if form.is_valid():
                        data = form.cleaned_data
                        if data.get(
                                "DELETE") or not data.get("index_file_key"):
                            continue
                        mapping[data["search_key"]] = data["index_file_key"]

                if location:
                    config.set_section_dict(
                        "metadata_mapping.%s" % location.url, mapping)
                else:
                    config.default_metadata_mapping = mapping

            errors = check_collection_configuration(config)

            if errors:
                for error in errors:
                    messages.error(request, error)
            else:
                backup_config(config._config_path)
                config.write()
                messages.info(
                    request,
                    "Saved configuration for collection %s." % collection)

            # re-read the forms here with the updated configuration
            config.read(reset=True)

            configuration_form = forms.CollectionConfigurationForm(
                initial={
                    "export_interval":
                    timedelta_to_duration(config.export_interval),
                    "harvest_interval":
                    timedelta_to_duration(config.harvest_interval),
                    "available_result_list_fields":
                    config.available_result_list_fields,
                    "available_alignment_fields":
                    config.available_alignment_fields
                })
            mapping_formsets = _generate_mapping_formsets(collection,
                                                          config=config)

    else:
        configuration_form = forms.CollectionConfigurationForm(
            initial={
                "export_interval": timedelta_to_duration(
                    config.export_interval),
                "harvest_interval": timedelta_to_duration(
                    config.harvest_interval),
                "available_result_list_fields":
                config.available_result_list_fields,
                "available_alignment_fields": config.available_alignment_fields
            })
        mapping_formsets = _generate_mapping_formsets(collection,
                                                      config=config)

    return render(
        request, "inventory/collection/configuration.html", {
            "collections": models.Collection.objects.all(),
            "collection": collection,
            "configuration_form": configuration_form,
            "mapping_formsets": mapping_formsets
        })
Beispiel #5
0
def configuration_view(request, mission, file_type):
    """ View function to provide a change form for the collections configuration.
    For the metadata mapping a seperate formset is used.
    """
    collection = models.Collection.objects.get(
        mission=mission, file_type=file_type
    )

    config = collection.configuration

    if request.method == "POST":
        configuration_form = forms.CollectionConfigurationForm(request.POST)
        mapping_formsets = _generate_mapping_formsets(
            collection, POST=request.POST
        )

        mapping_formsets_valid = all([
            f[1].is_valid() for f in mapping_formsets
        ])

        if configuration_form.is_valid() and mapping_formsets_valid:
            for key, value in configuration_form.cleaned_data.items():
                setattr(config, key, value)

            for location, mapping_formset in mapping_formsets:
                mapping = SortedDict()
                for form in mapping_formset:
                    if form.is_valid():
                        data = form.cleaned_data
                        if data.get("DELETE") or not data.get("index_file_key"):
                            continue
                        mapping[data["search_key"]] = data["index_file_key"]

                if location:
                    config.set_section_dict(
                        "metadata_mapping.%s" % location.url, mapping
                    )
                else:
                    config.default_metadata_mapping = mapping

            errors = check_collection_configuration(config)

            if errors:
                for error in errors:
                    messages.error(request, error)
            else:
                backup_config(config._config_path)
                config.write()
                messages.info(request,
                    "Saved configuration for collection %s." % collection
                )

            # re-read the forms here with the updated configuration
            config.read(reset=True)

            configuration_form = forms.CollectionConfigurationForm(
                initial={
                    "export_interval": timedelta_to_duration(
                        config.export_interval
                    ),
                    "harvest_interval": timedelta_to_duration(
                        config.harvest_interval
                    ),
                    "available_result_list_fields":
                        config.available_result_list_fields,
                    "available_alignment_fields":
                        config.available_alignment_fields
                }
            )
            mapping_formsets = _generate_mapping_formsets(
                collection, config=config
            )

    else:
        configuration_form = forms.CollectionConfigurationForm(
            initial={
                "export_interval": timedelta_to_duration(
                    config.export_interval
                ),
                "harvest_interval": timedelta_to_duration(
                    config.harvest_interval
                ),
                "available_result_list_fields":
                    config.available_result_list_fields,
                "available_alignment_fields":
                    config.available_alignment_fields
            }
        )
        mapping_formsets = _generate_mapping_formsets(
            collection, config=config
        )

    return render(
        request, "inventory/collection/configuration.html", {
            "collections": models.Collection.objects.all(),
            "collection": collection, "configuration_form": configuration_form,
            "mapping_formsets": mapping_formsets
        }
    )