-
Notifications
You must be signed in to change notification settings - Fork 357
[ENG-9827] - save collection-submission custom metadata to CedarMetadataRecord on create/update #11735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/es2-consolidation
Are you sure you want to change the base?
[ENG-9827] - save collection-submission custom metadata to CedarMetadataRecord on create/update #11735
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -429,15 +429,16 @@ def create(self, validated_data): | |
| raise exceptions.ValidationError('"creator" must be specified.') | ||
| if not (creator.has_perm('write_collection', collection) or (hasattr(guid.referent, 'has_permission') and guid.referent.has_permission(creator, WRITE))): | ||
| raise exceptions.PermissionDenied('Must have write permission on either collection or collected object to collect.') | ||
| if waffle.switch_is_active(features.COLLECTION_SUBMISSION_WITH_CEDAR) and collection.provider_id: | ||
| try: | ||
| collection.provider.validate_required_metadata(guid.referent) | ||
| except ValidationError as e: | ||
| raise InvalidModelValueError(e.message) | ||
| try: | ||
| obj = collection.collect_object(guid.referent, creator, **validated_data) | ||
| except ValidationError as e: | ||
| raise InvalidModelValueError(e.message) | ||
| if waffle.switch_is_active(features.COLLECTION_SUBMISSION_WITH_CEDAR): | ||
| if collection.provider_id: | ||
| try: | ||
| collection.provider.validate_required_metadata(guid.referent) | ||
| except ValidationError as e: | ||
| raise InvalidModelValueError(e.message) | ||
| if subjects: | ||
| auth = get_user_auth(self.context['request']) | ||
| try: | ||
|
|
@@ -470,15 +471,16 @@ def create(self, validated_data): | |
| raise exceptions.ValidationError('"creator" must be specified.') | ||
| if not (creator.has_perm('write_collection', collection) or (hasattr(guid.referent, 'has_permission') and guid.referent.has_permission(creator, WRITE))): | ||
| raise exceptions.PermissionDenied('Must have write permission on either collection or collected object to collect.') | ||
| if waffle.switch_is_active(features.COLLECTION_SUBMISSION_WITH_CEDAR) and collection.provider_id: | ||
| try: | ||
| collection.provider.validate_required_metadata(guid.referent) | ||
| except ValidationError as e: | ||
| raise InvalidModelValueError(e.message) | ||
| try: | ||
| obj = collection.collect_object(guid.referent, creator, **validated_data) | ||
| except ValidationError as e: | ||
| raise InvalidModelValueError(e.message) | ||
| if waffle.switch_is_active(features.COLLECTION_SUBMISSION_WITH_CEDAR): | ||
| if collection.provider_id: | ||
| try: | ||
| collection.provider.validate_required_metadata(guid.referent) | ||
| except ValidationError as e: | ||
| raise InvalidModelValueError(e.message) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (on closer inspection, if moving this |
||
| if subjects: | ||
| auth = get_user_auth(self.context['request']) | ||
| try: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import json | ||
| import requests | ||
| from jsonschema import validate as jsonschema_validate, ValidationError as JsonSchemaValidationError | ||
|
|
||
| from django.apps import apps | ||
| from django.contrib.postgres import fields | ||
|
|
@@ -21,7 +20,6 @@ | |
| from .brand import Brand | ||
| from .citation import CitationStyle | ||
| from .licenses import NodeLicense | ||
| from .cedar_metadata import CedarMetadataRecord | ||
| from .storage import ProviderAssetFile | ||
| from .subject import Subject | ||
| from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField | ||
|
|
@@ -167,6 +165,24 @@ def update_or_create_from_json(cls, provider_data, user): | |
| related_name='required_by_providers', | ||
| ) | ||
|
|
||
| def validate_required_metadata(self, obj): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was there a reason to move this method? there's at least a rough convention that regular methods go below dunder-methods (like |
||
| """ | ||
| Raises ValidationError if obj does not have a published CedarMetadataRecord for | ||
| this provider's required_metadata_template. | ||
| Does nothing when required_metadata_template is not set. | ||
| """ | ||
| if not self.required_metadata_template_id: | ||
| return | ||
| guid = obj.guids.first() | ||
| if guid is None or not guid.cedar_metadata_records.filter( | ||
| template_id=self.required_metadata_template_id, | ||
| is_published=True, | ||
| ).exists(): | ||
| raise ValidationError( | ||
| f'Submitted object must have a published CEDAR metadata record for template ' | ||
| f'"{self.required_metadata_template.schema_name}" to be submitted to this collection.' | ||
| ) | ||
|
|
||
| def __repr__(self): | ||
| return ('(name={self.name!r}, default_license={self.default_license!r}, ' | ||
| 'allow_submissions={self.allow_submissions!r}) with id {self.id!r}').format(self=self) | ||
|
|
@@ -259,27 +275,6 @@ def setup_share_source(self, provider_home_page): | |
|
|
||
| self.save() | ||
|
|
||
| def validate_required_metadata(self, osf_obj): | ||
| if not self.required_metadata_template: | ||
| return | ||
|
|
||
| record = CedarMetadataRecord.objects.filter( | ||
| guid__in=osf_obj.guids.all(), | ||
| template=self.required_metadata_template, | ||
| is_published=True, | ||
| ).first() | ||
|
|
||
| if record is None: | ||
| raise ValidationError( | ||
| f'Object must have a published CEDAR metadata record for the required template ' | ||
| f'"{self.required_metadata_template.schema_name}".' | ||
| ) | ||
|
|
||
| try: | ||
| jsonschema_validate(record.metadata, self.required_metadata_template.template) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, you removed the validation -- this change would make sense if even if the frontend is responsible for building the record, backend should still validate (and it's a feature of cedar that it's so easy to validate a record against a given template, because jsonschema) |
||
| except JsonSchemaValidationError as e: | ||
| raise ValidationError(e.message) | ||
|
|
||
|
|
||
| class CollectionProvider(AbstractProvider): | ||
| DEFAULT_SUBSCRIPTIONS = [ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.