Skip to content

Commit

Permalink
Add configurable limit to CSV file size
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Feb 11, 2020
1 parent ceb2d0f commit a3a8a5f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
20 changes: 12 additions & 8 deletions omero_parade/csv_filters/data_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@
from io import StringIO

from omero.model import FileAnnotationI
from omero_parade import parade_settings

logger = logging.getLogger(__name__)
MAX_CSV_SIZE = parade_settings.MAX_CSV_SIZE


def get_csv_files(conn, obj_type, obj_id):
def get_csv_annotations(conn, obj_type, obj_id):

csv_files = []

obj = conn.getObject(obj_type, obj_id)
if obj is None:
return []

for ann in obj.listAnnotations():
if (ann.OMERO_TYPE == FileAnnotationI):
if ann.file.name.val.endswith('.csv'):
csv_files.append(ann)
if (ann.OMERO_TYPE == FileAnnotationI and
ann.file.size.val < MAX_CSV_SIZE and
ann.file.name.val.endswith('.csv')):
csv_files.append(ann)

return csv_files

Expand All @@ -47,12 +52,11 @@ def get_csv_column_names(file_ann):


def get_names(conn, obj_type, obj_id):
csv_files = get_csv_files(conn, obj_type, obj_id)
csv_files = get_csv_annotations(conn, obj_type, obj_id)

names = []
for f in csv_files:
fname = f.file.name.val
print(fname)
for col in get_csv_column_names(f):
names.append("%s %s" % (fname, col))
return names
Expand All @@ -77,7 +81,7 @@ def get_data(request, data_name, conn):
for dtype in obj_types:
if request.GET.get(dtype) is not None:
obj_id = request.GET.get(dtype)
for file_ann in get_csv_files(conn, dtype, obj_id):
for file_ann in get_csv_annotations(conn, dtype, obj_id):
if data_name.startswith(file_ann.getFile().name):
csv_file = file_ann.getFile()
break
Expand Down
32 changes: 15 additions & 17 deletions omero_parade/csv_filters/omero_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@
from omero.model import FileAnnotationI
from omero_parade.views import NUMPY_GT_1_11_0
from omero_parade.utils import get_well_image_ids
from .data_providers import get_csv_annotations

logger = logging.getLogger(__name__)


def name_to_word(name):
"""
Replace any non alpha-numeric character or . with underscore.
This allows us to use the filter name in URL to load filter script
"""
w = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.'
return "".join([l if l in w else '_' for l in name])

Expand All @@ -39,23 +45,17 @@ def get_filters(request, conn):

obj_types = ["project", "dataset", "screen", "plate"]

obj = None
for ob_type in obj_types:
if request.GET.get(ob_type) is not None:
obj_id = request.GET.get(ob_type)
obj = conn.getObject(ob_type, obj_id)
obj_type = None
obj_id = None
for o in obj_types:
if request.GET.get(o) is not None:
obj_id = request.GET.get(o)
obj_type = o
break

if obj is None:
return []

csv_names = []
for ann in obj.listAnnotations():
if (ann.OMERO_TYPE == FileAnnotationI):
if ann.file.name.val.endswith('csv'):
csv_names.append(ann.file.name.val)

csv_names = [name_to_word(name) for name in csv_names]
csv_anns = get_csv_annotations(conn, obj_type, obj_id)
# Use name_to_word so that filter can be used in URL
csv_names = [name_to_word(ann.file.name.val) for ann in csv_anns]
return csv_names


Expand Down Expand Up @@ -86,8 +86,6 @@ def get_script(request, script_name, conn):
return JsonResponse(
{'Error': 'Specify object e.g. ?project=1'})

print('name', script_name)

# find csv file by name
file_ann = None
for ann in obj.listAnnotations():
Expand Down
4 changes: 4 additions & 0 deletions omero_parade/parade_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
# load settings
PARADE_SETTINGS_MAPPING = {

"omero.web.parade.max_csv_size": ["MAX_CSV_SIZE", 1000000, int,
("Maximum supported size of CSV files in bytes, to avoid trying \
to load very large files for filtering or plotting")],

"omero.web.parade.filters":
["PARADE_FILTERS",
'["omero_parade", "omero_parade.annotation_filters", '
Expand Down

0 comments on commit a3a8a5f

Please sign in to comment.