nci-cidc-api-modules 1.1.9__py3-none-any.whl → 1.1.10__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.
cidc_api/models/models.py CHANGED
@@ -24,10 +24,10 @@ __all__ = [
24
24
  "with_default_session",
25
25
  ]
26
26
 
27
- from collections import defaultdict
28
27
  import re
29
28
  import hashlib
30
29
  import os
30
+ from collections import defaultdict
31
31
  from datetime import datetime, timedelta
32
32
  from enum import Enum as EnumBaseClass
33
33
  from functools import wraps
@@ -149,7 +149,7 @@ def with_default_session(f):
149
149
  @wraps(f)
150
150
  def wrapped(*args, **kwargs):
151
151
  if "session" not in kwargs:
152
- kwargs["session"] = app.extensions["sqlalchemy"].db.session
152
+ kwargs["session"] = app.extensions["sqlalchemy"].session
153
153
  return f(*args, **kwargs)
154
154
 
155
155
  return wrapped
@@ -1329,6 +1329,7 @@ class TrialMetadata(CommonColumns):
1329
1329
  def list(
1330
1330
  cls,
1331
1331
  session: Session,
1332
+ user: Users,
1332
1333
  include_file_bundles: bool = False,
1333
1334
  include_counts: bool = False,
1334
1335
  **pagination_args,
@@ -1350,10 +1351,25 @@ class TrialMetadata(CommonColumns):
1350
1351
 
1351
1352
  # Add other subqueries/columns to include in the query
1352
1353
  subqueries = []
1354
+
1353
1355
  if include_file_bundles:
1354
- file_bundle_query = DownloadableFiles.build_file_bundle_query()
1356
+ allowed_upload_types = []
1357
+ if user and not user.is_admin() and not user.is_nci_user():
1358
+ permissions = Permissions.find_for_user(user.id)
1359
+ # An 'empty' upload_type means full trial-level access
1360
+ allowed_upload_types = [
1361
+ p.upload_type for p in permissions if p.upload_type
1362
+ ]
1363
+ logger.info(
1364
+ f"Restricting file bundle for user {user.id} to {allowed_upload_types=}"
1365
+ )
1366
+
1367
+ file_bundle_query = DownloadableFiles.build_file_bundle_query(
1368
+ allowed_upload_types
1369
+ )
1355
1370
  columns.append(file_bundle_query.c.file_bundle)
1356
1371
  subqueries.append(file_bundle_query)
1372
+
1357
1373
  if include_counts:
1358
1374
  trial_summaries: List[dict] = cls.get_summaries()
1359
1375
 
@@ -1369,6 +1385,7 @@ class TrialMetadata(CommonColumns):
1369
1385
  for subquery in subqueries:
1370
1386
  # Each subquery will have a trial_id column and one record per trial id
1371
1387
  query = query.outerjoin(subquery, cls.trial_id == subquery.c.trial_id)
1388
+
1372
1389
  query = cls._add_pagination_filters(query, **pagination_args)
1373
1390
 
1374
1391
  trials = []
@@ -3136,7 +3153,9 @@ class DownloadableFiles(CommonColumns):
3136
3153
  return [r[0] for r in query.all()]
3137
3154
 
3138
3155
  @classmethod
3139
- def build_file_bundle_query(cls) -> Query:
3156
+ def build_file_bundle_query(
3157
+ cls, allowed_upload_types: Optional[List[str]]
3158
+ ) -> Query:
3140
3159
  """
3141
3160
  Build a query that selects nested file bundles from the downloadable files table.
3142
3161
  The `file_bundles` query below should produce one bundle per unique `trial_id` that
@@ -3151,6 +3170,8 @@ class DownloadableFiles(CommonColumns):
3151
3170
  }
3152
3171
  ```
3153
3172
  where "type" is something like `"Olink"` or `"Participants Info"` and "purpose" is a `FilePurpose` string.
3173
+
3174
+ If `allowed_upload_types` is provided, the query will filter by files that only have an `upload_type` that appear in the list.
3154
3175
  """
3155
3176
  tid_col, type_col, purp_col, ids_col, purps_col = (
3156
3177
  literal_column("trial_id"),
@@ -3160,18 +3181,20 @@ class DownloadableFiles(CommonColumns):
3160
3181
  literal_column("purposes"),
3161
3182
  )
3162
3183
 
3163
- id_bundles = (
3164
- select(
3165
- [
3166
- cls.trial_id,
3167
- cls.data_category_prefix.label(type_col.key),
3168
- cls.file_purpose.label(purp_col.key),
3169
- func.json_agg(cls.id).label(ids_col.key),
3170
- ]
3171
- )
3172
- .group_by(cls.trial_id, cls.data_category_prefix, cls.file_purpose)
3173
- .alias("id_bundles")
3174
- )
3184
+ id_bundles = select(
3185
+ [
3186
+ cls.trial_id,
3187
+ cls.data_category_prefix.label(type_col.key),
3188
+ cls.file_purpose.label(purp_col.key),
3189
+ func.json_agg(cls.id).label(ids_col.key),
3190
+ ]
3191
+ ).group_by(cls.trial_id, cls.data_category_prefix, cls.file_purpose)
3192
+
3193
+ # Restrict files from appearing in the file bundle if the user doesn't have permissions for them
3194
+ if allowed_upload_types:
3195
+ id_bundles = id_bundles.filter(cls.upload_type.in_(allowed_upload_types))
3196
+ id_bundles = id_bundles.alias("id_bundles")
3197
+
3175
3198
  purpose_bundles = (
3176
3199
  select(
3177
3200
  [
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nci_cidc_api_modules
3
- Version: 1.1.9
3
+ Version: 1.1.10
4
4
  Summary: SQLAlchemy data models and configuration tools used in the NCI CIDC API
5
5
  Home-page: https://github.com/NCI-CIDC/cidc-api-gae
6
6
  License: MIT license
@@ -209,19 +209,27 @@ FLASK_APP=cidc_api.app:app flask db upgrade
209
209
  Install the [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test):
210
210
 
211
211
  ```bash
212
- curl -o /usr/local/bin/cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
213
- chmod +x /usr/local/bin/cloud_sql_proxy
212
+ sudo curl -o /usr/local/bin/cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.15.1/cloud-sql-proxy.darwin.amd64
213
+ sudo chmod +x /usr/local/bin/cloud-sql-proxy
214
214
  mkdir ~/.cloudsql
215
- chmod 777 ~/.cloudsql
215
+ chmod 770 ~/.cloudsql
216
216
  ```
217
217
 
218
218
  Proxy to the dev Cloud SQL instance:
219
219
 
220
220
  ```bash
221
- cloud-sql-proxy --unix-socket ~/.cloudsql/ nih-nci-cimac-cidc-dev2:us-east4:cidc-postgresql-dev2 &
221
+ cloud-sql-proxy --auto-iam-authn --address 127.0.0.1 --port 5432 nih-nci-cimac-cidc-dev2:us-east4:cidc-postgresql-dev2 &
222
222
  ```
223
223
 
224
- In your `.env` file, comment out `POSTGRES_URI` and uncomment all environment variables prefixed with `CLOUD_SQL_`. Change CLOUD_SQL_SOCKET_DIR to contain a reference to your home directory. Restart your local API instance, and it will connect to the staging Cloud SQL instance via the local proxy.
224
+ If you want to run the proxy alongside a postgres instance on localhost listening on 5432, change the port for the proxy to another port instead like 5433.
225
+ If you experience auth errors, make sure your google cloud sdk is authenticated.
226
+
227
+ ```bash
228
+ gcloud auth login
229
+ gcloud auth application-default login
230
+ ```
231
+
232
+ To point an API running on localhost to the remote Postgres database, edit your `.env` file and comment out `POSTGRES_URI` and uncomment all environment variables prefixed with `CLOUD_SQL_`. Change CLOUD_SQL_SOCKET_DIR to contain a reference to your home directory. Restart your local API instance, and it will connect to the staging Cloud SQL instance via the local proxy.
225
233
 
226
234
  If you wish to connect to the staging Cloud SQL instance via the postgres REPL, download and run the CIDC sql proxy tool (a wrapper for `cloud_sql_proxy`):
227
235
 
@@ -8,7 +8,7 @@ cidc_api/csms/auth.py,sha256=25Yma2Kz3KLENAPSeBYacFuSZXng-EDgmgInKBsRyP0,3191
8
8
  cidc_api/models/__init__.py,sha256=bl445G8Zic9YbhZ8ZBni07wtBMhLJRMBA-JqjLxx2bw,66
9
9
  cidc_api/models/csms_api.py,sha256=_uB9ZoxCFxKO8ZDTxCjS0CpeQg14EdlkEqnwyAFyYFQ,31377
10
10
  cidc_api/models/migrations.py,sha256=gp9vtkYbA9FFy2s-7woelAmsvQbJ41LO2_DY-YkFIrQ,11464
11
- cidc_api/models/models.py,sha256=kszT6ApBOIDsRJRHiYQvWo8283Jox_x0nPuetXBeK74,126680
11
+ cidc_api/models/models.py,sha256=71QpE6ZdLcnarzTrbckJFkCQ4cNN7WD_X-EsUXt_R-Y,127642
12
12
  cidc_api/models/schemas.py,sha256=7tDYtmULuzTt2kg7RorWhte06ffalgpQKrFiDRGcPEQ,2711
13
13
  cidc_api/models/files/__init__.py,sha256=8BMTnUSHzUbz0lBeEQY6NvApxDD3GMWMduoVMos2g4Y,213
14
14
  cidc_api/models/files/details.py,sha256=h6R0p_hi-ukHsO7HV-3Wukccp0zRLJ1Oie_JNA_7Pl0,62274
@@ -19,8 +19,8 @@ cidc_api/shared/emails.py,sha256=FXW9UfI2bCus350SQuL7ZQYq1Vg-vGXaGWmRfA6z2nM,440
19
19
  cidc_api/shared/gcloud_client.py,sha256=7dDs0crLMJKdIp4IDSfrZBMB3h-zvWNieB81azoeLO4,33746
20
20
  cidc_api/shared/jose.py,sha256=QO30uIhbYDwzPEWWJXz0PfyV7E1AZHReEZJUVT70UJY,1844
21
21
  cidc_api/shared/rest_utils.py,sha256=LMfBpvJRjkfQjCzVXuhTTe4Foz4wlvaKg6QntyR-Hkc,6648
22
- nci_cidc_api_modules-1.1.9.dist-info/licenses/LICENSE,sha256=pNYWVTHaYonnmJyplmeAp7tQAjosmDpAWjb34jjv7Xs,1102
23
- nci_cidc_api_modules-1.1.9.dist-info/METADATA,sha256=nePLp9yaC9CqWH-S3tTjDTb6eCYO-5ffp_1dt4ImZJg,40824
24
- nci_cidc_api_modules-1.1.9.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
25
- nci_cidc_api_modules-1.1.9.dist-info/top_level.txt,sha256=rNiRzL0lJGi5Q9tY9uSoMdTbJ-7u5c_D2E86KA94yRA,9
26
- nci_cidc_api_modules-1.1.9.dist-info/RECORD,,
22
+ nci_cidc_api_modules-1.1.10.dist-info/licenses/LICENSE,sha256=pNYWVTHaYonnmJyplmeAp7tQAjosmDpAWjb34jjv7Xs,1102
23
+ nci_cidc_api_modules-1.1.10.dist-info/METADATA,sha256=WG80O_AKzmoKtYB-85vOLgy6pwduCKhngjKfaBs2S5I,41284
24
+ nci_cidc_api_modules-1.1.10.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
25
+ nci_cidc_api_modules-1.1.10.dist-info/top_level.txt,sha256=rNiRzL0lJGi5Q9tY9uSoMdTbJ-7u5c_D2E86KA94yRA,9
26
+ nci_cidc_api_modules-1.1.10.dist-info/RECORD,,