diff --git a/atlas/forms.py b/atlas/forms.py index 9771a4c8..7ca34a9f 100755 --- a/atlas/forms.py +++ b/atlas/forms.py @@ -1,3 +1,4 @@ +from django.contrib.admin import widgets from django.forms import ( Form, ModelForm, @@ -35,6 +36,15 @@ class SeriesFindingForm(ModelForm): model = SeriesFinding exclude = ["series", "annotation_json", "viewport_json"] + widgets = { + "finding": autocomplete.ModelSelect2Multiple( + url="atlas:finding-autocomplete" + ), + "structure": autocomplete.ModelSelect2Multiple( + url="atlas:struture-autocomplete" + ), + } + def __init__(self, *args, **kwargs): if kwargs.get("series_id"): # We get the 'initial' keyword argument or initialize it diff --git a/atlas/urls.py b/atlas/urls.py index a75b7320..e97a373a 100755 --- a/atlas/urls.py +++ b/atlas/urls.py @@ -1,6 +1,6 @@ from django.urls import path, include -from atlas.models import Condition +from atlas.models import Condition, Finding, Structure from . import views app_name = "atlas" @@ -88,4 +88,15 @@ urlpatterns = [ views.ConditionAutocomplete.as_view(model=Condition, create_field='structure'), name="condition-autocomplete", ), + path( + "finding-autocomplete", + views.ConditionAutocomplete.as_view(model=Finding, create_field='finding'), + name="finding-autocomplete", + ), + path( + "structure-autocomplete", + views.ConditionAutocomplete.as_view(model=Structure, create_field='structure'), + name="structure-autocomplete", + ), + ] diff --git a/atlas/views.py b/atlas/views.py index 75080224..dd47bee1 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -41,6 +41,7 @@ from .models import ( Series, Examination, Finding, + Structure, Subspecialty, SeriesFinding, SeriesImage, @@ -599,3 +600,37 @@ class ConditionAutocomplete(autocomplete.Select2QuerySetView): return Condition.objects.none() return qs + +class FindingAutocomplete(autocomplete.Select2QuerySetView): + def get_queryset(self): + # Don't forget to filter out results depending on the visitor ! + if not self.request.user.is_authenticated: + return Finding.objects.none() + + qs = Finding.objects.all() + + if self.q: + # This raises a fielderror which breaks creating a new item if not caught + try: + qs = qs.filter(name__contains=self.q) + except FieldError: + return Finding.objects.none() + + return qs + +class StructureAutocomplete(autocomplete.Select2QuerySetView): + def get_queryset(self): + # Don't forget to filter out results depending on the visitor ! + if not self.request.user.is_authenticated: + return Structure.objects.none() + + qs = Structure.objects.all() + + if self.q: + # This raises a fielderror which breaks creating a new item if not caught + try: + qs = qs.filter(name__contains=self.q) + except FieldError: + return Structure.objects.none() + + return qs \ No newline at end of file