forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeposit.py
126 lines (89 loc) · 3.24 KB
/
deposit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- coding: utf-8 -*-
"""Functions for deposit module."""
__copyright__ = 'Copyright (c) 2021, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import genquery
import folder
import meta
from util import *
__all__ = ['api_deposit_path',
'api_deposit_status',
'api_deposit_submit',
'api_deposit_clear']
DEPOSIT_GROUP = "deposit-pilot"
def determine_deposit_path(ctx):
"""Determine deposit path for a user."""
deposit_path = ""
coll = "/" + user.zone(ctx) + "/home/" + DEPOSIT_GROUP
iter = genquery.row_iterator(
"COLL_NAME",
"COLL_PARENT_NAME = '{}'".format(coll),
genquery.AS_LIST, ctx
)
for row in iter:
deposit_path = row[0]
if deposit_path == "":
datapackage_name = pathutil.basename(coll)
if len(datapackage_name) > 235:
datapackage_name = datapackage_name[0:235]
ret = msi.get_icat_time(ctx, '', 'unix')
timestamp = ret['arguments'][0].lstrip('0')
# Ensure vault target does not exist.
i = 0
target_base = coll + "/" + datapackage_name + "[" + timestamp + "]"
deposit_path = target_base
while collection.exists(ctx, deposit_path):
i += 1
deposit_path = target_base + "[" + str(i) + "]"
collection.create(ctx, deposit_path)
space, zone, group, subpath = pathutil.info(deposit_path)
return "{}/{}".format(group, subpath)
@api.make()
def api_deposit_path(ctx):
"""Get deposit collection.
:param ctx: Combined type of a callback and rei struct
:returns: Path to deposit collection
"""
return {"deposit_path": determine_deposit_path(ctx)}
@api.make()
def api_deposit_status(ctx):
"""Retrieve status of deposit.
:param ctx: Combined type of a callback and rei struct
:returns: Deposit status
"""
deposit_path = determine_deposit_path(ctx)
coll = "/{}/home/{}".format(user.zone(ctx), deposit_path)
meta_path = '{}/{}'.format(coll, constants.IIJSONMETADATA)
data = False
if not collection.empty(ctx, coll):
if collection.data_count(ctx, coll) == 1 and data_object.exists(ctx, meta_path):
# Only file is yoda-metadata.json.
data = False
else:
data = True
metadata = False
if data_object.exists(ctx, meta_path) and meta.is_json_metadata_valid(ctx, meta_path):
metadata = True
return {"data": data, "metadata": metadata}
@api.make()
def api_deposit_submit(ctx):
"""Submit deposit collection.
:param ctx: Combined type of a callback and rei struct
:returns: API status
"""
deposit_path = determine_deposit_path(ctx)
coll = "/{}/home/{}".format(user.zone(ctx), deposit_path)
return folder.set_status(ctx, coll, constants.research_package_state.SUBMITTED)
@api.make()
def api_deposit_clear(ctx):
"""Clear deposit collection.
:param ctx: Combined type of a callback and rei struct
:returns: API status
"""
deposit_path = determine_deposit_path(ctx)
coll = "/{}/home/{}".format(user.zone(ctx), deposit_path)
try:
collection.remove(ctx, coll)
except msi.Error:
return api.Error('internal', 'Something went wrong. Please try again')
return api.Result.ok()