| 
 | 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 marshmallow import Schema, fields, ValidationError, validate, validates, \  | 
 | 30 | +    validates_schema  | 
 | 31 | + | 
 | 32 | +from invenio_files_rest.models import ObjectVersion  | 
 | 33 | + | 
 | 34 | +DATE_REGEX = r'\d{4}-\d{2}-\d{2}'  | 
 | 35 | +DATE_ERROR = 'The date should follow the pattern YYYY-mm-dd.'  | 
 | 36 | + | 
 | 37 | +UPLOAD_TYPES = [  | 
 | 38 | +    'publication',  | 
 | 39 | +    'poster',  | 
 | 40 | +    'presentation',  | 
 | 41 | +    'dataset',  | 
 | 42 | +    'image',  | 
 | 43 | +    'video',  | 
 | 44 | +    'software',  | 
 | 45 | +    'lesson',  | 
 | 46 | +    'physicalobject',  | 
 | 47 | +    'other'  | 
 | 48 | +]  | 
 | 49 | +LICENSES = [  | 
 | 50 | +    'CC-BY-4.0',  | 
 | 51 | +    'CC-BY-1.0',  | 
 | 52 | +    'CC-BY-2.0',  | 
 | 53 | +    'CC-BY-3.0'  | 
 | 54 | +]  | 
 | 55 | +ACCESS_RIGHTS = [  | 
 | 56 | +    'open',  | 
 | 57 | +    'embargoed',  | 
 | 58 | +    'restricted',  | 
 | 59 | +    'closed'  | 
 | 60 | +]  | 
 | 61 | + | 
 | 62 | + | 
 | 63 | +class ZenodoCreatorsSchema(Schema):  | 
 | 64 | +    name = fields.String(required=True)  | 
 | 65 | +    affiliation = fields.String()  | 
 | 66 | +    orcid = fields.String()  | 
 | 67 | + | 
 | 68 | + | 
 | 69 | +class ZenodoDepositMetadataSchema(Schema):  | 
 | 70 | +    title = fields.String(required=True)  | 
 | 71 | +    description = fields.String(required=True)  | 
 | 72 | +    version = fields.String()  | 
 | 73 | + | 
 | 74 | +    keywords = fields.List(fields.String())  | 
 | 75 | +    creators = fields.List(  | 
 | 76 | +        fields.Nested(ZenodoCreatorsSchema), required=True)  | 
 | 77 | + | 
 | 78 | +    upload_type = fields.String(  | 
 | 79 | +        required=True, validate=validate.OneOf(UPLOAD_TYPES))  | 
 | 80 | +    license = fields.String(  | 
 | 81 | +        required=True, validate=validate.OneOf(LICENSES))  | 
 | 82 | +    access_right = fields.String(  | 
 | 83 | +        required=True, validate=validate.OneOf(ACCESS_RIGHTS))  | 
 | 84 | + | 
 | 85 | +    publication_date = fields.String(  | 
 | 86 | +        required=True, validate=validate.Regexp(DATE_REGEX, error=DATE_ERROR))  | 
 | 87 | +    embargo_date = fields.String(  | 
 | 88 | +        validate=validate.Regexp(DATE_REGEX, error=DATE_ERROR))  | 
 | 89 | +    access_conditions = fields.String()  | 
 | 90 | + | 
 | 91 | +    @validates('embargo_date')  | 
 | 92 | +    def validate_embargo_date(self, value):  | 
 | 93 | +        """Validate that embargo date is in the future."""  | 
 | 94 | +        if arrow.get(value).date() <= arrow.utcnow().date():  | 
 | 95 | +            raise ValidationError(  | 
 | 96 | +                'Embargo date must be in the future.',  | 
 | 97 | +                field_names=['embargo_date']  | 
 | 98 | +            )  | 
 | 99 | + | 
 | 100 | +    @validates_schema()  | 
 | 101 | +    def validate_license(self, data, **kwargs):  | 
 | 102 | +        """Validate license."""  | 
 | 103 | +        access = data.get('access_right')  | 
 | 104 | +        if access in ['open', 'embargoed'] and 'license' not in data:  | 
 | 105 | +            raise ValidationError(  | 
 | 106 | +                'Required when access right is open or embargoed.',  | 
 | 107 | +                field_names=['license']  | 
 | 108 | +            )  | 
 | 109 | +        if access == 'embargoed' and 'embargo_date' not in data:  | 
 | 110 | +            raise ValidationError(  | 
 | 111 | +                'Required when access right is embargoed.',  | 
 | 112 | +                field_names=['embargo_date']  | 
 | 113 | +            )  | 
 | 114 | +        if access == 'restricted' and 'access_conditions' not in data:  | 
 | 115 | +            raise ValidationError(  | 
 | 116 | +                'Required when access right is restricted.',  | 
 | 117 | +                field_names=['access_conditions']  | 
 | 118 | +            )  | 
 | 119 | + | 
 | 120 | + | 
 | 121 | +class ZenodoUploadSchema(Schema):  | 
 | 122 | +    files = fields.List(fields.String(), required=True)  | 
 | 123 | +    data = fields.Nested(ZenodoDepositMetadataSchema, default=dict())  | 
 | 124 | +    bucket = fields.String(required=True)  | 
 | 125 | + | 
 | 126 | +    @validates_schema()  | 
 | 127 | +    def validate_files(self, data, **kwargs):  | 
 | 128 | +        bucket = data['bucket']  | 
 | 129 | +        files = data['files']  | 
 | 130 | + | 
 | 131 | +        for _file in files:  | 
 | 132 | +            obj = ObjectVersion.get(bucket, _file)  | 
 | 133 | +            if not obj:  | 
 | 134 | +                raise ValidationError(  | 
 | 135 | +                    f'File {_file} not found in bucket.',  | 
 | 136 | +                    field_names=['files']  | 
 | 137 | +                )  | 
0 commit comments