Skip to content

dataset_serializer#

VersionSerializer#

Bases: CommonModelSerializer

Source code in src/apps/core/serializers/dataset_serializer.py
class VersionSerializer(CommonModelSerializer):
    version = serializers.IntegerField(source="version_number", read_only=True)

    class Meta:
        model = Dataset
        fields = [
            "id",
            "title",
            "persistent_identifier",
            "state",
            "created",
            "removed",
            "deprecated",
            "next_draft",
            "draft_of",
            "version",
        ]
        list_serializer_class = CommonListSerializer

    def get_version(self, instance):
        return instance.version_number

    def to_representation(self, instance):
        ret = super().to_representation(instance)

        if not instance.has_permission_to_see_drafts(self.context["request"].user):
            ret.pop("next_draft", None)
            ret.pop("draft_of", None)

        return ret

LinkedDraftSerializer#

Bases: CommonNestedModelSerializer

Source code in src/apps/core/serializers/dataset_serializer.py
class LinkedDraftSerializer(CommonNestedModelSerializer):
    class Meta:
        model = Dataset
        fields = (
            "id",
            "persistent_identifier",
            "created",
            "modified",
            "title",
            "cumulative_state",
        )
        read_only_fields = fields

DatasetListSerializer#

Bases: CommonListSerializer

Source code in src/apps/core/serializers/dataset_serializer.py
class DatasetListSerializer(CommonListSerializer):

    def collect_pids(self, data: List[Dataset]):
        """Collect persistent identifiers used in dataset relations."""
        pids = set()
        for dataset in data:
            # Relations and other_identifiers should be prefetched
            for relation in dataset.relation.all():
                if pid := relation.entity.entity_identifier:
                    pids.add(clean_pid(pid))
            for oi in dataset.other_identifiers.all():
                if pid := oi.notation:
                    pids.add(clean_pid(pid))
        return pids

    def map_pids_to_datasets(self, data):
        """Save mapping of persistent_identifier to dataset id to serializer context."""
        pids = self.collect_pids(data)
        pids_ids = Dataset.available_objects.filter(
            persistent_identifier__in=pids, state=Dataset.StateChoices.PUBLISHED
        ).values_list("persistent_identifier", "id")
        mapping = {}
        for pid, id in pids_ids:
            mapping.setdefault(pid, []).append(id)
        self.context["datasets_by_pid"] = mapping

    def to_representation(self, data):
        self.map_pids_to_datasets(data)
        return super().to_representation(data)

collect_pids(data) #

Collect persistent identifiers used in dataset relations.

Source code in src/apps/core/serializers/dataset_serializer.py
def collect_pids(self, data: List[Dataset]):
    """Collect persistent identifiers used in dataset relations."""
    pids = set()
    for dataset in data:
        # Relations and other_identifiers should be prefetched
        for relation in dataset.relation.all():
            if pid := relation.entity.entity_identifier:
                pids.add(clean_pid(pid))
        for oi in dataset.other_identifiers.all():
            if pid := oi.notation:
                pids.add(clean_pid(pid))
    return pids

map_pids_to_datasets(data) #

Save mapping of persistent_identifier to dataset id to serializer context.

Source code in src/apps/core/serializers/dataset_serializer.py
def map_pids_to_datasets(self, data):
    """Save mapping of persistent_identifier to dataset id to serializer context."""
    pids = self.collect_pids(data)
    pids_ids = Dataset.available_objects.filter(
        persistent_identifier__in=pids, state=Dataset.StateChoices.PUBLISHED
    ).values_list("persistent_identifier", "id")
    mapping = {}
    for pid, id in pids_ids:
        mapping.setdefault(pid, []).append(id)
    self.context["datasets_by_pid"] = mapping

DatasetSerializer#

Bases: CommonNestedModelSerializer, SerializerCacheSerializer

