Skip to content

Commit

Permalink
Merge pull request #336 from jbernal0019/master
Browse files Browse the repository at this point in the history
Add `fname_nslashes` filter to all file APIs
  • Loading branch information
jbernal0019 authored Jul 31, 2021
2 parents 92018fc + 054b8dc commit 5a50f13
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
34 changes: 34 additions & 0 deletions chris_backend/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,37 @@ def json_zip2str(json_data):
json.dumps(json_data).encode('utf-8')
)
).decode('ascii')


def filter_files_by_n_slashes(queryset, value):
"""
Utility function to return the files that have the queried number of slashes in
their fname property. If the queried number ends in 'u' or 'U' then only one
file per each last "folder" in the path is returned (useful to efficiently get
the list of immediate folders under the path).
"""
value = value.lower()
try:
if value.endswith('u'):
val = int(value[:-1])
else:
val = int(value)
except Exception:
return queryset
lookup = r'^[^/]+'
for i in range(val):
lookup += '/[^/]+'
lookup += '$'
qs = queryset.filter(fname__regex=lookup)
if value.endswith('u'):
ids = []
hash_d = {}
for f in qs.all():
path = f.fname.name
last_slash_ix = path.rindex('/')
path = path[:last_slash_ix]
if path not in hash_d:
ids.append(f.id)
hash_d[path] = None
return qs.filter(pk__in=ids)
return qs
21 changes: 17 additions & 4 deletions chris_backend/pacsfiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import django_filters
from django_filters.rest_framework import FilterSet

from core.utils import filter_files_by_n_slashes


class PACS(models.Model):
identifier = models.CharField(max_length=20, unique=True)
Expand Down Expand Up @@ -45,6 +47,7 @@ class PACSFileFilter(FilterSet):
fname_exact = django_filters.CharFilter(field_name='fname', lookup_expr='exact')
fname_icontains = django_filters.CharFilter(field_name='fname',
lookup_expr='icontains')
fname_nslashes = django_filters.CharFilter(method='filter_by_n_slashes')
PatientName = django_filters.CharFilter(field_name='PatientName',
lookup_expr='icontains')
ProtocolName = django_filters.CharFilter(field_name='ProtocolName',
Expand All @@ -63,7 +66,17 @@ class PACSFileFilter(FilterSet):
class Meta:
model = PACSFile
fields = ['id', 'min_creation_date', 'max_creation_date', 'fname', 'fname_exact',
'fname_icontains', 'PatientID', 'PatientName', 'PatientSex',
'PatientAge', 'min_PatientAge', 'max_PatientAge', 'PatientBirthDate',
'StudyDate', 'ProtocolName', 'StudyInstanceUID', 'StudyDescription',
'SeriesInstanceUID', 'SeriesDescription', 'pacs_identifier']
'fname_icontains', 'fname_nslashes', 'PatientID', 'PatientName',
'PatientSex', 'PatientAge', 'min_PatientAge', 'max_PatientAge',
'PatientBirthDate', 'StudyDate', 'ProtocolName', 'StudyInstanceUID',
'StudyDescription', 'SeriesInstanceUID', 'SeriesDescription',
'pacs_identifier']

def filter_by_n_slashes(self, queryset, name, value):
"""
Custom method to return the files that have the queried number of slashes in
their fname property. If the queried number ends in 'u' or 'U' then only one
file per each last "folder" in the path is returned (useful to efficiently get
the list of immediate folders under the path).
"""
return filter_files_by_n_slashes(queryset, value)
13 changes: 12 additions & 1 deletion chris_backend/plugininstances/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import django_filters
from django_filters.rest_framework import FilterSet

from core.utils import filter_files_by_n_slashes
from feeds.models import Feed
from plugins.models import ComputeResource, Plugin, PluginParameter
from plugins.fields import CPUField, MemoryField
Expand Down Expand Up @@ -252,11 +253,21 @@ class PluginInstanceFileFilter(FilterSet):
fname_exact = django_filters.CharFilter(field_name='fname', lookup_expr='exact')
fname_icontains = django_filters.CharFilter(field_name='fname',
lookup_expr='icontains')
fname_nslashes = django_filters.CharFilter(method='filter_by_n_slashes')

class Meta:
model = PluginInstanceFile
fields = ['id', 'min_creation_date', 'max_creation_date', 'plugin_inst_id',
'feed_id', 'fname', 'fname_exact', 'fname_icontains']
'feed_id', 'fname', 'fname_exact', 'fname_icontains', 'fname_nslashes']

def filter_by_n_slashes(self, queryset, name, value):
"""
Custom method to return the files that have the queried number of slashes in
their fname property. If the queried number ends in 'u' or 'U' then only one
file per each last "folder" in the path is returned (useful to efficiently get
the list of immediate folders under the path).
"""
return filter_files_by_n_slashes(queryset, value)


class StrParameter(models.Model):
Expand Down
14 changes: 13 additions & 1 deletion chris_backend/servicefiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import django_filters
from django_filters.rest_framework import FilterSet

from core.utils import filter_files_by_n_slashes


REGISTERED_SERVICES = ['PACS']

Expand Down Expand Up @@ -35,11 +37,21 @@ class ServiceFileFilter(FilterSet):
fname_exact = django_filters.CharFilter(field_name='fname', lookup_expr='exact')
fname_icontains = django_filters.CharFilter(field_name='fname',
lookup_expr='icontains')
fname_nslashes = django_filters.CharFilter(method='filter_by_n_slashes')
service_identifier = django_filters.CharFilter(field_name='service__identifier',
lookup_expr='exact')
service_id = django_filters.CharFilter(field_name='service_id', lookup_expr='exact')

class Meta:
model = ServiceFile
fields = ['id', 'min_creation_date', 'max_creation_date', 'fname', 'fname_exact',
'fname_icontains', 'service_identifier', 'service_id']
'fname_icontains', 'fname_nslashes', 'service_identifier', 'service_id']

def filter_by_n_slashes(self, queryset, name, value):
"""
Custom method to return the files that have the queried number of slashes in
their fname property. If the queried number ends in 'u' or 'U' then only one
file per each last "folder" in the path is returned (useful to efficiently get
the list of immediate folders under the path).
"""
return filter_files_by_n_slashes(queryset, value)
14 changes: 13 additions & 1 deletion chris_backend/uploadedfiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import django_filters
from django_filters.rest_framework import FilterSet

from core.utils import filter_files_by_n_slashes


def uploaded_file_path(instance, filename):
# file will be stored to Swift at:
Expand Down Expand Up @@ -31,10 +33,20 @@ class UploadedFileFilter(FilterSet):
fname_exact = django_filters.CharFilter(field_name='fname', lookup_expr='exact')
fname_icontains = django_filters.CharFilter(field_name='fname',
lookup_expr='icontains')
fname_nslashes = django_filters.CharFilter(method='filter_by_n_slashes')
owner_username = django_filters.CharFilter(field_name='owner__username',
lookup_expr='exact')

class Meta:
model = UploadedFile
fields = ['id', 'min_creation_date', 'max_creation_date', 'fname', 'fname_exact',
'fname_icontains', 'owner_username']
'fname_icontains', 'fname_nslashes', 'owner_username']

def filter_by_n_slashes(self, queryset, name, value):
"""
Custom method to return the files that have the queried number of slashes in
their fname property. If the queried number ends in 'u' or 'U' then only one
file per each last "folder" in the path is returned (useful to efficiently get
the list of immediate folders under the path).
"""
return filter_files_by_n_slashes(queryset, value)

0 comments on commit 5a50f13

Please sign in to comment.