|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# This file is part of CERN Analysis Preservation Framework. |
| 4 | +# Copyright (C) 2020 CERN. |
| 5 | +# |
| 6 | +# CERN Analysis Preservation Framework is free software; you can redistribute |
| 7 | +# it and/or modify it under the terms of the GNU General Public License as |
| 8 | +# published by the Free Software Foundation; either version 2 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# CERN Analysis Preservation Framework is distributed in the hope that it will |
| 12 | +# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with CERN Analysis Preservation Framework; if not, write to the |
| 18 | +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | +# MA 02111-1307, USA. |
| 20 | +# |
| 21 | +# In applying this license, CERN does not |
| 22 | +# waive the privileges and immunities granted to it by virtue of its status |
| 23 | +# as an Intergovernmental Organization or submit itself to any jurisdiction. |
| 24 | +# or submit itself to any jurisdiction. |
| 25 | + |
| 26 | +"""Zenodo Serializer/Validator.""" |
| 27 | + |
| 28 | +import arrow |
| 29 | +from flask_login import current_user |
| 30 | +from marshmallow import Schema, fields, ValidationError, validate, validates, \ |
| 31 | + validates_schema |
| 32 | + |
| 33 | +from invenio_files_rest.models import ObjectVersion |
| 34 | + |
| 35 | +DATE_REGEX = r'\d{4}-\d{2}-\d{2}' |
| 36 | +DATE_ERROR = 'The date should follow the pattern YYYY-mm-dd.' |
| 37 | + |
| 38 | +UPLOAD_TYPES = [ |
| 39 | + 'publication', |
| 40 | + 'poster', |
| 41 | + 'presentation', |
| 42 | + 'dataset', |
| 43 | + 'image', |
| 44 | + 'video', |
| 45 | + 'software', |
| 46 | + 'lesson', |
| 47 | + 'physicalobject', |
| 48 | + 'other' |
| 49 | +] |
| 50 | +LICENSES = [ |
| 51 | + 'CC-BY-4.0', |
| 52 | + 'CC-BY-1.0', |
| 53 | + 'CC-BY-2.0', |
| 54 | + 'CC-BY-3.0' |
| 55 | +] |
| 56 | +ACCESS_RIGHTS = [ |
| 57 | + 'open', |
| 58 | + 'embargoed', |
| 59 | + 'restricted', |
| 60 | + 'closed' |
| 61 | +] |
| 62 | + |
| 63 | + |
| 64 | +def choice_error_msg(choices): |
| 65 | + return f'Not a valid choice. Select one of: {choices}' |
| 66 | + |
| 67 | + |
| 68 | +class ZenodoCreatorsSchema(Schema): |
| 69 | + name = fields.String(required=True) |
| 70 | + affiliation = fields.String() |
| 71 | + orcid = fields.String() |
| 72 | + |
| 73 | + |
| 74 | +class ZenodoDepositMetadataSchema(Schema): |
| 75 | + title = fields.String(required=True) |
| 76 | + description = fields.String(required=True) |
| 77 | + version = fields.String() |
| 78 | + |
| 79 | + keywords = fields.List(fields.String()) |
| 80 | + creators = fields.List( |
| 81 | + fields.Nested(ZenodoCreatorsSchema), required=True) |
| 82 | + |
| 83 | + upload_type = fields.String(required=True, validate=validate.OneOf( |
| 84 | + UPLOAD_TYPES, error=choice_error_msg(UPLOAD_TYPES))) |
| 85 | + license = fields.String(required=True, validate=validate.OneOf( |
| 86 | + LICENSES, error=choice_error_msg(LICENSES))) |
| 87 | + access_right = fields.String(required=True, validate=validate.OneOf( |
| 88 | + ACCESS_RIGHTS, error=choice_error_msg(ACCESS_RIGHTS))) |
| 89 | + |
| 90 | + publication_date = fields.String( |
| 91 | + required=True, validate=validate.Regexp(DATE_REGEX, error=DATE_ERROR)) |
| 92 | + embargo_date = fields.String( |
| 93 | + validate=validate.Regexp(DATE_REGEX, error=DATE_ERROR)) |
| 94 | + access_conditions = fields.String() |
| 95 | + |
| 96 | + @validates('embargo_date') |
| 97 | + def validate_embargo_date(self, value): |
| 98 | + """Validate that embargo date is in the future.""" |
| 99 | + if arrow.get(value).date() <= arrow.utcnow().date(): |
| 100 | + raise ValidationError( |
| 101 | + 'Embargo date must be in the future.', |
| 102 | + field_names=['embargo_date'] |
| 103 | + ) |
| 104 | + |
| 105 | + @validates_schema() |
| 106 | + def validate_license(self, data, **kwargs): |
| 107 | + """Validate license.""" |
| 108 | + access = data.get('access_right') |
| 109 | + if access in ['open', 'embargoed'] and 'license' not in data: |
| 110 | + raise ValidationError( |
| 111 | + 'Required when access right is open or embargoed.', |
| 112 | + field_names=['license'] |
| 113 | + ) |
| 114 | + if access == 'embargoed' and 'embargo_date' not in data: |
| 115 | + raise ValidationError( |
| 116 | + 'Required when access right is embargoed.', |
| 117 | + field_names=['embargo_date'] |
| 118 | + ) |
| 119 | + if access == 'restricted' and 'access_conditions' not in data: |
| 120 | + raise ValidationError( |
| 121 | + 'Required when access right is restricted.', |
| 122 | + field_names=['access_conditions'] |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +class ZenodoUploadSchema(Schema): |
| 127 | + files = fields.List(fields.String(), required=True) |
| 128 | + data = fields.Nested(ZenodoDepositMetadataSchema, default=dict()) |
| 129 | + bucket = fields.String(required=True) |
| 130 | + |
| 131 | + @validates_schema() |
| 132 | + def validate_files(self, data, **kwargs): |
| 133 | + bucket = data['bucket'] |
| 134 | + files = data['files'] |
| 135 | + |
| 136 | + for _file in files: |
| 137 | + obj = ObjectVersion.get(bucket, _file) |
| 138 | + if not obj: |
| 139 | + raise ValidationError( |
| 140 | + f'File {_file} not found in bucket.', |
| 141 | + field_names=['files'] |
| 142 | + ) |
| 143 | + |
| 144 | + |
| 145 | +class ZenodoDepositSchema(Schema): |
| 146 | + id = fields.Int(dump_only=True) |
| 147 | + created = fields.String(dump_only=True) |
| 148 | + |
| 149 | + title = fields.Method('get_title', dump_only=True, allow_none=True) |
| 150 | + creator = fields.Method('get_creator', dump_only=True, allow_none=True) |
| 151 | + links = fields.Method('get_links', dump_only=True) |
| 152 | + |
| 153 | + def get_creator(self, data): |
| 154 | + return current_user.id if current_user else None |
| 155 | + |
| 156 | + def get_title(self, data): |
| 157 | + return data.get('metadata', {}).get('title') |
| 158 | + |
| 159 | + def get_links(self, data): |
| 160 | + return { |
| 161 | + 'self': data['links']['self'], |
| 162 | + 'bucket': data['links']['bucket'], |
| 163 | + 'html': data['links']['html'], |
| 164 | + 'publish': data['links']['publish'] |
| 165 | + } |
0 commit comments