Source code in src/apps/core/serializers/dataset_serializer.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
class DatasetSerializer(CommonNestedModelSerializer, SerializerCacheSerializer):
    metadata_repository = ConstantField(value="Fairdata")
    access_rights = AccessRightsModelSerializer(required=False, allow_null=True, many=False)
    field_of_science = FieldOfScience.get_serializer_field(required=False, many=True)
    infrastructure = ResearchInfra.get_serializer_field(required=False, many=True)
    actors = DatasetActorSerializer(required=False, many=True)
    fileset = FileSetSerializer(required=False, source="file_set", allow_null=True)
    remote_resources = RemoteResourceSerializer(many=True, required=False)
    language = Language.get_serializer_field(required=False, many=True)
    metadata_owner = MetadataProviderModelSerializer(required=False)
    other_identifiers = OtherIdentifierModelSerializer(required=False, many=True)
    theme = Theme.get_serializer_field(required=False, many=True)
    spatial = SpatialModelSerializer(required=False, many=True, lazy=True)
    temporal = TemporalModelSerializer(required=False, many=True, lazy=True)
    relation = EntityRelationSerializer(required=False, many=True)
    preservation = PreservationModelSerializer(required=False, many=False)
    provenance = ProvenanceModelSerializer(required=False, many=True, lazy=True)
    projects = ProjectModelSerializer(required=False, many=True)
    dataset_versions = serializers.SerializerMethodField()
    allowed_actions = DatasetAllowedActionsSerializer(read_only=True, source="*")
    created = serializers.DateTimeField(required=False, read_only=False)
    modified = serializers.DateTimeField(required=False, read_only=False)
    next_draft = LinkedDraftSerializer(read_only=True)
    draft_of = LinkedDraftSerializer(read_only=True)
    version = serializers.IntegerField(source="version_number", read_only=True)
    metrics = DatasetMetricsSerializer(read_only=True)  # Included when include_metrics=true
    pid_type = NoopField(help_text="No longer in use. Replaced by generate_pid_on_publish.")
    generate_pid_on_publish = ListValidChoicesField(
        choices=GeneratedPIDType.choices, required=False, allow_null=True
    )
    pid_generated_by_fairdata = serializers.BooleanField(read_only=True)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.versions_serializer = VersionSerializer(
            many=True
        )  # Avoid initing VersionSerializer multiple times

    def can_view_drafts(self, instance) -> bool:
        """Return true if user can see drafts related to an instance they can see."""
        return (
            instance.state == Dataset.StateChoices.DRAFT
            or instance.has_permission_to_see_drafts(self.context["request"].user)
        )

    def get_version(self, instance):
        return instance.version_number

    def get_dataset_versions(self, instance):
        if version_set := instance.dataset_versions:
            # Use prefetched results stored in _datasets when available
            versions = getattr(version_set, "_datasets", None) or version_set.datasets(
                manager="all_objects"
            ).order_by("-created").prefetch_related(*Dataset.dataset_versions_prefetch_fields)

            has_drafts = any(
                dataset for dataset in versions if dataset.state != Dataset.StateChoices.PUBLISHED
            )
            if has_drafts and not self.can_view_drafts(instance):
                versions = [
                    dataset
                    for dataset in versions
                    if dataset.state == Dataset.StateChoices.PUBLISHED
                ]

            self.versions_serializer._context = self.context
            return self.versions_serializer.to_representation(versions)

    # Fields that should be left unchanged when omitted from PUT
    no_put_default_fields = {
        "id",
        "state",
        "metadata_owner",
        "cumulative_state",
    }

    def get_fields(self):
        fields = super().get_fields()
        if not self.context["view"].query_params.get("include_allowed_actions"):
            fields.pop("allowed_actions", None)
        if not self.context["view"].query_params.get("include_metrics"):
            fields.pop("metrics", None)
        return fields

    def save(self, **kwargs):
        if self.instance:
            if (
                "state" in self._validated_data
                and self._validated_data["state"] != self.instance.state
            ):
                raise serializers.ValidationError(
                    {"state": "Value cannot be changed directly for an existing dataset."}
                )

        # If missing, assign metadata owner with metadata_owner.save()
        if not (self.instance and self.instance.metadata_owner):
            # Nested serializer is not called with None,
            # so use {} as "empty" value.
            if not self._validated_data.get("metadata_owner"):
                self._validated_data["metadata_owner"] = {}
        return super().save(**kwargs)

    def omit_pid_fields(self, instance: Dataset, ret: dict):
        if instance.persistent_identifier:
            ret.pop("generate_pid_on_publish", None)
        else:
            ret.pop("pid_generated_by_fairdata", None)

    def to_representation(self, instance: Dataset):
        instance.ensure_prefetch()
        request = self.context["request"]
        ret = super().to_representation(instance)

        # Drafts should be hidden from users without access to them
        if not self.can_view_drafts(instance):
            ret.pop("draft_revision", None)
            ret.pop("next_draft", None)
            ret.pop("draft_of", None)

        has_emails = self.context.pop("has_emails", False)

        # Save to serializer cache
        if cache := self.cache:
            if value_context := cache.get_value_context(instance):
                has_emails = has_emails or value_context.get("has_emails", False)
            cache.set_value(
                instance, ret, value_context={"has_emails": has_emails}, only_if_modified=True
            )

        view = self.context["view"]
        if view.query_params.get("expand_catalog"):
            ret["data_catalog"] = DataCatalogModelSerializer(
                instance.data_catalog, context={"request": request}
            ).data

        if fields := view.query_params.get("fields"):
            not_found = [field for field in fields if field not in self.fields]
            if len(not_found):
                raise serializers.ValidationError(
                    {"fields": f"Fields not found in dataset: {','.join(not_found)}"}
                )
            ret = {k: v for k, v in ret.items() if k in fields}

        self.omit_pid_fields(instance, ret)

        if has_emails:
            # Handle email values. Copies dicts and lists to avoid accidentally modifying
            # data that has not yet been committed to cache.
            handle_private_emails(
                ret,
                show_emails=instance.has_permission_to_edit(request.user),
                ignore_fields={  # These fields should not contain PrivateEmailValue objects
                    "access_rights",
                    "api_version",
                    "created",
                    "cumulative_state",
                    "data_catalog",
                    "infrastructure",
                    "dataset_versions",
                    "description",
                    "field_of_science",
                    "fileset",
                    "id",
                    "issued",
                    "keyword",
                    "language",
                    "metadata_owner",
                    "metadata_repository",
                    "modified",
                    "other_identifiers",
                    "persistent_identifier",
                    "published_revision",
                    "relation",
                    "remote_resources",
                    "spatial",
                    "state",
                    "temporal",
                    "theme",
                    "title",
                    "version",
                },
            )
        return ret

    class Meta:
        model = Dataset
        read_only_fields = (
            "created",
            "cumulation_started",
            "cumulation_ended",
            "deprecated",
            "removed",
            "modified",
            "dataset_versions",
            "published_revision",
            "draft_revision",
            "allowed_actions",
            "draft_of",
            "next_draft",
            "version",
            "api_version",
            "metadata_repository",
            "metrics",
        )
        fields = (
            "id",  # read only
            "access_rights",
            "actors",
            "bibliographic_citation",
            "cumulative_state",
            "data_catalog",
            "description",
            "field_of_science",
            "fileset",
            "generate_pid_on_publish",
            "infrastructure",
            "issued",
            "keyword",
            "language",
            "metadata_owner",
            "other_identifiers",
            "persistent_identifier",
            "pid_generated_by_fairdata",  # read only
            "pid_type",  # deprecated
            "preservation",
            "projects",
            "provenance",
            "relation",
            "remote_resources",
            "spatial",
            "state",
            "temporal",
            "theme",
            "title",
            *read_only_fields,
        )
        list_serializer_class = DatasetListSerializer

    def _dc_is_harvested(self, data):
        if self.context["request"].method == "POST":
            if data.get("data_catalog") != None:
                return DataCatalog.objects.get(id=data["data_catalog"]).harvested
        elif self.context["request"].method in {"PUT", "PATCH"}:
            if self.instance and self.instance.data_catalog:
                return self.instance.data_catalog.harvested
            elif data.get("data_catalog") != None:
                return DataCatalog.objects.get(id=data["data_catalog"]).harvested
        return None

    def _ds_is_published(self, data):
        state = None
        if data.get("state"):
            state = data["state"]
        elif self.instance:
            state = self.instance.state
        if state == "published":
            return True
        return False

    def _validate_timestamps(self, data, errors):
        _now = timezone.now()

        if "modified" in data and data["modified"] > _now:
            errors["modified"] = "Timestamp cannot be in the future"
        if "created" in data and data["created"] > _now:
            errors["created"] = "Timestamp cannot be in the future"

        if self.context["request"].method == "POST":
            if data["modified"] < data["created"]:
                errors["timestamps"] = "Date modified earlier than date created"

        elif self.context["request"].method in {"PUT", "PATCH"}:
            data["created"] = self.instance.created
            if "modified" in data and data["modified"] < data["created"].replace(microsecond=0):
                errors["timestamps"] = "Date modified earlier than date created"
        return errors

    def _validate_data(self, data, errors):
        """Check data constraints."""
        existing_fileset = None
        existing_remote_resources = None
        if self.instance:
            existing_fileset = getattr(self.instance, "file_set", None)
            existing_remote_resources = self.instance.remote_resources.all()

        fileset = data.get("file_set", existing_fileset)
        remote_resources = data.get("remote_resources", existing_remote_resources)
        if fileset and remote_resources:
            errors[api_settings.NON_FIELD_ERRORS_KEY] = (
                "Cannot have files and remote resources in the same dataset."
            )
        return errors

    @classmethod
    def validate_new_catalog(
        cls, dataset: Optional[Dataset], new_data_catalog: Optional[DataCatalog] = None
    ):
        old_catalog = None
        if dataset:  # Omit dataset to indicate creating new dataset
            old_catalog = dataset.data_catalog

        if old_catalog and old_catalog != new_data_catalog:
            raise serializers.ValidationError({"data_catalog": "Cannot change data catalog."})

    @classmethod
    def validate_new_pid(
        cls,
        dataset: Optional["Dataset"],
        new_pid: Optional[str] = None,
        new_data_catalog: Optional[DataCatalog] = None,
    ):
        """Validate that user-provided persistent_identifier is allowed for dataset."""
        msg = None
        data_catalog = new_data_catalog
        old_pid = None
        if dataset:  # Omit dataset to indicate creating new dataset
            old_pid = dataset.persistent_identifier
            data_catalog = data_catalog or dataset.data_catalog

        if new_pid == old_pid:
            return  # No change, no need to validate new value

        if data_catalog and not data_catalog.allow_external_pid:
            msg = "Assigning user-defined PID is not supported by the catalog."

        if dataset:
            if dataset.pid_generated_by_fairdata:
                msg = "Changing generated PID is not allowed."
            elif dataset.generate_pid_on_publish:
                msg = "Cannot assign user-defined PID when using generate_pid_on_publish."

        # PIDs starting wih draft: are reserved for "draft_of" draft datasets
        if new_pid and new_pid.startswith("draft:"):
            msg = "Cannot assign draft PID."

        if msg:
            raise serializers.ValidationError({"persistent_identifier": msg})

    def to_internal_value(self, data):
        if self.instance:  # dataset actors need dataset in context
            self.context["dataset"] = self.instance
        else:
            self.context["dataset"] = None
        _data = super().to_internal_value(data)

        errors = {}
        errors = self._validate_timestamps(_data, errors)
        errors = self._validate_data(_data, errors)

        if errors:
            raise serializers.ValidationError(errors)

        # Assign API version
        _data["api_version"] = 3
        return _data

    def update(self, instance: Dataset, validated_data):
        instance._updating = True
        validated_data["last_modified_by"] = self.context["request"].user

        if "data_catalog" in validated_data:
            self.validate_new_catalog(
                dataset=instance,
                new_data_catalog=validated_data["data_catalog"],
            )

        if "persistent_identifier" in validated_data:
            self.validate_new_pid(
                dataset=instance,
                new_pid=validated_data["persistent_identifier"],
                new_data_catalog=validated_data.get("data_catalog"),
            )

        # Ensure modification timestamp gets set on PATCH which does not use model defaults
        if "modified" not in validated_data:
            validated_data["modified"] = timezone.now()

        dataset: Dataset = super().update(instance, validated_data=validated_data)
        instance._updating = False
        return dataset

    def create(self, validated_data):
        validated_data["last_modified_by"] = self.context["request"].user
        self.validate_new_catalog(
            dataset=None,
            new_data_catalog=validated_data.get("data_catalog"),
        )
        self.validate_new_pid(
            dataset=None,
            new_pid=validated_data.get("persistent_identifier"),
            new_data_catalog=validated_data.get("data_catalog"),
        )

        # Always initialize dataset as draft. This allows assigning
        # reverse and many-to-many relations to the newly created
        # dataset before it is actually published.
        state = validated_data.pop("state", None)
        instance: Dataset
        if state == Dataset.StateChoices.PUBLISHED:
            with skip_index_update():  # Don't add draft to search index if publish fails
                instance = super().create(validated_data=validated_data)
            # Now reverse and many-to-many relations have been assigned, try to publish
            instance.publish()
        else:
            instance = super().create(validated_data=validated_data)
        return instance

