Exemplo n.º 1
0
    def prepare_update_record(self, data, record):
        # This contains all fields which can be removed from record (they are optional)
        # if new value sent from the form is None, or empty in any other way
        # (after de-serialization if it's missing from input data)
        # this fields will be removed from record
        additional_fields = [
            "external_job_identifier",
            "accelerator_experiments",
            "urls",
            "contact_details",
            "reference_letters",
        ]

        if not is_superuser_or_cataloger_logged_in():
            old_status = record.get("status", "pending")
            new_status = data.get("status", old_status)
            if (
                new_status != old_status
                and new_status not in self.user_allowed_status_changes[old_status]
            ):
                raise RESTDataError(
                    f"Only curator can change status from '{old_status}' to '{new_status}'."
                )
        record_data = dict(record)
        for key in additional_fields:
            if key not in data and key in record_data:
                del record_data[key]
        record_data.update(data)
        builder = JobBuilder(record=record_data)
        return builder
Exemplo n.º 2
0
 def get_valid_record_data_from_builder(self, builder):
     try:
         builder.validate_record()
     except ValidationError as e:
         LOGGER.exception("Cannot process job submission")
         raise RESTDataError(e.args[0])
     except SchemaError as e:
         LOGGER.exception("Schema is broken")
         abort(500, str(e))
     data = builder.record
     return data
Exemplo n.º 3
0
    def raise_if_user_can_not_modify_status(self, data, existing_record):
        if is_superuser_or_cataloger_logged_in():
            return

        old_status = existing_record.get("status", "pending")
        new_status = data.get("status", old_status)
        deadline = data.get("deadline_date")

        has_status_changed = new_status != old_status
        is_change_to_new_status_allowed = (
            new_status in self.user_allowed_status_changes[old_status])
        can_change_status = (is_change_to_new_status_allowed and
                             not has_30_days_passed_after_deadline(deadline))

        if has_status_changed and not can_change_status:
            raise RESTDataError(
                f"Only curator can change status from '{old_status}' to '{new_status}'."
            )
Exemplo n.º 4
0
    def json_loader():
        request_json = request.get_json().get("data")

        context = {}
        pid_data = request.view_args.get("pid_value")
        if pid_data:
            pid, _ = pid_data.data
            context["pid"] = pid

        try:
            result = schema_class(context=context).load(request_json)
        except ValueError as e:
            raise RESTDataError(e.args)

        # To return nice message when builder.validation() will fail
        if result.errors:
            raise MarshmallowErrors(result.errors)
        return result.data
Exemplo n.º 5
0
    def json_loader():
        request_json = request.get_json().get("data")

        context = {}
        pid_data = request.view_args.get("pid_value")
        if pid_data:
            pid = pid_data
            context["pid"] = pid

        try:
            result = schema_class(context=context).load(request_json)
        except ValueError as e:
            raise RESTDataError() from e
        except Exception as e:
            raise LoaderDataError(e.args) from e

        if result.errors:
            raise MarshmallowErrors(result.errors)
        return result.data
Exemplo n.º 6
0
    def prepare_data(self, data, record=None):
        """Prepares data received from form.
        As jobs do not have any 'workflows' it's required to set all the logic
        for updating record from data provided by the user somewhere..."""

        if record:
            builder = self.prepare_update_record(data, record)
        else:
            builder = self.prepare_new_record(data)

        try:
            builder.validate_record()
        except ValidationError as e:
            LOGGER.exception("Cannot process job submission")
            raise RESTDataError(e.args[0])
        except SchemaError as e:
            LOGGER.exception("Schema is broken")
            abort(500, str(e))
        data = builder.record
        return data