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+ from marshmallow import Schema , fields , ValidationError , \
27+ validate , validates , validates_schema
28+ from pprint import pprint
29+ import arrow
30+
31+ UPLOAD_TYPES = [
32+ 'publication' ,
33+ 'poster' ,
34+ 'presentation' ,
35+ 'dataset' ,
36+ 'image' ,
37+ 'video' ,
38+ 'software' ,
39+ 'lesson' ,
40+ 'physicalobject' ,
41+ 'other'
42+ ]
43+ LICENSES = [
44+ 'CC-BY-4.0' ,
45+ 'CC-BY-1.0' ,
46+ 'CC-BY-2.0' ,
47+ 'CC-BY-3.0'
48+ ]
49+ ACCESS_RIGHTS = [
50+ 'open' ,
51+ '' ,
52+ '' ,
53+ 'closed'
54+ ]
55+
56+
57+ class ZenodoCreatorsSchema (Schema ):
58+ name = fields .String (required = True )
59+ affiliation = fields .String ()
60+ orcid = fields .String ()
61+
62+
63+ class ZenodoDepositMetadataSchema (Schema ):
64+ title = fields .String (required = True )
65+ description = fields .String (required = True )
66+ publication_date = fields .String (required = True )
67+ version = fields .String ()
68+
69+ keywords = fields .List (fields .String ())
70+ creators = fields .List (fields .Nested (ZenodoCreatorsSchema ), required = True )
71+
72+ upload_type = fields .String (required = True ,
73+ validate = validate .OneOf (UPLOAD_TYPES ))
74+ license = fields .String (required = True ,
75+ validate = validate .OneOf (LICENSES ))
76+ access_right = fields .String (required = True ,
77+ validate = validate .OneOf (ACCESS_RIGHTS ))
78+
79+ @validates ('publication_date' )
80+ def validate_date (self , value ):
81+ return True
82+
83+ @validates ('access_right' )
84+ def validate_access (self , value ):
85+ return True
86+
87+ @validates ('embargo_date' )
88+ def validate_embargo_date (self , value ):
89+ """Validate that embargo date is in the future."""
90+ if arrow .get (value ).date () <= arrow .utcnow ().date ():
91+ raise ValidationError (
92+ 'Embargo date must be in the future.' ,
93+ field_names = ['embargo_date' ]
94+ )
95+
96+ @validates_schema ()
97+ def validate_license (self , data ):
98+ """Validate license."""
99+ # acc = data.get('access_right')
100+ # if acc in [AccessRight.OPEN, AccessRight.EMBARGOED] and \
101+ # 'license' not in data:
102+ # raise ValidationError(
103+ # _('Required when access right is open or embargoed.'),
104+ # field_names=['license']
105+ # )
106+ # if acc == AccessRight.EMBARGOED and 'embargo_date' not in data:
107+ # raise ValidationError(
108+ # _('Required when access right is embargoed.'),
109+ # field_names=['embargo_date']
110+ # )
111+ # if acc == AccessRight.RESTRICTED and 'access_conditions' not in data:
112+ # raise ValidationError(
113+ # _('Required when access right is restricted.'),
114+ # field_names=['access_conditions']
115+ # )
116+
117+
118+ class ZenodoUploadSchema (Schema ):
119+ files = fields .List (fields .String , required = True )
120+ data = fields .Nested (ZenodoDepositMetadataSchema , default = dict ())
121+ bucket = fields .String (required = True )
122+
123+ @validates ('files' )
124+ def validate (self , value ):
125+ for _file in value :
126+ if _file not in ['b' , 'c' ]:
127+ raise ValidationError ('FILE ERROR' )
128+
129+
130+ xx = {
131+ 'data' : {
132+ 'title' : 'My first upload yoohoo' ,
133+ 'upload_type' : 'poster' ,
134+ 'description' : 'This is my first upload' ,
135+ 'creators' : [
136+ {'name' : 'Ilias KoKoKo' , 'affiliation' : 'Zenodo CAP' }
137+ ],
138+ 'access_right' : 'lolo'
139+ },
140+ 'files' : ['a' ],
141+ 'bucket' : 'bucket'
142+ }
143+
144+ try :
145+ lol = ZenodoUploadSchema ().load (xx )
146+ pprint (lol )
147+ except ValidationError as err :
148+ pprint (err .messages )
0 commit comments