can_view_drafts(instance) #

Return true if user can see drafts related to an instance they can see.

Source code in src/apps/core/serializers/dataset_serializer.py
def can_view_drafts(self, instance) -> bool:
    """Return true if user can see drafts related to an instance they can see."""
    return (
        instance.state == Dataset.StateChoices.DRAFT
        or instance.has_permission_to_see_drafts(self.context["request"].user)
    )

validate_new_pid(dataset, new_pid=None, new_data_catalog=None) classmethod #

Validate that user-provided persistent_identifier is allowed for dataset.

Source code in src/apps/core/serializers/dataset_serializer.py
@classmethod
def validate_new_pid(
    cls,
    dataset: Optional["Dataset"],
    new_pid: Optional[str] = None,
    new_data_catalog: Optional[DataCatalog] = None,
):
    """Validate that user-provided persistent_identifier is allowed for dataset."""
    msg = None
    data_catalog = new_data_catalog
    old_pid = None
    if dataset:  # Omit dataset to indicate creating new dataset
        old_pid = dataset.persistent_identifier
        data_catalog = data_catalog or dataset.data_catalog

    if new_pid == old_pid:
        return  # No change, no need to validate new value

    if data_catalog and not data_catalog.allow_external_pid:
        msg = "Assigning user-defined PID is not supported by the catalog."

    if dataset:
        if dataset.pid_generated_by_fairdata:
            msg = "Changing generated PID is not allowed."
        elif dataset.generate_pid_on_publish:
            msg = "Cannot assign user-defined PID when using generate_pid_on_publish."

    # PIDs starting wih draft: are reserved for "draft_of" draft datasets
    if new_pid and new_pid.startswith("draft:"):
        msg = "Cannot assign draft PID."

    if msg:
        raise serializers.ValidationError({"persistent_identifier": msg})

