Exemple #1
0
    def post(self, request):
        """Valid a form using POST data and return any error messages"""

        form_data = request.POST

        measurement_forms = utils.create_measurement_form(form_data)
        #filters out all valid forms.
        for form in filter(lambda x: not measurement_forms[x].is_valid(), measurement_forms):
            for error in measurement_forms[form].errors:
                return JsonResponse({'status': "ERROR", 'message': measurement_forms[form].errors[error][0]})
        return JsonResponse({'status': "OK", 'message': "OK"})
Exemple #2
0
    def post(self, request):
        """Export a dataset type to yaml from forms

        Using the metadata and measurement forms, create an ordered yaml file that can be used to add the dataset
        to the database.

        POST Data:
            measurement form(s), metadata form

        Returns:
            Json response with a status and (if OK) a url to a yaml file
        """
        form_data = request.POST
        measurements = json.loads(form_data.get('measurements'))
        metadata = json.loads(form_data.get('metadata_form'))
        #each measurement_form contains a dict of other forms..
        measurement_forms = [
            utils.create_measurement_form(measurements[measurement])
            for measurement in measurements
        ]
        #just a single form
        metadata_form = forms.DatasetTypeMetadataForm(metadata)

        valid, error = utils.validate_dataset_type_forms(
            metadata_form, measurement_forms)
        if not valid:
            return JsonResponse({'status': "ERROR", 'message': error})

        #since everything is valid, now create yaml from defs..
        product_def = utils.dataset_type_definition_from_forms(
            metadata_form, measurement_forms)
        try:
            os.makedirs(
                os.path.join(settings.RESULTS_DATA_DIR,
                             'data_cube_manager/product_defs/'))
        except:
            pass

        def _dict_representer(dumper, data):
            return dumper.represent_mapping(
                yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items())

        yaml.SafeDumper.add_representer(OrderedDict, _dict_representer)

        yaml_url = os.path.join(settings.RESULTS_DATA_DIR, 'data_cube_manager/product_defs/') \
            + str(uuid.uuid4()) + '.yaml'
        with open(yaml_url, 'w') as yaml_file:
            yaml.dump(product_def,
                      yaml_file,
                      Dumper=yaml.SafeDumper,
                      default_flow_style=False,
                      indent=4)
        return JsonResponse({'status': 'OK', 'url': yaml_url})
Exemple #3
0
    def post(self, request):
        """Add a dataset type to the database

        Using forms, create a json representation of a dataset type and attempt to index it in the database.

        POST Data:
            Measurement forms, metadata forms
        """
        if not request.user.is_superuser:
            return JsonResponse({'status': "ERROR", 'message': "Only superusers can add or update datasets."})

        form_data = request.POST
        measurements = json.loads(form_data.get('measurements'))
        metadata = json.loads(form_data.get('metadata_form'))
        #each measurement_form contains a dict of other forms..
        measurement_forms = [utils.create_measurement_form(measurements[measurement]) for measurement in measurements]
        #just a single form
        metadata_form = forms.DatasetTypeMetadataForm(metadata)

        valid, error = utils.validate_dataset_type_forms(metadata_form, measurement_forms)
        if not valid:
            return JsonResponse({'status': "ERROR", 'message': error})

        if models.DatasetType.objects.using('agdc').filter(name=metadata_form.cleaned_data['name']).exists():
            return JsonResponse({
                'status':
                "ERROR",
                'message':
                'A dataset type already exists with the entered name. Please enter a new name for your dataset and ensure that the definition is different.'
            })

        #since everything is valid, now create yaml from defs..
        product_def = utils.dataset_type_definition_from_forms(metadata_form, measurement_forms)

        conf_path = '/home/' + settings.LOCAL_USER + '/Datacube/data_cube_ui/config/.datacube.conf'
        index = index_connect(local_config=conf_path)
        try:
            type_ = index.products.from_doc(product_def)
            index.products.add(type_)
        except:
            return JsonResponse({
                'status':
                "ERROR",
                'message':
                'Invalid product definition. Please contact a system administrator if this problem persists.'
            })

        return JsonResponse({'status': 'OK', 'message': 'Your dataset type has been added to the database.'})