Newer
Older
# @author: Gunnar Schaefer
import webapp2
import bson.json_util
import logging
log = logging.getLogger('nimsapi')
import nimsdata
import nimsapiutil
class Experiments(nimsapiutil.NIMSRequestHandler):
json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Experiment List',
'type': 'array',
'items': {
'title': 'Experiment',
'type': 'object',
'properties': {
'_id': {
'title': 'Database ID',
},
'site': {
'title': 'Site',
'type': 'string',
'group': {
'title': 'Group',
'name': {
'title': 'Name',
'timestamp': {
'title': 'Timestamp',
},
'permissions': {
'title': 'Permissions',
'type': 'object',
},
"""Return the number of Experiments."""
self.response.write(self.app.db.experiments.count())
self.response.write('experiments post\n')
"""Return the list of Experiments."""
query = {'permissions.uid': self.uid} if not self.user_is_superuser else None
projection = {'group': 1, 'name': 1, 'timestamp': 1, 'notes': 1, 'permissions': {'$elemMatch': {'uid': self.uid}}}
experiments = list(self.app.db.experiments.find(query, projection))
for exp in experiments:
exp['site'] = self.app.config['site_id']
"""Update many Experiments."""
self.response.write('experiments put\n')
class Experiment(nimsapiutil.NIMSRequestHandler):
json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Experiment',
'type': 'object',
'properties': {
'_id': {
'title': 'Database ID',
},
'site': {
'title': 'Site',
'type': 'string',
},
'group': {
'title': 'Group',
'type': 'string',
},
'name': {
'title': 'Name',
'type': 'string',
'maxLength': 32,
},
'timestamp': {
'title': 'Timestamp',
},
'permissions': {
'title': 'Permissions',
'type': 'object',
'minProperties': 1,
},
'files': {
'title': 'Files',
'type': 'array',
'items': nimsapiutil.NIMSRequestHandler.file_schema,
'uniqueItems': True,
},
},
'required': ['_id', 'group', 'name'], #FIXME
"""Return one Experiment, conditionally with details."""
"""Update an existing Experiment."""
xid = bson.ObjectId(xid)
self.get_experiment(xid, 'read-write') # ensure permissions
updates = {'$set': {}, '$unset': {}}
for k, v in self.request.params.iteritems():
if k in ['notes']:
updates['$set'][k] = v # FIXME: do appropriate type conversion
self.app.db.experiments.update({'_id': xid}, updates)
"""Delete an Experiment."""
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class Sessions(nimsapiutil.NIMSRequestHandler):
"""/sessions """
json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Session List',
'type': 'array',
'items': {
'title': 'Session',
'type': 'object',
'properties': {
'_id': {
'title': 'Database ID',
},
'name': {
'title': 'Session',
'type': 'string',
},
'subject': {
'title': 'Subject',
'type': 'string',
},
'site': {
'title': 'Site',
'type': 'string',
},
}
}
}
def count(self):
"""Return the number of Sessions."""
self.response.write(self.app.db.sessions.count())
def post(self):
"""Create a new Session"""
self.response.write('sessions post\n')
def get(self, xid):
"""Return the list of Experiment Sessions."""
xid = bson.ObjectId(xid)
self.get_experiment(xid) # ensure permissions
query = {'experiment': xid}
return list(self.app.db.sessions.find(query, projection))
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def put(self):
"""Update many Sessions."""
self.response.write('sessions put\n')
class Session(nimsapiutil.NIMSRequestHandler):
"""/sessions/<sid> """
json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Session',
'type': 'object',
'properties': {
'_id': {
'title': 'Database ID',
},
'uid': {
'title': 'UID',
'type': 'string',
},
'experiment': {
'title': 'Experiment ID',
},
'site': {
'title': 'Site',
'type': 'string',
},
'files': {
'title': 'Files',
'type': 'array',
'items': nimsapiutil.NIMSRequestHandler.file_schema,
'uniqueItems': True,
},
},
'required': ['_id', 'experiment', 'uid', 'patient_id', 'subject'], #FIXME
}
def schema(self, *args, **kwargs):
import copy
json_schema = copy.deepcopy(self.json_schema)
json_schema['properties'].update(nimsdata.NIMSData.session_properties)
def get(self, sid):
"""Return one Session, conditionally with details."""
def put(self, sid):
"""Update an existing Session."""
sid = bson.ObjectId(sid)
self.get_session(sid, 'read-write') # ensure permissions
updates = {'$set': {}, '$unset': {}}
for k, v in self.request.params.iteritems():
if k in ['notes']:
updates['$set'][k] = v # FIXME: do appropriate type conversion
self.app.db.sessions.update({'_id': sid}, updates)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
def move(self, sid):
"""
Move a Session to another Experiment.
Usage:
/nimsapi/sessions/123/move?dest=456
"""
self.response.write('session %s move, %s\n' % (sid, self.request.params))
class Epochs(nimsapiutil.NIMSRequestHandler):
"""/nimsapi/epochs """
json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Epoch List',
'type': 'array',
'items': {
'title': 'Epoch',
'type': 'object',
'properties': {
'_id': {
'title': 'Database ID',
},
'name': {
'title': 'Epoch',
'type': 'string',
},
'description': {
'title': 'Description',
'type': 'string',
},
'datatype': {
'title': 'Datatype',
'type': 'string',
},
}
}
}
def count(self):
"""Return the number of Epochs."""
def post(self):
"""Create a new Epoch."""
self.response.write('epochs post\n')
def get(self, sid):
"""Return the list of Session Epochs."""
sid = bson.ObjectId(sid)
self.get_session(sid) # ensure permissions
query = {'session': sid}
projection = ['name', 'description', 'datatype', 'notes']
return list(self.app.db.epochs.find(query, projection))
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
def put(self):
"""Update many Epochs."""
self.response.write('epochs put\n')
class Epoch(nimsapiutil.NIMSRequestHandler):
"""/nimsapi/epochs/<eid> """
json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Epoch',
'type': 'object',
'properties': {
'_id': {
'title': 'Database ID',
},
'uid': {
'title': 'UID',
'type': 'string',
},
'session': {
'title': 'Session ID',
},
'files': {
'title': 'Files',
'type': 'array',
'items': nimsapiutil.NIMSRequestHandler.file_schema,
'uniqueItems': True,
},
},
'required': ['_id'], #FIXME
}
def schema(self, *args, **kwargs):
import copy
json_schema = copy.deepcopy(self.json_schema)
json_schema['properties'].update(nimsdata.nimsdicom.NIMSDicom.epoch_properties)
def get(self, eid):
"""Return one Epoch, conditionally with details."""
def put(self, eid):
"""Update an existing Epoch."""
eid = bson.ObjectId(eid)
self.get_epoch(eid, 'read-write') # ensure permissions
updates = {'$set': {}, '$unset': {}}
for k, v in self.request.params.iteritems():
if k in ['notes']:
updates['$set'][k] = v # FIXME: do appropriate type conversion
self.app.db.epochs.update({'_id': eid}, updates)
def delete(self, eid):
"""Delete an Epoch."""