girder-jsonforms 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,362 @@
1
+ import cherrypy
2
+ from girder.api import access
3
+ from girder.api.describe import Description, autoDescribeRoute
4
+ from girder.api.rest import (
5
+ Resource,
6
+ filtermodel,
7
+ setRawResponse,
8
+ setResponseHeader,
9
+ )
10
+ from girder.constants import AccessType, SortDir, TokenScope
11
+ from girder.exceptions import RestException
12
+ from girder.models.folder import Folder
13
+ from girder.utility import RequestBodyStream
14
+ from girder.utility.progress import noProgress
15
+
16
+ from ..models.form import Form as FormModel
17
+
18
+
19
+ class Form(Resource):
20
+ def __init__(self):
21
+ super(Form, self).__init__()
22
+ self.resourceName = "form"
23
+ self.route("GET", (), self.listForm)
24
+ self.route("GET", (":id",), self.getForm)
25
+ self.route("POST", (), self.createForm)
26
+ self.route("PUT", (":id",), self.updateForm)
27
+ self.route("DELETE", (":id",), self.deleteForm)
28
+ self.route("GET", (":id", "access"), self.getFromAccess)
29
+ self.route("PUT", (":id", "access"), self.updateFromAccess)
30
+ self.route("GET", (":id", "export"), self.exportForm)
31
+ self.route("POST", (":id", "import"), self.importForm)
32
+
33
+ @access.public
34
+ @autoDescribeRoute(
35
+ Description("List all forms")
36
+ .param(
37
+ "entryFileName",
38
+ "Pass to lookup a form by exact entry filename match.",
39
+ required=False,
40
+ dataType="string",
41
+ )
42
+ .param(
43
+ "level",
44
+ "The minimum access level to filter the forms by",
45
+ dataType="integer",
46
+ required=False,
47
+ default=AccessType.READ,
48
+ enum=[AccessType.NONE, AccessType.READ, AccessType.WRITE, AccessType.ADMIN],
49
+ )
50
+ .pagingParams(defaultSort="name", defaultSortDir=SortDir.ASCENDING)
51
+ )
52
+ @filtermodel(model="form", plugin="jsonforms")
53
+ def listForm(self, entryFileName, level, limit, offset, sort):
54
+ query = {}
55
+ if entryFileName is not None:
56
+ query["entryFileName"] = entryFileName
57
+
58
+ return FormModel().findWithPermissions(
59
+ query=query,
60
+ offset=offset,
61
+ limit=limit,
62
+ sort=sort,
63
+ user=self.getCurrentUser(),
64
+ level=level,
65
+ )
66
+
67
+ @access.user(scope=TokenScope.DATA_WRITE)
68
+ @autoDescribeRoute(
69
+ Description("Import form entries from a file")
70
+ .modelParam("id", "The ID of the form", model=FormModel, level=AccessType.WRITE)
71
+ .param(
72
+ "dryRun",
73
+ "Whether to perform import or return description of what would happen",
74
+ required=False,
75
+ dataType="boolean",
76
+ default=True,
77
+ )
78
+ .errorResponse("ID was invalid.")
79
+ .errorResponse("Invalid file format", 400)
80
+ .errorResponse("Write access was denied on the form.", 403)
81
+ )
82
+ def importForm(self, form, dryRun):
83
+ content_type = cherrypy.request.headers.get("Content-Type")
84
+ if content_type not in (
85
+ "application/csv",
86
+ "application/vnd.ms-excel",
87
+ ):
88
+ raise RestException("Invalid file format")
89
+ content_length = cherrypy.request.headers.get("Content-Length")
90
+ if content_length is None or not content_length.isdigit():
91
+ raise RestException("Content-Length header is required")
92
+ content_length = int(content_length)
93
+ if content_length < 1:
94
+ raise RestException("File is empty")
95
+ file_obj = RequestBodyStream(cherrypy.request.body)
96
+ file_type = "csv" if content_type == "application/csv" else "xlsx"
97
+ return FormModel().import_entries(form, file_obj, file_type, dry_run=dryRun)
98
+
99
+ @access.public(scope=TokenScope.DATA_READ, cookie=True)
100
+ @autoDescribeRoute(
101
+ Description("Export form entries as a table")
102
+ .modelParam("id", "The ID of the form", model=FormModel, level=AccessType.READ)
103
+ .param(
104
+ "exportFormat",
105
+ "The format to export the entries as",
106
+ required=False,
107
+ default="csv",
108
+ enum=["csv", "xlsx"],
109
+ )
110
+ .errorResponse("ID was invalid.")
111
+ .errorResponse("Read access was denied on the form.", 403)
112
+ )
113
+ def exportForm(self, form, exportFormat):
114
+ export = FormModel().export(form, exportFormat)
115
+ setResponseHeader("Content-Length", export.getbuffer().nbytes)
116
+ content_type = (
117
+ "text/csv" if exportFormat == "csv" else "application/vnd.ms-excel"
118
+ )
119
+ setResponseHeader("Content-Type", content_type)
120
+ setResponseHeader(
121
+ "Content-Disposition",
122
+ f"attachment; filename={form['name']}." + exportFormat,
123
+ )
124
+ setRawResponse()
125
+ return export.getvalue()
126
+
127
+ @access.public
128
+ @autoDescribeRoute(
129
+ Description("Get a form by ID").modelParam(
130
+ "id", "The ID of the form", model=FormModel, level=AccessType.READ
131
+ )
132
+ )
133
+ @filtermodel(model="form", plugin="jsonforms")
134
+ def getForm(self, form):
135
+ return FormModel().materialize(form, self.getCurrentUser())
136
+
137
+ @access.user
138
+ @autoDescribeRoute(
139
+ Description("Create a new form")
140
+ .param("name", "The name of the form", required=True, dataType="string")
141
+ .param(
142
+ "description",
143
+ "The description of the form",
144
+ required=True,
145
+ dataType="string",
146
+ )
147
+ .param("schema", "The schema of the form", required=True, dataType="string")
148
+ .modelParam(
149
+ "folderId",
150
+ "The folder ID to save the form",
151
+ required=False,
152
+ model=Folder,
153
+ paramType="query",
154
+ level=AccessType.WRITE,
155
+ )
156
+ .param(
157
+ "pathTemplate",
158
+ "Python template string to transform destination path based on form entry",
159
+ required=False,
160
+ dataType="string",
161
+ )
162
+ .param(
163
+ "entryFileName",
164
+ "The name of the file to save the form entry in the destination folder",
165
+ required=False,
166
+ dataType="string",
167
+ )
168
+ .param(
169
+ "gdriveFolderId",
170
+ "Google Drive folder ID to save the form entry",
171
+ required=False,
172
+ dataType="string",
173
+ )
174
+ .param(
175
+ "serialize",
176
+ "Store the form schema as a serialized JSON string",
177
+ required=False,
178
+ dataType="boolean",
179
+ default=False,
180
+ )
181
+ .param(
182
+ "uniqueField",
183
+ "The field name used as an unique index",
184
+ required=True,
185
+ dataType="string",
186
+ default="sampleId",
187
+ )
188
+ )
189
+ @filtermodel(model="form", plugin="jsonforms")
190
+ def createForm(
191
+ self,
192
+ name,
193
+ description,
194
+ schema,
195
+ folder,
196
+ pathTemplate,
197
+ entryFileName,
198
+ gdriveFolderId,
199
+ serialize,
200
+ uniqueField,
201
+ ):
202
+ return FormModel().create(
203
+ name,
204
+ description,
205
+ schema,
206
+ self.getCurrentUser(),
207
+ folder=folder,
208
+ pathTemplate=pathTemplate,
209
+ entryFileName=entryFileName,
210
+ gdriveFolderId=gdriveFolderId or None,
211
+ serialize=serialize,
212
+ uniqueField=uniqueField,
213
+ )
214
+
215
+ @access.user(scope=TokenScope.DATA_WRITE)
216
+ @filtermodel(model="form", plugin="jsonforms")
217
+ @autoDescribeRoute(
218
+ Description("Update a form")
219
+ .modelParam("id", "The ID of the form", model=FormModel, level=AccessType.WRITE)
220
+ .param("name", "The name of the form", required=False, dataType="string")
221
+ .param(
222
+ "description",
223
+ "The description of the form",
224
+ required=False,
225
+ dataType="string",
226
+ )
227
+ .param("schema", "The schema of the form", required=False, dataType="string")
228
+ .modelParam(
229
+ "folderId",
230
+ "The folder ID to save the form",
231
+ model=Folder,
232
+ required=False,
233
+ paramType="query",
234
+ level=AccessType.WRITE,
235
+ )
236
+ .param(
237
+ "pathTemplate",
238
+ "Python template string to transform destination path based on form entry",
239
+ required=False,
240
+ dataType="string",
241
+ )
242
+ .param(
243
+ "entryFileName",
244
+ "The name of the file to save the form entry in the destination folder",
245
+ required=False,
246
+ dataType="string",
247
+ )
248
+ .param(
249
+ "gdriveFolderId",
250
+ "Google Drive folder ID to save the form entry",
251
+ required=False,
252
+ dataType="string",
253
+ )
254
+ .param(
255
+ "serialize",
256
+ "Store the form schema as a serialized JSON string",
257
+ required=False,
258
+ dataType="boolean",
259
+ )
260
+ .param(
261
+ "uniqueField",
262
+ "The field name used as an unique index",
263
+ required=False,
264
+ dataType="string",
265
+ )
266
+ .responseClass("Form")
267
+ .errorResponse("ID was invalid.")
268
+ .errorResponse("Write access was denied on the form.", 403)
269
+ )
270
+ def updateForm(
271
+ self,
272
+ form,
273
+ name,
274
+ description,
275
+ schema,
276
+ folder,
277
+ pathTemplate,
278
+ entryFileName,
279
+ gdriveFolderId,
280
+ serialize,
281
+ uniqueField,
282
+ ):
283
+ if name is not None:
284
+ form["name"] = name
285
+ if description is not None:
286
+ form["description"] = description
287
+ if schema is not None:
288
+ form["schema"] = schema
289
+ if entryFileName is not None:
290
+ form["entryFileName"] = entryFileName
291
+ if folder:
292
+ form["folderId"] = folder["_id"]
293
+ if pathTemplate is not None:
294
+ if not pathTemplate:
295
+ form["pathTemplate"] = None
296
+ else:
297
+ form["pathTemplate"] = pathTemplate
298
+ if gdriveFolderId:
299
+ form["gdriveFolderId"] = gdriveFolderId
300
+ if serialize is not None:
301
+ form["serialize"] = serialize
302
+ if uniqueField is not None:
303
+ form["uniqueField"] = uniqueField
304
+ return FormModel().save(form)
305
+
306
+ @access.user
307
+ @autoDescribeRoute(
308
+ Description("Delete a form").modelParam(
309
+ "id", "The ID of the form", model=FormModel, level=AccessType.WRITE
310
+ )
311
+ )
312
+ @filtermodel(model="form", plugin="jsonforms")
313
+ def deleteForm(self, form):
314
+ FormModel().remove(form)
315
+
316
+ @access.user(scope=TokenScope.DATA_OWN)
317
+ @autoDescribeRoute(
318
+ Description("Get the access control list for a form").modelParam(
319
+ "id", "The ID of the form", model=FormModel, level=AccessType.ADMIN
320
+ )
321
+ )
322
+ def getFromAccess(self, form):
323
+ return FormModel().getFullAccessList(form)
324
+
325
+ @access.user(scope=TokenScope.DATA_OWN)
326
+ @autoDescribeRoute(
327
+ Description("Update the access control list for a form")
328
+ .modelParam("id", "The ID of the form", model=FormModel, level=AccessType.ADMIN)
329
+ .jsonParam(
330
+ "access", "The JSON-encoded access control list.", requireObject=True
331
+ )
332
+ .jsonParam(
333
+ "publicFlags",
334
+ "JSON list of public access flags.",
335
+ requireArray=True,
336
+ required=False,
337
+ )
338
+ .param(
339
+ "public",
340
+ "Whether the form should be publicly visible.",
341
+ dataType="boolean",
342
+ required=False,
343
+ )
344
+ .errorResponse("ID was invalid.")
345
+ .errorResponse("Admin access was denied for the form.", 403)
346
+ )
347
+ def updateFromAccess(self, form, access, publicFlags, public):
348
+ user = self.getCurrentUser()
349
+ if form["folderId"]:
350
+ folder = Folder().load(form["folderId"], force=True)
351
+ Folder().setAccessList(
352
+ folder,
353
+ access,
354
+ save=True,
355
+ recurse=True,
356
+ user=user,
357
+ progress=noProgress,
358
+ setPublic=public,
359
+ publicFlags=publicFlags,
360
+ )
361
+
362
+ return FormModel().setAccessList(form, access, save=True, user=user)