DatasetRevisionsQueryParamsSerializer#

Bases: Serializer

Source code in src/apps/core/serializers/dataset_serializer.py
class DatasetRevisionsQueryParamsSerializer(serializers.Serializer):
    latest_published = serializers.BooleanField(
        help_text=("Get latest published revision."), required=False
    )
    published_revision = serializers.IntegerField(
        help_text=("Get specific published revision."),
        required=False,
    )
    all_published_revisions = serializers.BooleanField(
        help_text=("Get all published revision. "),
        required=False,
    )

    class Meta:
        validators = [
            OneOf(
                ["latest_published", "published_revision", "all_published_versions"],
                required=False,
                count_all_falsy=True,
            )
        ]

ExpandCatalogQueryParamsSerializer#

Bases: Serializer

Source code in src/apps/core/serializers/dataset_serializer.py
class ExpandCatalogQueryParamsSerializer(serializers.Serializer):
    expand_catalog = serializers.BooleanField(
        default=False, help_text=_("Include expanded data catalog in response.")
    )

LatestVersionQueryParamsSerializer#

Bases: Serializer

Source code in src/apps/core/serializers/dataset_serializer.py
class LatestVersionQueryParamsSerializer(serializers.Serializer):
    latest_versions = serializers.BooleanField(
        default=False,
        help_text=_("Return only latest datasets versions available for the requesting user."),
    )